Exemple #1
0
 def start_serialization(self):
     """
     Start serialization -- open the XML document and the root element.
     """
     self.xml = SimplerXMLGenerator(self.stream,
                                    self.options.get("encoding", 'utf-8'))
     self.xml.startDocument()
     self.xml.startElement("django-pbs", {
         "version": "1.0",
         "xmlns:xlink": "http://www.w3.org/1999/xlink",
     })
Exemple #2
0
 def start_serialization(self):
     """
     Start serialization -- open the XML document and the root element.
     """
     self.xml = SimplerXMLGenerator(self.stream, self.options.get("encoding", 'utf-8'))
     self.xml.startDocument()
     self.xml.startElement("django-pbs", {
             "version" : "1.0",
             "xmlns:xlink": "http://www.w3.org/1999/xlink",
             })
Exemple #3
0
class Serializer(base.Serializer):
    """
    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):
        """
        Start serialization -- open the XML document and the root element.
        """
        self.xml = SimplerXMLGenerator(self.stream,
                                       self.options.get("encoding", 'utf-8'))
        self.xml.startDocument()
        self.xml.startElement("django-pbs", {
            "version": "1.0",
            "xmlns:xlink": "http://www.w3.org/1999/xlink",
        })

    def end_serialization(self):
        """
        End serialization -- end the document.
        """
        self.indent(0)
        self.xml.endElement("django-pbs")
        self.xml.endDocument()

    def start_object(self, obj):
        """
        Called as each object is handled.
        """
        self.indent(1)
        self.xml.startElement(
            'object', {
                "pk":
                obj._get_pk_val(),
                "xlink:href":
                'http://%s/xml%s' % (socket.gethostbyaddr(
                    socket.gethostname())[0], obj.get_absolute_url()),
                "class":
                '%s.%s' % (obj.__module__, obj.__class__.__name__),
            })

    def end_object(self, obj):
        """
        Called after handling all fields for an object.
        """
        self.indent(1)
        self.xml.endElement('object')

    def handle_field(self, obj, field):
        """
        Called to handle each field on an object (except for ForeignKeys and
        ManyToManyFields)
        """

        self.indent(2)
        self.xml.startElement("field", {
            "name": field,
            "type": type(getattr(obj, field)).__name__
        })

        # Get a "string version" of the object's data (this is handled by the
        # serializer base class).
        #if getattr(obj, field.name) is not None:
        value = self.get_string_value(obj, field)
        self.xml.characters(value)
        #else:
        #self.xml.addQuickElement("None")

        self.xml.endElement("field")

    def handle_relation(self, object, field=None):

        self.indent(2)
        self.xml.startElement(
            'object', {
                "pk":
                object._get_pk_val(),
                "xlink:href":
                'http://%s/xml%s' % (socket.gethostbyaddr(
                    socket.gethostname())[0], object.get_absolute_url()),
                "class":
                '%s.%s' % (object.__module__, object.__class__.__name__),
            })
        self.xml.endElement('object')

    def handle_relation_list(self, object, field):

        self.indent(2)
        self.xml.startElement('RelationList', {
            "name": field,
        })
        for item in getattr(object, field):
            self.handle_relation(item)
        self.xml.endElement("RelationList")
Exemple #4
0
class Serializer(base.Serializer):
    """
    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):
        """
        Start serialization -- open the XML document and the root element.
        """
        self.xml = SimplerXMLGenerator(self.stream, self.options.get("encoding", 'utf-8'))
        self.xml.startDocument()
        self.xml.startElement("django-pbs", {
                "version" : "1.0",
                "xmlns:xlink": "http://www.w3.org/1999/xlink",
                })

    def end_serialization(self):
        """
        End serialization -- end the document.
        """
        self.indent(0)
        self.xml.endElement("django-pbs")
        self.xml.endDocument()

    def start_object(self, obj):
        """
        Called as each object is handled.
        """
        self.indent(1)
        self.xml.startElement('object', {
                "pk"    : obj._get_pk_val(),
                "xlink:href" : 'http://%s/xml%s' % (socket.gethostbyaddr(socket.gethostname())[0], obj.get_absolute_url()),
                "class" : '%s.%s' % (obj.__module__, obj.__class__.__name__),
        })

    def end_object(self, obj):
        """
        Called after handling all fields for an object.
        """
        self.indent(1)
        self.xml.endElement('object')
    

    def handle_field(self, obj, field):
        """
        Called to handle each field on an object (except for ForeignKeys and
        ManyToManyFields)
        """

        self.indent(2)
        self.xml.startElement("field", {
            "name" : field,
            "type" : type(getattr(obj, field)).__name__
        })

        # Get a "string version" of the object's data (this is handled by the
        # serializer base class).
        #if getattr(obj, field.name) is not None:
        value = self.get_string_value(obj, field)
        self.xml.characters(value)
        #else:
            #self.xml.addQuickElement("None")

        self.xml.endElement("field")


    def handle_relation(self, object, field=None):
        
        self.indent(2)
        self.xml.startElement('object', {
                "pk"    : object._get_pk_val(),
                "xlink:href" : 'http://%s/xml%s' % (socket.gethostbyaddr(socket.gethostname())[0], object.get_absolute_url()),
                "class" : '%s.%s' % (object.__module__, object.__class__.__name__),
        })
        self.xml.endElement('object')
              

    def handle_relation_list(self, object, field):
        
        self.indent(2)
        self.xml.startElement('RelationList', {
                "name"    : field,
        })
        for item in getattr(object, field):
            self.handle_relation(item)
        self.xml.endElement("RelationList")