def load_fixture(apps, schema_editor):
from django.conf import settings
if settings.TESTS_RUNNING:
# Currently we don't have any test relying to borders, we skip
# to accelerate tests
return
fixture_file = os.path.join(fixture_dir, fixture_filename)
with open(fixture_file) as fixture:
objects = serializers.deserialize('json', fixture, ignorenonexistent=True)
for obj in objects:
obj.save()
# Most probably we won't add some revertion code to go to 0001, so this code
# won't execute ever and I take it out for coverage. At some point we may
# delete. Stefanos 2015-10-12 16:46:00+03:00
python类deserialize()的实例源码
0002_country_continent_borders_initial_data.py 文件源码
项目:grical
作者: wikical
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
def test_serialization_partial(self):
o1 = models.PartialVersionModel(
partial=Version('0.1.1', partial=True),
optional=Version('0.2.4-rc42', partial=True),
optional_spec=None,
)
o2 = models.PartialVersionModel(
partial=Version('0.4.3-rc3+build3', partial=True),
optional='',
optional_spec=Spec('==0.1.1,!=0.1.1-alpha'),
)
data = serializers.serialize('json', [o1, o2])
obj1, obj2 = serializers.deserialize('json', data)
self.assertEqual(o1.partial, obj1.object.partial)
self.assertEqual(o1.optional, obj1.object.optional)
self.assertEqual(o2.partial, obj2.object.partial)
self.assertEqual(o2.optional, obj2.object.optional)
def setUp(self):
self.image = Image.objects.create(
title="Test image",
file=get_test_image_file(),
)
# Create an image with a missing file, by deserializing fom a python object
# (which bypasses FileField's attempt to read the file)
self.bad_image = list(serializers.deserialize('python', [{
'fields': {
'title': 'missing image',
'height': 100,
'file': 'original_images/missing-image.jpg',
'width': 100,
},
'model': 'wagtailimages.image'
}]))[0].object
self.bad_image.save()
def setUp(self):
self.engine = engines['jinja2']
self.image = Image.objects.create(
title="Test image",
file=get_test_image_file(),
)
# Create an image with a missing file, by deserializing fom a python object
# (which bypasses FileField's attempt to read the file)
self.bad_image = list(serializers.deserialize('python', [{
'fields': {
'title': 'missing image',
'height': 100,
'file': 'original_images/missing-image.jpg',
'width': 100,
},
'model': 'wagtailimages.image'
}]))[0].object
self.bad_image.save()
def testSerialization(self):
model = MinimalTestingModel(pickle_field={'foo': 'bar'})
serialized = serializers.serialize('json', [model])
data = json.loads(serialized)
# determine output at runtime, because pickle output in python 3
# is different (but compatible with python 2)
p = dbsafe_encode({'foo': 'bar'})
self.assertEquals(data,
[{'pk': None, 'model': 'picklefield.minimaltestingmodel',
'fields': {"pickle_field": p}}])
for deserialized_test in serializers.deserialize('json', serialized):
self.assertEquals(deserialized_test.object,
model)
def test_model_deserialization_json(self):
dump = (
'[{'
'"fields": {"values": "|3|1|2|"}, '
'"model": "tests.integertuplemodel", "pk": 1'
'}]'
)
obj = list(deserialize('json', dump))[0].object
self.assertEqual(obj.values, (3, 1, 2))
def deserialize(format, stream_or_string, **options):
"""
Deserialize a stream or a string. Returns an iterator that yields ``(obj,
m2m_relation_dict)``, where ``obj`` is an instantiated -- but *unsaved* --
object, and ``m2m_relation_dict`` is a dictionary of ``{m2m_field_name :
list_of_related_objects}``.
"""
d = get_deserializer(format)
return d(stream_or_string, **options)
def deserialize_db_from_string(self, data):
"""
Reloads the database with data from a string generated by
the serialize_db_to_string method.
"""
data = StringIO(data)
for obj in serializers.deserialize("json", data, using=self.connection.alias):
obj.save()
def deserialize(format, stream_or_string, **options):
"""
Deserialize a stream or a string. Returns an iterator that yields ``(obj,
m2m_relation_dict)``, where ``obj`` is an instantiated -- but *unsaved* --
object, and ``m2m_relation_dict`` is a dictionary of ``{m2m_field_name :
list_of_related_objects}``.
"""
d = get_deserializer(format)
return d(stream_or_string, **options)
def deserialize(format, stream_or_string, **options):
"""
Deserialize a stream or a string. Returns an iterator that yields ``(obj,
m2m_relation_dict)``, where ``obj`` is an instantiated -- but *unsaved* --
object, and ``m2m_relation_dict`` is a dictionary of ``{m2m_field_name :
list_of_related_objects}``.
"""
d = get_deserializer(format)
return d(stream_or_string, **options)
def test_deserialize(self):
pk = Car.objects.latest("id").id + 1
json_car = json.dumps([{"model": "readonly_app.car",
"pk": pk,
"fields": {"wheel_number": 12,
"manufacturer": "Volvo"}}])
# Check that the save works
with self.assertSQLQueries(Car) as capture:
deserialized = serializers.deserialize("json", json_car)
car, = deserialized
car.save()
# Because a pk is specified, Django will try to do an UPDATE and then
# an INSERT when the UPDATE returns with 0 rows affected
self.assertNumCommands(capture, command="UPDATE", count=1)
self.assertNumCommands(capture, command="INSERT", count=1)
car = Car.objects.get(pk=pk)
self.assertEqual(car.wheel_number, 12)
self.assertEqual(car.manufacturer, "Renault")
json_car = json_car.replace("12", "14")
# Check that the UPDATE works
with self.assertSQLQueries(Car) as capture:
deserialized = serializers.deserialize("json", json_car)
car, = deserialized
car.save()
self.assertNumCommands(capture, command="UPDATE", count=1)
self.assertNumCommands(capture, command="INSERT", count=0)
car = Car.objects.get(pk=pk)
self.assertEqual(car.wheel_number, 14)
self.assertEqual(car.manufacturer, "Renault")
def deserialize(format, stream_or_string, **options):
"""
Deserialize a stream or a string. Returns an iterator that yields ``(obj,
m2m_relation_dict)``, where ``obj`` is an instantiated -- but *unsaved* --
object, and ``m2m_relation_dict`` is a dictionary of ``{m2m_field_name :
list_of_related_objects}``.
"""
d = get_deserializer(format)
return d(stream_or_string, **options)
def field_value(self):
instances = serializers.deserialize('json', self.serialized_data)
instance = list(instances)[0].object
return getattr(instance, self.field_name)
def load_fixture(cls, fixture_spec, apps, schema_editor):
app_name, model_name, fixture_file_name = fixture_spec
original_apps = serializers.python.apps
serializers.python.apps = apps
queryset = cls.get_model_class(app_name, model_name,
apps, schema_editor)
path, fixture_type = cls.fixture_path(app_name, fixture_file_name)
fixture_file = open(path)
objects = serializers.deserialize(fixture_type,
fixture_file,
ignorenonexistent=True)
for obj in objects:
cls.update_or_create_object(obj, queryset)
fixture_file.close()
serializers.python.apps = original_apps
def test_integer_loading(self):
instance = list(
serializers.deserialize('json', self.test_data_integer))[0].object
self.assertEqual(42, instance.field)
def test_char_loading(self):
instance = list(serializers.deserialize('json',
self.test_data_char))[0].object
self.assertEqual('Hello, world!', instance.field)
def test_loading(self):
instance = list(serializers.deserialize('json',
self.test_data))[0].object
self.assertEqual([1, 2, None], instance.field)
0002_data_migrations_for_fitbit_types.py 文件源码
项目:betterself
作者: jeffshek
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
def load_fixture(apps, schema_editor):
fixture_file = os.path.join(fixture_dir, fixture_filename)
fixture = open(fixture_file, 'rb')
objects = serializers.deserialize('json', fixture, ignorenonexistent=True)
for obj in objects:
obj.save()
fixture.close()
def deserialize(format, stream_or_string, **options):
"""
Deserialize a stream or a string. Returns an iterator that yields ``(obj,
m2m_relation_dict)``, where ``obj`` is an instantiated -- but *unsaved* --
object, and ``m2m_relation_dict`` is a dictionary of ``{m2m_field_name :
list_of_related_objects}``.
"""
d = get_deserializer(format)
return d(stream_or_string, **options)
def deserialize(format, stream_or_string, **options):
"""
Deserialize a stream or a string. Returns an iterator that yields ``(obj,
m2m_relation_dict)``, where ``obj`` is an instantiated -- but *unsaved* --
object, and ``m2m_relation_dict`` is a dictionary of ``{m2m_field_name :
list_of_related_objects}``.
"""
d = get_deserializer(format)
return d(stream_or_string, **options)
def test_serializer(self, format='json'):
from django.core import serializers
created = datetime.now()
x = SerializeModel(key_name='blue_key', name='blue', count=4)
x.put()
SerializeModel(name='green', count=1, created=created).put()
data = serializers.serialize(format, SerializeModel.all())
db.delete(SerializeModel.all().fetch(100))
for obj in serializers.deserialize(format, data):
obj.save()
self.validate_state(
('key.name', 'name', 'count', 'created'),
(None, 'green', 1, created),
('blue_key', 'blue', 4, None),
)
def deserialize(format, stream_or_string, **options):
"""
Deserialize a stream or a string. Returns an iterator that yields ``(obj,
m2m_relation_dict)``, where ``obj`` is an instantiated -- but *unsaved* --
object, and ``m2m_relation_dict`` is a dictionary of ``{m2m_field_name :
list_of_related_objects}``.
"""
d = get_deserializer(format)
return d(stream_or_string, **options)
def deserialize(format, stream_or_string, **options):
"""
Deserialize a stream or a string. Returns an iterator that yields ``(obj,
m2m_relation_dict)``, where ``obj`` is an instantiated -- but *unsaved* --
object, and ``m2m_relation_dict`` is a dictionary of ``{m2m_field_name :
list_of_related_objects}``.
"""
d = get_deserializer(format)
return d(stream_or_string, **options)
def deserialize(format, stream_or_string, **options):
"""
Deserialize a stream or a string. Returns an iterator that yields ``(obj,
m2m_relation_dict)``, where ``obj`` is an instantiated -- but *unsaved* --
object, and ``m2m_relation_dict`` is a dictionary of ``{m2m_field_name :
list_of_related_objects}``.
"""
d = get_deserializer(format)
return d(stream_or_string, **options)
def deserialize(format, stream_or_string, **options):
"""
Deserialize a stream or a string. Returns an iterator that yields ``(obj,
m2m_relation_dict)``, where ``obj`` is an instantiated -- but *unsaved* --
object, and ``m2m_relation_dict`` is a dictionary of ``{m2m_field_name :
list_of_related_objects}``.
"""
d = get_deserializer(format)
return d(stream_or_string, **options)
def deserialize_db_from_string(self, data):
"""
Reloads the database with data from a string generated by
the serialize_db_to_string method.
"""
data = StringIO(data)
for obj in serializers.deserialize("json", data, using=self.connection.alias):
obj.save()
def load_from_redis(cls, sensor_id):
r = redis.Redis(host=settings.REDIS_HOSTNAME, port=settings.REDIS_PORT, password=settings.REDIS_PASSWORD)
try:
redis_response = r.get('grav_{}_full'.format(sensor_id))
serializer = serializers.deserialize('json', redis_response)
for obj2 in serializer:
obj = obj2.object
return obj
except:
return None
##### Tilt Hydrometer Specific Models
def deserialize(format, stream_or_string, **options):
"""
Deserialize a stream or a string. Returns an iterator that yields ``(obj,
m2m_relation_dict)``, where ``obj`` is an instantiated -- but *unsaved* --
object, and ``m2m_relation_dict`` is a dictionary of ``{m2m_field_name :
list_of_related_objects}``.
"""
d = get_deserializer(format)
return d(stream_or_string, **options)
def deserialize(format, stream_or_string, **options):
"""
Deserialize a stream or a string. Returns an iterator that yields ``(obj,
m2m_relation_dict)``, where ``obj`` is an instantiated -- but *unsaved* --
object, and ``m2m_relation_dict`` is a dictionary of ``{m2m_field_name :
list_of_related_objects}``.
"""
d = get_deserializer(format)
return d(stream_or_string, **options)
def deserialize_db_from_string(self, data):
"""
Reloads the database with data from a string generated by
the serialize_db_to_string method.
"""
data = StringIO(data)
for obj in serializers.deserialize("json", data, using=self.connection.alias):
obj.save()