Code

Ticket #5458: 20070914_patchSerializeXml4Flex.diff

File 20070914_patchSerializeXml4Flex.diff, 2.4 KB (added by trebor74hr@…, 7 years ago)

patch described in the ticket

Line 
1Index: core/serializers/xml_serializer.py
2===================================================================
3--- core/serializers/xml_serializer.py  (revision 1465)
4+++ core/serializers/xml_serializer.py  (working copy)
5@@ -12,8 +12,16 @@
6 class Serializer(base.Serializer):
7     """
8     Serializes a QuerySet to XML.
9+     - flexmode - enables easier xml parsing from Adobe Flex (HttpService+Dataprovider), no '-' in tag names, tags 'field'  to '%field.name%' tag.
10     """
11-
12+    def serialize(self, queryset, **options):
13+        self.flexmode = options.get("flexmode")
14+        if self.flexmode:
15+            self.startelemname="django_objects"
16+        else:
17+            self.startelemname="django-objects"
18+        return base.Serializer.serialize(self, queryset, **options)
19+       
20     def indent(self, level):
21         if self.options.get('indent', None) is not None:
22             self.xml.ignorableWhitespace('\n' + ' ' * self.options.get('indent', None) * level)
23@@ -24,14 +32,14 @@
24         """
25         self.xml = SimplerXMLGenerator(self.stream, self.options.get("encoding", settings.DEFAULT_CHARSET))
26         self.xml.startDocument()
27-        self.xml.startElement("django-objects", {"version" : "1.0"})
28+        self.xml.startElement(self.startelemname, {"version" : "1.0"})
29 
30     def end_serialization(self):
31         """
32         End serialization -- end the document.
33         """
34         self.indent(0)
35-        self.xml.endElement("django-objects")
36+        self.xml.endElement(self.startelemname)
37         self.xml.endDocument()
38 
39     def start_object(self, obj):
40@@ -60,10 +68,14 @@
41         ManyToManyFields)
42         """
43         self.indent(2)
44-        self.xml.startElement("field", {
45+        if self.flexmode:
46+            elemname = field.name
47+        else:
48+            elemname = "field"
49+        self.xml.startElement(elemname, {
50             "name" : field.name,
51-            "type" : field.get_internal_type()
52-        })
53+            "type" : field.get_internal_type(),
54+            })
55 
56         # Get a "string version" of the object's data (this is handled by the
57         # serializer base class).
58@@ -73,7 +85,7 @@
59         else:
60             self.xml.addQuickElement("None")
61 
62-        self.xml.endElement("field")
63+        self.xml.endElement(elemname)
64 
65     def handle_fk_field(self, obj, field):
66         """