Python django.core.serializers.json.Serializer() Examples

The following are code examples for showing how to use django.core.serializers.json.Serializer(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: course-discovery   Author: edx   File: test_load_program_fixture.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def test_existing_seat_types(self):

        fixture = json_serializer.Serializer().serialize([
            self.organization,
            self.seat_type_verified,
            self.program_type_masters,
            self.program,
        ])
        self._mock_fixture_response(fixture)
        self.reset_db_state()

        # create existing verified seat with different pk than fixture and
        # a second seat type with the same pk but different values
        new_pk = self.seat_type_verified.id + 1
        SeatType.objects.create(id=new_pk, name='Verified', slug='verified')
        SeatType.objects.create(id=self.seat_type_verified.id, name='Test', slug='test')
        self._call_load_program_fixture([str(self.program.uuid)])

        stored_program = Program.objects.get(uuid=self.program.uuid)
        stored_seat_type = stored_program.type.applicable_seat_types.first()

        self.assertEqual(stored_seat_type.id, new_pk)
        self.assertEqual(stored_seat_type.name, self.seat_type_verified.name) 
Example 2
Project: course-discovery   Author: edx   File: test_load_program_fixture.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def test_fail_on_save_error(self):

        fixture = json_serializer.Serializer().serialize([
            self.organization,
        ])

        #  Should not be able to save an organization without uuid
        fixture_json = json.loads(fixture)
        fixture_json[0]['fields']['uuid'] = None
        fixture = json.dumps(fixture_json)

        self._mock_fixture_response(fixture)
        self.reset_db_state()

        with pytest.raises(IntegrityError) as err:
            self._call_load_program_fixture([str(self.program.uuid)])
        expected_msg = r'Failed to save course_metadata.Organization\(pk={pk}\):'.format(pk=self.organization.id)
        assert re.match(expected_msg, str(err.value)) 
Example 3
Project: course-discovery   Author: edx   File: test_load_program_fixture.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def test_ignore_program_external_key(self):
        fixture = json_serializer.Serializer().serialize([
            self.organization,
            self.seat_type_verified,
            self.program_type_masters,
            self.program,
        ])
        self._mock_fixture_response(fixture)
        self.reset_db_state()

        self._call_load_program_fixture([
            '{uuid}:{external_key}'.format(
                uuid=str(self.program.uuid),
                external_key='CS-104-FALL-2019'
            )
        ])

        Program.objects.get(uuid=self.program.uuid) 
Example 4
Project: course-discovery   Author: edx   File: views.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def get(self, request):
        uuids_string = self.request.GET.get(self.QUERY_PARAM)
        if not uuids_string:
            return HttpResponse(self.HELP_STRING, status=404)
        uuids_split = uuids_string.split(',')
        try:
            uuids = {UUID(uuid_str) for uuid_str in uuids_split}
        except ValueError:
            return HttpResponse(self.HELP_STRING, status=404)
        if len(uuids) > self.MAX_REQUESTED_PROGRAMS:
            return HttpResponse(
                'Too many programs requested, only {} allowed.'.format(self.MAX_REQUESTED_PROGRAMS),
                status=422,
            )
        programs = use_read_replica_if_available(
            Program.objects.filter(uuid__in=list(uuids))
        )
        loaded_uuids = {program.uuid for program in programs}
        bad_uuids = uuids - loaded_uuids
        if bad_uuids:
            return HttpResponse(
                "Could not load programs from UUIDs: [{}]".format(
                    ",".join(str(uuid) for uuid in bad_uuids)
                ),
                status=404,
            )
        objects = load_program_fixture(programs)
        json_text = json.Serializer().serialize(objects)
        return HttpResponse(json_text, content_type='text/json') 
Example 5
Project: course-discovery   Author: edx   File: test_load_program_fixture.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def test_update_existing_program_type(self):

        fixture = json_serializer.Serializer().serialize([
            self.organization,
            self.seat_type_verified,
            self.program_type_masters,
            self.program,
        ])
        self._mock_fixture_response(fixture)
        self.reset_db_state()

        # set DB to have a conflicting program type on load
        seat_type = SeatTypeFactory(
            name='Something',
            slug='something',
        )
        existing_program_type = ProgramTypeFactory(
            name='Masters',
            slug='masters',
            applicable_seat_types=[seat_type]
        )

        self._call_load_program_fixture([str(self.program.uuid)])

        stored_program = Program.objects.get(uuid=self.program.uuid)

        # assert existing DB value is used
        stored_program_type = stored_program.type
        self.assertEqual(stored_program_type, existing_program_type)

        # assert existing DB value is updated to match fixture
        stored_seat_types = list(stored_program_type.applicable_seat_types.all())
        self.assertEqual(len(stored_seat_types), 1)
        self.assertEqual(stored_seat_types[0].name, self.seat_type_verified.name) 
Example 6
Project: Django_Handsontable-sample   Author: thinkAmi-sandbox   File: serializers.py    The Unlicense 5 votes vote down vote up
def start_serialization(self):
        super(Serializer, self).start_serialization()
        # 日本語対応
        self.json_kwargs["ensure_ascii"] = False
        # タブインデントは2にしておく
        self.json_kwargs['indent'] = 2
        # JSONエンコーダーのクラスはデフォルトで「DjangoJSONEncoder」が指定されている
        # https://github.com/django/django/blob/1d1e246db6ae8a8c7b9a54f3485809a36c5ee373/django/core/serializers/json.py#L41 
Example 7
Project: course-discovery   Author: edx   File: test_load_program_fixture.py    GNU Affero General Public License v3.0 4 votes vote down vote up
def test_load_programs(self):

        fixture = json_serializer.Serializer().serialize([
            self.program_type_masters,
            self.program_type_mm,
            self.organization,
            self.seat_type_verified,
            self.program,
            self.program_2,
            self.program_mm,
            self.curriculum_program_membership,
            self.curriculum_course_membership,
            self.curriculum,
            self.course,
            self.course_mm,
            self.course_run,
            self.course_run_mm,
        ])
        self._mock_fixture_response(fixture)

        requested_programs = [
            str(self.program.uuid),
            str(self.program_2.uuid),
        ]
        self.reset_db_state()
        self._call_load_program_fixture(requested_programs)

        # walk through program structure to validate correct
        # objects have been created
        stored_program = Program.objects.get(uuid=self.program.uuid)
        stored_program_2 = Program.objects.get(uuid=self.program_2.uuid)
        self.assertEqual(stored_program.title, self.program.title)
        self.assertEqual(stored_program_2.title, self.program_2.title)

        stored_organization = stored_program.authoring_organizations.first()
        self.assertEqual(stored_organization.name, self.organization.name)

        # partner should use existing edx value
        self.assertEqual(stored_program.partner, self.default_partner)
        self.assertEqual(stored_organization.partner, self.default_partner)

        stored_program_type = stored_program.type
        self.assertEqual(stored_program_type.name, self.program_type_masters.name)

        stored_seat_type = stored_program_type.applicable_seat_types.first()
        self.assertEqual(stored_seat_type.name, self.seat_type_verified.name)

        stored_curriculum = stored_program.curricula.first()
        self.assertEqual(stored_curriculum.uuid, self.curriculum.uuid)

        stored_course = stored_curriculum.course_curriculum.first()
        self.assertEqual(stored_course.key, self.course.key)

        stored_mm = stored_curriculum.program_curriculum.first()
        self.assertEqual(stored_mm.uuid, self.program_mm.uuid)

        stored_course_run = stored_course.course_runs.first()
        self.assertEqual(stored_course_run.key, self.course_run.key) 
Example 8
Project: course-discovery   Author: edx   File: test_load_program_fixture.py    GNU Affero General Public License v3.0 4 votes vote down vote up
def test_update_existing_data(self):

        fixture = json_serializer.Serializer().serialize([
            self.organization,
            self.seat_type_verified,
            self.program_type_masters,
            self.program,
            self.curriculum,
            self.course,
            self.course_run,
            self.curriculum_course_membership,
        ])
        self._mock_fixture_response(fixture)
        self._call_load_program_fixture([str(self.program.uuid)])

        self.program.title = 'program-title-modified'
        self.course.title = 'course-title-modified'
        new_course = CourseFactory(partner=self.partner, authoring_organizations=[self.organization])
        new_course_run = CourseRunFactory(course=new_course)
        new_course_membership = CurriculumCourseMembershipFactory(course=new_course, curriculum=self.curriculum)

        fixture = json_serializer.Serializer().serialize([
            self.organization,
            self.seat_type_verified,
            self.program_type_masters,
            self.program,
            self.curriculum,
            self.course,
            self.course_run,
            self.curriculum_course_membership,
            new_course_membership,
            new_course,
            new_course_run,
        ])
        responses.reset()
        self._mock_oauth_request()
        self._mock_fixture_response(fixture)
        self.reset_db_state()
        self._call_load_program_fixture([str(self.program.uuid)])

        stored_program = Program.objects.get(uuid=self.program.uuid)
        self.assertEqual(stored_program.title, 'program-title-modified')

        stored_program_courses = stored_program.curricula.first().course_curriculum.all()
        modified_existing_course = stored_program_courses.get(uuid=self.course.uuid)
        stored_new_course = stored_program_courses.get(uuid=new_course.uuid)

        self.assertEqual(len(stored_program_courses), 2)
        self.assertEqual(modified_existing_course.title, 'course-title-modified')
        self.assertEqual(stored_new_course.key, new_course.key)