Ejemplo n.º 1
0
    def _serializeSqlObject(self, parentNode, sqlObj):
        # Add the 'id' attribute to the parent node.
        parentNode.newProp('id', str(sqlObj.id))

        # Add this object to the node stack.
        self.__nodeStack.append(sqlObj)

        # Serialize each column.
        for column in sqlObj.sqlmeta.columnList:
            # Retrieve the name, type, and value of the column.
            colName = column.origName
            colType = type(column)
            colValue = getattr(sqlObj, colName)

            # Get the tag name.
            colTagName = names.pythonNameToXmlTag(colName)

            # Ignore empty columns.
            if colValue is None:
                continue

            # Serialize the object based on its type.
            if column.foreignKey:
                # Do not descend into ignored relationships.  Instead, just
                # output the id of the related column.
                if (sqlObj.sqlmeta.soClass, colName) in self.ignoreRelationship:
                    parentNode.newTextChild(None, colTagName, str(colValue.id))
                    continue

                # Only traverse the foreign key if it is not already in
                # our node stack.  This is used to avoid an SQLObject ->
                # MultipleJoin -> ForeignKey -> MultipleJoin -> ... loop).
                if colValue not in self.__nodeStack:
                    node = parentNode.newChild(None, colTagName, None)
                    self._serializeSqlObject(node, colValue)
            elif column.__class__.__name__ == 'SODateTimeCol':
                parentNode.newTextChild(None, colTagName, colValue.isoformat())
            else:
                parentNode.newTextChild(None, colTagName, str(colValue))

        # Serialize each join.
        for join in sqlObj.sqlmeta.joins:
            # Ignore this join if requested to do so.
            if (sqlObj.sqlmeta.soClass, join.joinMethodName) in self.ignoreRelationship:
                continue

            # Create the join node.
            joinNode = parentNode.newChild(
                None, names.pythonNameToXmlTag(join.joinMethodName), None)

            # Add the entries from the joined table.  Do not process the
            # ForeignKey entry in the joined table that points back to this
            # table.
            for joinedRow in getattr(sqlObj, join.joinMethodName):
                itemNode = joinNode.newChild(None, 'item', None)
                self._serializeSqlObject(itemNode, joinedRow)

        # Remove this object from the node stack.
        self.__nodeStack.pop()
Ejemplo n.º 2
0
    def _serializeObject(self, parentNode, objName, obj):
        # Convert the object name to an XML tag name.
        tagName = names.pythonNameToXmlTag(objName)

        # Ignore empty objects.
        if obj is None:
            return

        # Create the node for this object.
        node = parentNode.newChild(None, tagName, None)

        # Serialize the object.
        self._serializeItem(node, obj)