Skip to content

Commit

Permalink
Added implementation of --indent for the XML serializer.
Browse files Browse the repository at this point in the history
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4733 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
freakboy3742 committed Mar 15, 2007
1 parent f9c4ce5 commit 2a8da0a
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions django/core/serializers/xml_serializer.py
Expand Up @@ -13,6 +13,10 @@ class Serializer(base.Serializer):
Serializes a QuerySet to XML. Serializes a QuerySet to XML.
""" """


def indent(self, level):
if self.options.get('indent', None) is not None:
self.xml.ignorableWhitespace('\n' + ' ' * self.options.get('indent', None) * level)

def start_serialization(self): def start_serialization(self):
""" """
Start serialization -- open the XML document and the root element. Start serialization -- open the XML document and the root element.
Expand All @@ -25,6 +29,7 @@ def end_serialization(self):
""" """
End serialization -- end the document. End serialization -- end the document.
""" """
self.indent(0)
self.xml.endElement("django-objects") self.xml.endElement("django-objects")
self.xml.endDocument() self.xml.endDocument()


Expand All @@ -35,6 +40,7 @@ def start_object(self, obj):
if not hasattr(obj, "_meta"): if not hasattr(obj, "_meta"):
raise base.SerializationError("Non-model object (%s) encountered during serialization" % type(obj)) raise base.SerializationError("Non-model object (%s) encountered during serialization" % type(obj))


self.indent(1)
self.xml.startElement("object", { self.xml.startElement("object", {
"pk" : str(obj._get_pk_val()), "pk" : str(obj._get_pk_val()),
"model" : str(obj._meta), "model" : str(obj._meta),
Expand All @@ -44,13 +50,15 @@ def end_object(self, obj):
""" """
Called after handling all fields for an object. Called after handling all fields for an object.
""" """
self.indent(1)
self.xml.endElement("object") self.xml.endElement("object")


def handle_field(self, obj, field): def handle_field(self, obj, field):
""" """
Called to handle each field on an object (except for ForeignKeys and Called to handle each field on an object (except for ForeignKeys and
ManyToManyFields) ManyToManyFields)
""" """
self.indent(2)
self.xml.startElement("field", { self.xml.startElement("field", {
"name" : field.name, "name" : field.name,
"type" : field.get_internal_type() "type" : field.get_internal_type()
Expand Down Expand Up @@ -94,6 +102,7 @@ def _start_relational_field(self, field):
""" """
Helper to output the <field> element for relational fields Helper to output the <field> element for relational fields
""" """
self.indent(2)
self.xml.startElement("field", { self.xml.startElement("field", {
"name" : field.name, "name" : field.name,
"rel" : field.rel.__class__.__name__, "rel" : field.rel.__class__.__name__,
Expand Down

0 comments on commit 2a8da0a

Please sign in to comment.