2006-06-29 00:00:37 +08:00
|
|
|
"""
|
|
|
|
Module for abstract serializer/unserializer base classes.
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
from cStringIO import StringIO
|
|
|
|
except ImportError:
|
|
|
|
from StringIO import StringIO
|
|
|
|
from django.db import models
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from django.utils.encoding import smart_str, smart_unicode
|
2006-06-29 00:00:37 +08:00
|
|
|
|
|
|
|
class SerializationError(Exception):
|
|
|
|
"""Something bad happened during serialization."""
|
|
|
|
pass
|
2006-08-31 12:11:46 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
class DeserializationError(Exception):
|
|
|
|
"""Something bad happened during deserialization."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
class Serializer(object):
|
|
|
|
"""
|
|
|
|
Abstract serializer base class.
|
|
|
|
"""
|
2006-08-31 12:11:46 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def serialize(self, queryset, **options):
|
|
|
|
"""
|
|
|
|
Serialize a queryset.
|
|
|
|
"""
|
|
|
|
self.options = options
|
2006-08-31 12:11:46 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
self.stream = options.get("stream", StringIO())
|
2006-11-07 12:50:13 +08:00
|
|
|
self.selected_fields = options.get("fields")
|
2006-08-31 12:11:46 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
self.start_serialization()
|
|
|
|
for obj in queryset:
|
|
|
|
self.start_object(obj)
|
|
|
|
for field in obj._meta.fields:
|
2007-03-19 19:57:53 +08:00
|
|
|
if field.serialize:
|
|
|
|
if field.rel is None:
|
|
|
|
if self.selected_fields is None or field.attname in self.selected_fields:
|
|
|
|
self.handle_field(obj, field)
|
|
|
|
else:
|
|
|
|
if self.selected_fields is None or field.attname[:-3] in self.selected_fields:
|
|
|
|
self.handle_fk_field(obj, field)
|
2006-06-29 00:00:37 +08:00
|
|
|
for field in obj._meta.many_to_many:
|
2007-03-19 19:57:53 +08:00
|
|
|
if field.serialize:
|
|
|
|
if self.selected_fields is None or field.attname in self.selected_fields:
|
|
|
|
self.handle_m2m_field(obj, field)
|
2006-06-29 00:00:37 +08:00
|
|
|
self.end_object(obj)
|
|
|
|
self.end_serialization()
|
|
|
|
return self.getvalue()
|
2006-08-31 12:11:46 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def get_string_value(self, obj, field):
|
|
|
|
"""
|
|
|
|
Convert a field's value to a string.
|
|
|
|
"""
|
|
|
|
if isinstance(field, models.DateTimeField):
|
2007-03-13 08:59:34 +08:00
|
|
|
value = getattr(obj, field.name).strftime("%Y-%m-%d %H:%M:%S")
|
2006-06-29 00:00:37 +08:00
|
|
|
elif isinstance(field, models.FileField):
|
|
|
|
value = getattr(obj, "get_%s_url" % field.name, lambda: None)()
|
|
|
|
else:
|
|
|
|
value = field.flatten_data(follow=None, obj=obj).get(field.name, "")
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return smart_unicode(value)
|
2006-08-31 12:11:46 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def start_serialization(self):
|
|
|
|
"""
|
|
|
|
Called when serializing of the queryset starts.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
2006-08-31 12:11:46 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def end_serialization(self):
|
|
|
|
"""
|
|
|
|
Called when serializing of the queryset ends.
|
|
|
|
"""
|
|
|
|
pass
|
2006-08-31 12:11:46 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def start_object(self, obj):
|
|
|
|
"""
|
|
|
|
Called when serializing of an object starts.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
2006-08-31 12:11:46 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def end_object(self, obj):
|
|
|
|
"""
|
|
|
|
Called when serializing of an object ends.
|
|
|
|
"""
|
|
|
|
pass
|
2006-08-31 12:11:46 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def handle_field(self, obj, field):
|
|
|
|
"""
|
|
|
|
Called to handle each individual (non-relational) field on an object.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
2006-08-31 12:11:46 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def handle_fk_field(self, obj, field):
|
|
|
|
"""
|
|
|
|
Called to handle a ForeignKey field.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
2006-08-31 12:11:46 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def handle_m2m_field(self, obj, field):
|
|
|
|
"""
|
|
|
|
Called to handle a ManyToManyField.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
2006-08-31 12:11:46 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def getvalue(self):
|
|
|
|
"""
|
2007-04-25 18:12:05 +08:00
|
|
|
Return the fully serialized queryset (or None if the output stream is
|
|
|
|
not seekable).
|
2006-06-29 00:00:37 +08:00
|
|
|
"""
|
2007-04-25 18:12:05 +08:00
|
|
|
if callable(getattr(self.stream, 'getvalue', None)):
|
|
|
|
return self.stream.getvalue()
|
2006-06-29 00:00:37 +08:00
|
|
|
|
|
|
|
class Deserializer(object):
|
|
|
|
"""
|
|
|
|
Abstract base deserializer class.
|
|
|
|
"""
|
2006-08-31 12:11:46 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def __init__(self, stream_or_string, **options):
|
|
|
|
"""
|
|
|
|
Init this serializer given a stream or a string
|
|
|
|
"""
|
|
|
|
self.options = options
|
|
|
|
if isinstance(stream_or_string, basestring):
|
|
|
|
self.stream = StringIO(stream_or_string)
|
|
|
|
else:
|
|
|
|
self.stream = stream_or_string
|
|
|
|
# hack to make sure that the models have all been loaded before
|
|
|
|
# deserialization starts (otherwise subclass calls to get_model()
|
|
|
|
# and friends might fail...)
|
|
|
|
models.get_apps()
|
2006-08-31 12:11:46 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def __iter__(self):
|
|
|
|
return self
|
2006-08-31 12:11:46 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def next(self):
|
|
|
|
"""Iteration iterface -- return the next item in the stream"""
|
|
|
|
raise NotImplementedError
|
2006-08-31 12:11:46 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
class DeserializedObject(object):
|
|
|
|
"""
|
2007-03-01 21:11:08 +08:00
|
|
|
A deserialized model.
|
2006-08-31 12:11:46 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
Basically a container for holding the pre-saved deserialized data along
|
|
|
|
with the many-to-many data saved with the object.
|
2006-08-31 12:11:46 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
Call ``save()`` to save the object (with the many-to-many data) to the
|
|
|
|
database; call ``save(save_m2m=False)`` to save just the object fields
|
|
|
|
(and not touch the many-to-many stuff.)
|
|
|
|
"""
|
2006-08-31 12:11:46 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def __init__(self, obj, m2m_data=None):
|
|
|
|
self.object = obj
|
|
|
|
self.m2m_data = m2m_data
|
2006-08-31 12:11:46 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def __repr__(self):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return "<DeserializedObject: %s>" % smart_str(self.object)
|
2006-08-31 12:11:46 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def save(self, save_m2m=True):
|
2007-07-12 15:45:35 +08:00
|
|
|
# Call save on the Model baseclass directly. This bypasses any
|
|
|
|
# model-defined save. The save is also forced to be raw.
|
|
|
|
# This ensures that the data that is deserialized is literally
|
|
|
|
# what came from the file, not post-processed by pre_save/save
|
|
|
|
# methods.
|
|
|
|
models.Model.save(self.object, raw=True)
|
2006-06-29 00:00:37 +08:00
|
|
|
if self.m2m_data and save_m2m:
|
|
|
|
for accessor_name, object_list in self.m2m_data.items():
|
|
|
|
setattr(self.object, accessor_name, object_list)
|
2006-08-31 12:11:46 +08:00
|
|
|
|
|
|
|
# prevent a second (possibly accidental) call to save() from saving
|
2006-06-29 00:00:37 +08:00
|
|
|
# the m2m data twice.
|
|
|
|
self.m2m_data = None
|