2006-06-29 00:00:37 +08:00
|
|
|
"""
|
|
|
|
XML serializer.
|
|
|
|
"""
|
|
|
|
|
2006-06-30 00:42:49 +08:00
|
|
|
from django.conf import settings
|
2006-06-29 00:00:37 +08:00
|
|
|
from django.core.serializers import base
|
|
|
|
from django.db import models
|
2006-06-30 00:42:49 +08:00
|
|
|
from django.utils.xmlutils import SimplerXMLGenerator
|
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_unicode
|
2006-06-30 00:42:49 +08:00
|
|
|
from xml.dom import pulldom
|
2006-06-29 00:00:37 +08:00
|
|
|
|
|
|
|
class Serializer(base.Serializer):
|
|
|
|
"""
|
|
|
|
Serializes a QuerySet to XML.
|
|
|
|
"""
|
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
|
|
|
|
2007-03-15 15:44:44 +08:00
|
|
|
def indent(self, level):
|
|
|
|
if self.options.get('indent', None) is not None:
|
|
|
|
self.xml.ignorableWhitespace('\n' + ' ' * self.options.get('indent', None) * level)
|
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def start_serialization(self):
|
|
|
|
"""
|
|
|
|
Start serialization -- open the XML document and the root element.
|
|
|
|
"""
|
2006-06-30 00:42:49 +08:00
|
|
|
self.xml = SimplerXMLGenerator(self.stream, self.options.get("encoding", settings.DEFAULT_CHARSET))
|
2006-06-29 00:00:37 +08:00
|
|
|
self.xml.startDocument()
|
|
|
|
self.xml.startElement("django-objects", {"version" : "1.0"})
|
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
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def end_serialization(self):
|
|
|
|
"""
|
|
|
|
End serialization -- end the document.
|
|
|
|
"""
|
2007-03-15 15:44:44 +08:00
|
|
|
self.indent(0)
|
2006-06-29 00:00:37 +08:00
|
|
|
self.xml.endElement("django-objects")
|
|
|
|
self.xml.endDocument()
|
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
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def start_object(self, obj):
|
|
|
|
"""
|
|
|
|
Called as each object is handled.
|
|
|
|
"""
|
|
|
|
if not hasattr(obj, "_meta"):
|
|
|
|
raise base.SerializationError("Non-model object (%s) encountered during serialization" % type(obj))
|
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
|
|
|
|
2007-03-15 15:44:44 +08:00
|
|
|
self.indent(1)
|
2006-06-29 00:00:37 +08:00
|
|
|
self.xml.startElement("object", {
|
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
|
|
|
"pk" : smart_unicode(obj._get_pk_val()),
|
|
|
|
"model" : smart_unicode(obj._meta),
|
2006-06-29 00:00:37 +08:00
|
|
|
})
|
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
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def end_object(self, obj):
|
|
|
|
"""
|
|
|
|
Called after handling all fields for an object.
|
|
|
|
"""
|
2007-03-15 15:44:44 +08:00
|
|
|
self.indent(1)
|
2006-06-29 00:00:37 +08:00
|
|
|
self.xml.endElement("object")
|
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
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def handle_field(self, obj, field):
|
|
|
|
"""
|
|
|
|
Called to handle each field on an object (except for ForeignKeys and
|
|
|
|
ManyToManyFields)
|
|
|
|
"""
|
2007-03-15 15:44:44 +08:00
|
|
|
self.indent(2)
|
2006-06-29 00:00:37 +08:00
|
|
|
self.xml.startElement("field", {
|
|
|
|
"name" : field.name,
|
|
|
|
"type" : field.get_internal_type()
|
|
|
|
})
|
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
|
|
|
|
2009-04-13 20:35:49 +08:00
|
|
|
# Get a "string version" of the object's data.
|
2007-03-13 08:59:34 +08:00
|
|
|
if getattr(obj, field.name) is not None:
|
2009-04-13 20:35:49 +08:00
|
|
|
self.xml.characters(field.value_to_string(obj))
|
2007-03-13 08:59:34 +08:00
|
|
|
else:
|
|
|
|
self.xml.addQuickElement("None")
|
2006-06-29 00:00:37 +08:00
|
|
|
|
|
|
|
self.xml.endElement("field")
|
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
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def handle_fk_field(self, obj, field):
|
|
|
|
"""
|
|
|
|
Called to handle a ForeignKey (we need to treat them slightly
|
|
|
|
differently from regular fields).
|
|
|
|
"""
|
|
|
|
self._start_relational_field(field)
|
|
|
|
related = getattr(obj, field.name)
|
|
|
|
if related is not None:
|
2009-12-14 20:39:20 +08:00
|
|
|
if self.use_natural_keys and hasattr(related, 'natural_key'):
|
|
|
|
# If related object has a natural key, use it
|
|
|
|
related = related.natural_key()
|
|
|
|
# Iterable natural keys are rolled out as subelements
|
|
|
|
for key_value in related:
|
|
|
|
self.xml.startElement("natural", {})
|
|
|
|
self.xml.characters(smart_unicode(key_value))
|
|
|
|
self.xml.endElement("natural")
|
2007-05-28 13:41:32 +08:00
|
|
|
else:
|
2009-12-14 20:39:20 +08:00
|
|
|
if field.rel.field_name == related._meta.pk.name:
|
|
|
|
# Related to remote object via primary key
|
|
|
|
related = related._get_pk_val()
|
|
|
|
else:
|
|
|
|
# Related to remote object via other field
|
|
|
|
related = getattr(related, field.rel.field_name)
|
|
|
|
self.xml.characters(smart_unicode(related))
|
2006-06-29 00:00:37 +08:00
|
|
|
else:
|
|
|
|
self.xml.addQuickElement("None")
|
|
|
|
self.xml.endElement("field")
|
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
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def handle_m2m_field(self, obj, field):
|
|
|
|
"""
|
|
|
|
Called to handle a ManyToManyField. Related objects are only
|
|
|
|
serialized as references to the object's PK (i.e. the related *data*
|
|
|
|
is not dumped, just the relation).
|
|
|
|
"""
|
2009-11-03 22:02:49 +08:00
|
|
|
if field.rel.through._meta.auto_created:
|
2008-08-12 20:58:33 +08:00
|
|
|
self._start_relational_field(field)
|
2009-12-14 20:39:20 +08:00
|
|
|
if self.use_natural_keys and hasattr(field.rel.to, 'natural_key'):
|
|
|
|
# If the objects in the m2m have a natural key, use it
|
|
|
|
def handle_m2m(value):
|
|
|
|
natural = value.natural_key()
|
|
|
|
# Iterable natural keys are rolled out as subelements
|
|
|
|
self.xml.startElement("object", {})
|
|
|
|
for key_value in natural:
|
|
|
|
self.xml.startElement("natural", {})
|
|
|
|
self.xml.characters(smart_unicode(key_value))
|
|
|
|
self.xml.endElement("natural")
|
|
|
|
self.xml.endElement("object")
|
|
|
|
else:
|
|
|
|
def handle_m2m(value):
|
|
|
|
self.xml.addQuickElement("object", attrs={
|
|
|
|
'pk' : smart_unicode(value._get_pk_val())
|
|
|
|
})
|
2008-08-12 20:58:33 +08:00
|
|
|
for relobj in getattr(obj, field.name).iterator():
|
2009-12-14 20:39:20 +08:00
|
|
|
handle_m2m(relobj)
|
|
|
|
|
2008-08-12 20:58:33 +08:00
|
|
|
self.xml.endElement("field")
|
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
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def _start_relational_field(self, field):
|
|
|
|
"""
|
|
|
|
Helper to output the <field> element for relational fields
|
|
|
|
"""
|
2007-03-15 15:44:44 +08:00
|
|
|
self.indent(2)
|
2006-06-29 00:00:37 +08:00
|
|
|
self.xml.startElement("field", {
|
|
|
|
"name" : field.name,
|
|
|
|
"rel" : field.rel.__class__.__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
|
|
|
"to" : smart_unicode(field.rel.to._meta),
|
2006-06-29 00:00:37 +08:00
|
|
|
})
|
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
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
class Deserializer(base.Deserializer):
|
|
|
|
"""
|
|
|
|
Deserialize XML.
|
|
|
|
"""
|
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
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def __init__(self, stream_or_string, **options):
|
|
|
|
super(Deserializer, self).__init__(stream_or_string, **options)
|
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
|
|
|
self.event_stream = pulldom.parse(self.stream)
|
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def next(self):
|
|
|
|
for event, node in self.event_stream:
|
|
|
|
if event == "START_ELEMENT" and node.nodeName == "object":
|
|
|
|
self.event_stream.expandNode(node)
|
|
|
|
return self._handle_object(node)
|
|
|
|
raise StopIteration
|
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
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def _handle_object(self, node):
|
|
|
|
"""
|
|
|
|
Convert an <object> node to a DeserializedObject.
|
|
|
|
"""
|
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
|
|
|
# Look up the model using the model loading mechanism. If this fails,
|
|
|
|
# bail.
|
2006-06-29 00:00:37 +08:00
|
|
|
Model = self._get_model_from_node(node, "model")
|
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
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
# Start building a data dictionary from the object. If the node is
|
|
|
|
# missing the pk attribute, bail.
|
|
|
|
pk = node.getAttribute("pk")
|
|
|
|
if not pk:
|
|
|
|
raise base.DeserializationError("<object> node is missing the 'pk' attribute")
|
2007-03-13 08:59:34 +08:00
|
|
|
|
|
|
|
data = {Model._meta.pk.attname : Model._meta.pk.to_python(pk)}
|
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
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
# Also start building a dict of m2m data (this is saved as
|
|
|
|
# {m2m_accessor_attribute : [list_of_related_objects]})
|
|
|
|
m2m_data = {}
|
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
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
# Deseralize each field.
|
|
|
|
for field_node in node.getElementsByTagName("field"):
|
|
|
|
# If the field is missing the name attribute, bail (are you
|
|
|
|
# sensing a pattern here?)
|
|
|
|
field_name = field_node.getAttribute("name")
|
|
|
|
if not field_name:
|
|
|
|
raise base.DeserializationError("<field> node is missing the 'name' attribute")
|
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
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
# Get the field from the Model. This will raise a
|
|
|
|
# FieldDoesNotExist if, well, the field doesn't exist, which will
|
|
|
|
# be propagated correctly.
|
|
|
|
field = Model._meta.get_field(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
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
# As is usually the case, relation fields get the special treatment.
|
|
|
|
if field.rel and isinstance(field.rel, models.ManyToManyRel):
|
2007-03-13 08:59:34 +08:00
|
|
|
m2m_data[field.name] = self._handle_m2m_field_node(field_node, field)
|
2006-06-29 00:00:37 +08:00
|
|
|
elif field.rel and isinstance(field.rel, models.ManyToOneRel):
|
2007-03-13 08:59:34 +08:00
|
|
|
data[field.attname] = self._handle_fk_field_node(field_node, field)
|
2006-06-29 00:00:37 +08:00
|
|
|
else:
|
2007-07-20 20:07:58 +08:00
|
|
|
if field_node.getElementsByTagName('None'):
|
2007-03-13 08:59:34 +08:00
|
|
|
value = None
|
|
|
|
else:
|
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
|
|
|
value = field.to_python(getInnerText(field_node).strip())
|
2006-06-29 00:00:37 +08:00
|
|
|
data[field.name] = value
|
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
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
# Return a DeserializedObject so that the m2m data has a place to live.
|
|
|
|
return base.DeserializedObject(Model(**data), m2m_data)
|
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
|
|
|
|
2007-03-13 08:59:34 +08:00
|
|
|
def _handle_fk_field_node(self, node, field):
|
2006-06-29 00:00:37 +08:00
|
|
|
"""
|
|
|
|
Handle a <field> node for a ForeignKey
|
|
|
|
"""
|
2006-11-07 13:01:35 +08:00
|
|
|
# Check if there is a child node named 'None', returning None if so.
|
2007-07-20 20:07:58 +08:00
|
|
|
if node.getElementsByTagName('None'):
|
2006-11-07 13:01:35 +08:00
|
|
|
return None
|
|
|
|
else:
|
2009-12-14 20:39:20 +08:00
|
|
|
if hasattr(field.rel.to._default_manager, 'get_by_natural_key'):
|
|
|
|
keys = node.getElementsByTagName('natural')
|
|
|
|
if keys:
|
|
|
|
# If there are 'natural' subelements, it must be a natural key
|
|
|
|
field_value = [getInnerText(k).strip() for k in keys]
|
|
|
|
obj = field.rel.to._default_manager.get_by_natural_key(*field_value)
|
|
|
|
obj_pk = getattr(obj, field.rel.field_name)
|
|
|
|
else:
|
|
|
|
# Otherwise, treat like a normal PK
|
|
|
|
field_value = getInnerText(node).strip()
|
|
|
|
obj_pk = field.rel.to._meta.get_field(field.rel.field_name).to_python(field_value)
|
|
|
|
return obj_pk
|
|
|
|
else:
|
|
|
|
field_value = getInnerText(node).strip()
|
|
|
|
return field.rel.to._meta.get_field(field.rel.field_name).to_python(field_value)
|
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
|
|
|
|
2007-03-13 08:59:34 +08:00
|
|
|
def _handle_m2m_field_node(self, node, field):
|
2006-06-29 00:00:37 +08:00
|
|
|
"""
|
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
|
|
|
Handle a <field> node for a ManyToManyField.
|
2006-06-29 00:00:37 +08:00
|
|
|
"""
|
2009-12-14 20:39:20 +08:00
|
|
|
if hasattr(field.rel.to._default_manager, 'get_by_natural_key'):
|
|
|
|
def m2m_convert(n):
|
|
|
|
keys = n.getElementsByTagName('natural')
|
|
|
|
if keys:
|
|
|
|
# If there are 'natural' subelements, it must be a natural key
|
|
|
|
field_value = [getInnerText(k).strip() for k in keys]
|
|
|
|
obj_pk = field.rel.to._default_manager.get_by_natural_key(*field_value).pk
|
|
|
|
else:
|
|
|
|
# Otherwise, treat like a normal PK value.
|
|
|
|
obj_pk = field.rel.to._meta.pk.to_python(n.getAttribute('pk'))
|
|
|
|
return obj_pk
|
|
|
|
else:
|
|
|
|
m2m_convert = lambda n: field.rel.to._meta.pk.to_python(n.getAttribute('pk'))
|
|
|
|
return [m2m_convert(c) for c in node.getElementsByTagName("object")]
|
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
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def _get_model_from_node(self, node, attr):
|
|
|
|
"""
|
|
|
|
Helper to look up a model from a <object model=...> or a <field
|
|
|
|
rel=... to=...> node.
|
|
|
|
"""
|
|
|
|
model_identifier = node.getAttribute(attr)
|
|
|
|
if not model_identifier:
|
|
|
|
raise base.DeserializationError(
|
|
|
|
"<%s> node is missing the required '%s' attribute" \
|
|
|
|
% (node.nodeName, attr))
|
|
|
|
try:
|
|
|
|
Model = models.get_model(*model_identifier.split("."))
|
|
|
|
except TypeError:
|
|
|
|
Model = None
|
|
|
|
if Model is None:
|
|
|
|
raise base.DeserializationError(
|
|
|
|
"<%s> node has invalid model identifier: '%s'" % \
|
|
|
|
(node.nodeName, model_identifier))
|
|
|
|
return Model
|
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
|
|
|
|
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
def getInnerText(node):
|
|
|
|
"""
|
|
|
|
Get all the inner text of a DOM node (recursively).
|
|
|
|
"""
|
|
|
|
# inspired by http://mail.python.org/pipermail/xml-sig/2005-March/011022.html
|
|
|
|
inner_text = []
|
|
|
|
for child in node.childNodes:
|
|
|
|
if child.nodeType == child.TEXT_NODE or child.nodeType == child.CDATA_SECTION_NODE:
|
|
|
|
inner_text.append(child.data)
|
|
|
|
elif child.nodeType == child.ELEMENT_NODE:
|
|
|
|
inner_text.extend(getInnerText(child))
|
|
|
|
else:
|
|
|
|
pass
|
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 u"".join(inner_text)
|