2006-06-29 00:00:37 +08:00
|
|
|
"""
|
|
|
|
XML serializer.
|
|
|
|
"""
|
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from xml.dom import pulldom
|
|
|
|
from xml.sax import handler
|
|
|
|
from xml.sax.expatreader import ExpatParser as _ExpatParser
|
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
from django.apps import apps
|
2006-06-30 00:42:49 +08:00
|
|
|
from django.conf import settings
|
2018-07-14 05:54:47 +08:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2006-06-29 00:00:37 +08:00
|
|
|
from django.core.serializers import base
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db import DEFAULT_DB_ALIAS, models
|
2015-06-19 14:42:48 +08:00
|
|
|
from django.utils.xmlutils import (
|
|
|
|
SimplerXMLGenerator, UnserializableContentError,
|
|
|
|
)
|
2006-06-29 00:00:37 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
class Serializer(base.Serializer):
|
2017-01-26 03:02:33 +08:00
|
|
|
"""Serialize 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):
|
2015-05-14 02:51:18 +08:00
|
|
|
if self.options.get('indent') is not None:
|
|
|
|
self.xml.ignorableWhitespace('\n' + ' ' * self.options.get('indent') * level)
|
2007-03-15 15:44:44 +08:00
|
|
|
|
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()
|
2013-10-27 09:27:42 +08:00
|
|
|
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)
|
2017-02-10 22:29:34 +08:00
|
|
|
attrs = {'model': str(obj._meta)}
|
2012-08-01 09:49:01 +08:00
|
|
|
if not self.use_natural_primary_keys or not hasattr(obj, 'natural_key'):
|
2017-06-06 03:20:34 +08:00
|
|
|
obj_pk = obj.pk
|
2012-08-01 09:49:01 +08:00
|
|
|
if obj_pk is not None:
|
2017-04-22 01:52:26 +08:00
|
|
|
attrs['pk'] = str(obj_pk)
|
2010-12-21 14:05:40 +08:00
|
|
|
|
|
|
|
self.xml.startElement("object", attrs)
|
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):
|
|
|
|
"""
|
2017-01-26 03:02:33 +08:00
|
|
|
Handle each field on an object (except for ForeignKeys and
|
|
|
|
ManyToManyFields).
|
2006-06-29 00:00:37 +08:00
|
|
|
"""
|
2007-03-15 15:44:44 +08:00
|
|
|
self.indent(2)
|
2017-02-10 22:29:34 +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:
|
2015-06-19 14:42:48 +08:00
|
|
|
try:
|
|
|
|
self.xml.characters(field.value_to_string(obj))
|
|
|
|
except UnserializableContentError:
|
|
|
|
raise ValueError("%s.%s (pk:%s) contains unserializable characters" % (
|
2017-06-06 03:20:34 +08:00
|
|
|
obj.__class__.__name__, field.name, obj.pk))
|
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):
|
|
|
|
"""
|
2017-01-26 03:02:33 +08:00
|
|
|
Handle a ForeignKey (they need to be treated slightly
|
2006-06-29 00:00:37 +08:00
|
|
|
differently from regular fields).
|
|
|
|
"""
|
|
|
|
self._start_relational_field(field)
|
2012-02-05 02:27:07 +08:00
|
|
|
related_att = getattr(obj, field.get_attname())
|
|
|
|
if related_att is not None:
|
2015-02-26 22:19:17 +08:00
|
|
|
if self.use_natural_foreign_keys and hasattr(field.remote_field.model, 'natural_key'):
|
2012-02-05 02:27:07 +08:00
|
|
|
related = getattr(obj, field.name)
|
2009-12-14 20:39:20 +08:00
|
|
|
# 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", {})
|
2017-04-22 01:52:26 +08:00
|
|
|
self.xml.characters(str(key_value))
|
2009-12-14 20:39:20 +08:00
|
|
|
self.xml.endElement("natural")
|
2007-05-28 13:41:32 +08:00
|
|
|
else:
|
2017-04-22 01:52:26 +08:00
|
|
|
self.xml.characters(str(related_att))
|
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):
|
|
|
|
"""
|
2017-01-26 03:02:33 +08:00
|
|
|
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).
|
2006-06-29 00:00:37 +08:00
|
|
|
"""
|
2015-02-26 22:19:17 +08:00
|
|
|
if field.remote_field.through._meta.auto_created:
|
2008-08-12 20:58:33 +08:00
|
|
|
self._start_relational_field(field)
|
2015-02-26 22:19:17 +08:00
|
|
|
if self.use_natural_foreign_keys and hasattr(field.remote_field.model, 'natural_key'):
|
2009-12-14 20:39:20 +08:00
|
|
|
# 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", {})
|
2017-04-22 01:52:26 +08:00
|
|
|
self.xml.characters(str(key_value))
|
2009-12-14 20:39:20 +08:00
|
|
|
self.xml.endElement("natural")
|
|
|
|
self.xml.endElement("object")
|
|
|
|
else:
|
|
|
|
def handle_m2m(value):
|
|
|
|
self.xml.addQuickElement("object", attrs={
|
2017-06-06 03:20:34 +08:00
|
|
|
'pk': str(value.pk)
|
2009-12-14 20:39:20 +08:00
|
|
|
})
|
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):
|
2017-01-26 03:02:33 +08:00
|
|
|
"""Output the <field> element for relational fields."""
|
2007-03-15 15:44:44 +08:00
|
|
|
self.indent(2)
|
2017-02-10 22:29:34 +08:00
|
|
|
self.xml.startElement('field', {
|
|
|
|
'name': field.name,
|
|
|
|
'rel': field.remote_field.__class__.__name__,
|
|
|
|
'to': str(field.remote_field.model._meta),
|
|
|
|
})
|
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
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
class Deserializer(base.Deserializer):
|
2017-01-26 03:02:33 +08:00
|
|
|
"""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
|
|
|
|
2017-02-02 00:41:56 +08:00
|
|
|
def __init__(self, stream_or_string, *, using=DEFAULT_DB_ALIAS, ignorenonexistent=False, **options):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(stream_or_string, **options)
|
2018-07-14 05:54:47 +08:00
|
|
|
self.handle_forward_references = options.pop('handle_forward_references', False)
|
2013-02-12 12:54:53 +08:00
|
|
|
self.event_stream = pulldom.parse(self.stream, self._make_parser())
|
2017-02-02 00:41:56 +08:00
|
|
|
self.db = using
|
|
|
|
self.ignore = ignorenonexistent
|
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
|
|
|
|
2013-02-12 12:54:53 +08:00
|
|
|
def _make_parser(self):
|
|
|
|
"""Create a hardened XML parser (no custom/external entities)."""
|
|
|
|
return DefusedExpatParser()
|
|
|
|
|
2012-08-09 20:36:05 +08:00
|
|
|
def __next__(self):
|
2006-06-29 00:00:37 +08:00
|
|
|
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):
|
2017-01-26 03:02:33 +08:00
|
|
|
"""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
|
|
|
|
2010-09-19 22:04:34 +08:00
|
|
|
# Start building a data dictionary from the object.
|
2012-08-01 09:49:01 +08:00
|
|
|
data = {}
|
|
|
|
if node.hasAttribute('pk'):
|
|
|
|
data[Model._meta.pk.attname] = Model._meta.pk.to_python(
|
2013-10-20 07:33:10 +08:00
|
|
|
node.getAttribute('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 = {}
|
2018-07-14 05:54:47 +08:00
|
|
|
deferred_fields = {}
|
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
|
|
|
|
2015-01-07 08:16:35 +08:00
|
|
|
field_names = {f.name for f in Model._meta.get_fields()}
|
2014-11-04 06:48:03 +08:00
|
|
|
# Deserialize each field.
|
2006-06-29 00:00:37 +08:00
|
|
|
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
|
2013-03-07 05:15:36 +08:00
|
|
|
# be propagated correctly unless ignorenonexistent=True is used.
|
2015-01-07 08:16:35 +08:00
|
|
|
if self.ignore and field_name not in field_names:
|
2013-03-07 05:15:36 +08:00
|
|
|
continue
|
2006-06-29 00:00:37 +08:00
|
|
|
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.
|
2015-02-26 22:19:17 +08:00
|
|
|
if field.remote_field and isinstance(field.remote_field, models.ManyToManyRel):
|
2018-07-14 05:54:47 +08:00
|
|
|
value = self._handle_m2m_field_node(field_node, field)
|
|
|
|
if value == base.DEFER_FIELD:
|
|
|
|
deferred_fields[field] = [
|
|
|
|
[
|
|
|
|
getInnerText(nat_node).strip()
|
|
|
|
for nat_node in obj_node.getElementsByTagName('natural')
|
|
|
|
]
|
|
|
|
for obj_node in field_node.getElementsByTagName('object')
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
m2m_data[field.name] = value
|
2015-02-26 22:19:17 +08:00
|
|
|
elif field.remote_field and isinstance(field.remote_field, models.ManyToOneRel):
|
2018-07-14 05:54:47 +08:00
|
|
|
value = self._handle_fk_field_node(field_node, field)
|
|
|
|
if value == base.DEFER_FIELD:
|
|
|
|
deferred_fields[field] = [
|
|
|
|
getInnerText(k).strip()
|
|
|
|
for k in field_node.getElementsByTagName('natural')
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
data[field.attname] = value
|
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
|
|
|
|
2012-08-01 09:49:01 +08:00
|
|
|
obj = base.build_instance(Model, data, self.db)
|
|
|
|
|
2006-06-29 00:00:37 +08:00
|
|
|
# Return a DeserializedObject so that the m2m data has a place to live.
|
2018-07-14 05:54:47 +08:00
|
|
|
return base.DeserializedObject(obj, m2m_data, deferred_fields)
|
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:
|
2015-09-12 07:33:12 +08:00
|
|
|
model = field.remote_field.model
|
|
|
|
if hasattr(model._default_manager, 'get_by_natural_key'):
|
2009-12-14 20:39:20 +08:00
|
|
|
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]
|
2018-07-14 05:54:47 +08:00
|
|
|
try:
|
|
|
|
obj = model._default_manager.db_manager(self.db).get_by_natural_key(*field_value)
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
if self.handle_forward_references:
|
|
|
|
return base.DEFER_FIELD
|
|
|
|
else:
|
|
|
|
raise
|
2015-02-26 22:19:17 +08:00
|
|
|
obj_pk = getattr(obj, field.remote_field.field_name)
|
2010-03-18 23:22:15 +08:00
|
|
|
# If this is a natural foreign key to an object that
|
|
|
|
# has a FK/O2O as the foreign key, use the FK value
|
2015-02-26 22:19:17 +08:00
|
|
|
if field.remote_field.model._meta.pk.remote_field:
|
2010-03-18 23:22:15 +08:00
|
|
|
obj_pk = obj_pk.pk
|
2009-12-14 20:39:20 +08:00
|
|
|
else:
|
|
|
|
# Otherwise, treat like a normal PK
|
|
|
|
field_value = getInnerText(node).strip()
|
2015-09-12 07:33:12 +08:00
|
|
|
obj_pk = model._meta.get_field(field.remote_field.field_name).to_python(field_value)
|
2009-12-14 20:39:20 +08:00
|
|
|
return obj_pk
|
|
|
|
else:
|
|
|
|
field_value = getInnerText(node).strip()
|
2015-09-12 07:33:12 +08:00
|
|
|
return model._meta.get_field(field.remote_field.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
|
|
|
"""
|
2015-09-12 07:33:12 +08:00
|
|
|
model = field.remote_field.model
|
|
|
|
default_manager = model._default_manager
|
|
|
|
if hasattr(default_manager, 'get_by_natural_key'):
|
2009-12-14 20:39:20 +08:00
|
|
|
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]
|
2015-09-12 07:33:12 +08:00
|
|
|
obj_pk = default_manager.db_manager(self.db).get_by_natural_key(*field_value).pk
|
2009-12-14 20:39:20 +08:00
|
|
|
else:
|
|
|
|
# Otherwise, treat like a normal PK value.
|
2015-09-12 07:33:12 +08:00
|
|
|
obj_pk = model._meta.pk.to_python(n.getAttribute('pk'))
|
2009-12-14 20:39:20 +08:00
|
|
|
return obj_pk
|
|
|
|
else:
|
2016-01-24 00:47:07 +08:00
|
|
|
def m2m_convert(n):
|
|
|
|
return model._meta.pk.to_python(n.getAttribute('pk'))
|
2018-07-14 05:54:47 +08:00
|
|
|
values = []
|
|
|
|
try:
|
|
|
|
for c in node.getElementsByTagName('object'):
|
|
|
|
values.append(m2m_convert(c))
|
|
|
|
except Exception as e:
|
|
|
|
if isinstance(e, ObjectDoesNotExist) and self.handle_forward_references:
|
|
|
|
return base.DEFER_FIELD
|
|
|
|
else:
|
|
|
|
raise base.M2MDeserializationError(e, c)
|
|
|
|
else:
|
|
|
|
return values
|
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):
|
|
|
|
"""
|
2017-01-26 03:02:33 +08:00
|
|
|
Look up a model from a <object model=...> or a <field rel=... to=...>
|
|
|
|
node.
|
2006-06-29 00:00:37 +08:00
|
|
|
"""
|
|
|
|
model_identifier = node.getAttribute(attr)
|
|
|
|
if not model_identifier:
|
|
|
|
raise base.DeserializationError(
|
2013-10-18 08:24:41 +08:00
|
|
|
"<%s> node is missing the required '%s' attribute"
|
2013-10-20 07:33:10 +08:00
|
|
|
% (node.nodeName, attr))
|
2006-06-29 00:00:37 +08:00
|
|
|
try:
|
2014-01-26 19:57:08 +08:00
|
|
|
return apps.get_model(model_identifier)
|
2013-12-28 21:55:54 +08:00
|
|
|
except (LookupError, TypeError):
|
2006-06-29 00:00:37 +08:00
|
|
|
raise base.DeserializationError(
|
2013-10-20 07:33:10 +08:00
|
|
|
"<%s> node has invalid model identifier: '%s'"
|
|
|
|
% (node.nodeName, model_identifier))
|
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):
|
2017-01-26 03:02:33 +08:00
|
|
|
"""Get all the inner text of a DOM node (recursively)."""
|
2006-06-29 00:00:37 +08:00
|
|
|
# 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:
|
2013-09-04 02:22:21 +08:00
|
|
|
pass
|
2012-06-08 00:08:47 +08:00
|
|
|
return "".join(inner_text)
|
2013-02-12 12:54:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
# Below code based on Christian Heimes' defusedxml
|
|
|
|
|
|
|
|
|
|
|
|
class DefusedExpatParser(_ExpatParser):
|
|
|
|
"""
|
|
|
|
An expat parser hardened against XML bomb attacks.
|
|
|
|
|
2017-01-26 03:02:33 +08:00
|
|
|
Forbid DTDs, external entity references
|
2013-02-12 12:54:53 +08:00
|
|
|
"""
|
|
|
|
def __init__(self, *args, **kwargs):
|
2017-12-07 22:10:32 +08:00
|
|
|
super().__init__(*args, **kwargs)
|
2013-02-12 12:54:53 +08:00
|
|
|
self.setFeature(handler.feature_external_ges, False)
|
|
|
|
self.setFeature(handler.feature_external_pes, False)
|
|
|
|
|
|
|
|
def start_doctype_decl(self, name, sysid, pubid, has_internal_subset):
|
|
|
|
raise DTDForbidden(name, sysid, pubid)
|
|
|
|
|
|
|
|
def entity_decl(self, name, is_parameter_entity, value, base,
|
|
|
|
sysid, pubid, notation_name):
|
|
|
|
raise EntitiesForbidden(name, value, base, sysid, pubid, notation_name)
|
|
|
|
|
|
|
|
def unparsed_entity_decl(self, name, base, sysid, pubid, notation_name):
|
|
|
|
# expat 1.2
|
|
|
|
raise EntitiesForbidden(name, None, base, sysid, pubid, notation_name)
|
|
|
|
|
|
|
|
def external_entity_ref_handler(self, context, base, sysid, pubid):
|
|
|
|
raise ExternalReferenceForbidden(context, base, sysid, pubid)
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
_ExpatParser.reset(self)
|
|
|
|
parser = self._parser
|
|
|
|
parser.StartDoctypeDeclHandler = self.start_doctype_decl
|
|
|
|
parser.EntityDeclHandler = self.entity_decl
|
|
|
|
parser.UnparsedEntityDeclHandler = self.unparsed_entity_decl
|
|
|
|
parser.ExternalEntityRefHandler = self.external_entity_ref_handler
|
|
|
|
|
|
|
|
|
|
|
|
class DefusedXmlException(ValueError):
|
|
|
|
"""Base exception."""
|
|
|
|
def __repr__(self):
|
|
|
|
return str(self)
|
|
|
|
|
|
|
|
|
|
|
|
class DTDForbidden(DefusedXmlException):
|
|
|
|
"""Document type definition is forbidden."""
|
|
|
|
def __init__(self, name, sysid, pubid):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__()
|
2013-02-12 12:54:53 +08:00
|
|
|
self.name = name
|
|
|
|
self.sysid = sysid
|
|
|
|
self.pubid = pubid
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
tpl = "DTDForbidden(name='{}', system_id={!r}, public_id={!r})"
|
|
|
|
return tpl.format(self.name, self.sysid, self.pubid)
|
|
|
|
|
|
|
|
|
|
|
|
class EntitiesForbidden(DefusedXmlException):
|
|
|
|
"""Entity definition is forbidden."""
|
|
|
|
def __init__(self, name, value, base, sysid, pubid, notation_name):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__()
|
2013-02-12 12:54:53 +08:00
|
|
|
self.name = name
|
|
|
|
self.value = value
|
|
|
|
self.base = base
|
|
|
|
self.sysid = sysid
|
|
|
|
self.pubid = pubid
|
|
|
|
self.notation_name = notation_name
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
tpl = "EntitiesForbidden(name='{}', system_id={!r}, public_id={!r})"
|
|
|
|
return tpl.format(self.name, self.sysid, self.pubid)
|
|
|
|
|
|
|
|
|
|
|
|
class ExternalReferenceForbidden(DefusedXmlException):
|
|
|
|
"""Resolving an external reference is forbidden."""
|
|
|
|
def __init__(self, context, base, sysid, pubid):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__()
|
2013-02-12 12:54:53 +08:00
|
|
|
self.context = context
|
|
|
|
self.base = base
|
|
|
|
self.sysid = sysid
|
|
|
|
self.pubid = pubid
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
tpl = "ExternalReferenceForbidden(system_id='{}', public_id={})"
|
|
|
|
return tpl.format(self.sysid, self.pubid)
|