本文整理汇总了Python中django.core.serializers.json.Serializer方法的典型用法代码示例。如果您正苦于以下问题:Python json.Serializer方法的具体用法?Python json.Serializer怎么用?Python json.Serializer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.core.serializers.json
的用法示例。
在下文中一共展示了json.Serializer方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_existing_seat_types
# 需要导入模块: from django.core.serializers import json [as 别名]
# 或者: from django.core.serializers.json import Serializer [as 别名]
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)
示例2: test_fail_on_save_error
# 需要导入模块: from django.core.serializers import json [as 别名]
# 或者: from django.core.serializers.json import Serializer [as 别名]
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))
示例3: test_ignore_program_external_key
# 需要导入模块: from django.core.serializers import json [as 别名]
# 或者: from django.core.serializers.json import Serializer [as 别名]
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)
示例4: get
# 需要导入模块: from django.core.serializers import json [as 别名]
# 或者: from django.core.serializers.json import Serializer [as 别名]
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')
示例5: test_update_existing_program_type
# 需要导入模块: from django.core.serializers import json [as 别名]
# 或者: from django.core.serializers.json import Serializer [as 别名]
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',
name_t='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)
示例6: test_load_programs
# 需要导入模块: from django.core.serializers import json [as 别名]
# 或者: from django.core.serializers.json import Serializer [as 别名]
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_t, 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)
示例7: test_update_existing_data
# 需要导入模块: from django.core.serializers import json [as 别名]
# 或者: from django.core.serializers.json import Serializer [as 别名]
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)