示例#1
0
    def serialize(self, fields=None, exclude=None):
        """
        serialize to a different form than used by the internal class structure

        """

        exclude = [] if exclude is None else exclude
        ret = JSONSerializer().handle_model(self, fields, exclude)

        ret['cardinality'] = self.cardinality if 'cardinality' not in exclude else ret.pop(
            'cardinality', None)
        ret['cards'] = self.cards if 'cards' not in exclude else ret.pop(
            'cards', None)
        ret['nodes'] = list(self.nodegroup.node_set.all(
        )) if 'nodes' not in exclude else ret.pop('nodes', None)
        ret['visible'] = self.visible if 'visible' not in exclude else ret.pop(
            'visible', None)
        ret['active'] = self.active if 'active' not in exclude else ret.pop(
            'active', None)
        ret['is_editable'] = self.is_editable(
        ) if 'is_editable' not in exclude else ret.pop('is_editable', None)
        ret['ontologyproperty'] = self.ontologyproperty if 'ontologyproperty' not in exclude else ret.pop(
            'ontologyproperty', None)
        ret['disabled'] = self.disabled if 'disabled' not in exclude else ret.pop(
            'disabled', None)
        ret['constraints'] = self.constraints if 'constraints' not in exclude else ret.pop(
            'constraints', None)
        if self.graph and self.graph.ontology and self.graph.isresource:
            edge = self.get_edge_to_parent()
            ret['ontologyproperty'] = edge.ontologyproperty

        # provide a models.CardXNodeXWidget model for every node
        # even if a widget hasn't been configured
        ret['widgets'] = self.widgets
        if 'widgets' not in exclude:
            for node in ret['nodes']:
                found = False
                for widget in ret['widgets']:
                    if node.nodeid == widget.node_id:
                        found = True
                if not found:
                    widget = models.DDataType.objects.get(
                        pk=node.datatype).defaultwidget
                    if widget:
                        widget_model = models.CardXNodeXWidget()
                        widget_model.node_id = node.nodeid
                        widget_model.card_id = self.cardid
                        widget_model.widget_id = widget.pk
                        widget_model.config = JSONSerializer().serialize(
                            widget.defaultconfig)
                        widget_model.label = node.name
                        ret['widgets'].append(widget_model)
        else:
            ret.pop('widgets', None)

        return ret
示例#2
0
    def serialize(self, fields=None, exclude=None):
        """
        serialize to a different form than used by the internal class structure

        """
        exclude = [] if exclude is None else exclude
        ret = JSONSerializer().handle_model(self, fields, exclude)

        ret["cardinality"] = self.cardinality if "cardinality" not in exclude else ret.pop("cardinality", None)
        ret["cards"] = self.cards if "cards" not in exclude else ret.pop("cards", None)
        ret["nodes"] = list(self.nodegroup.node_set.all()) if "nodes" not in exclude else ret.pop("nodes", None)
        ret["visible"] = self.visible if "visible" not in exclude else ret.pop("visible", None)
        ret["active"] = self.active if "active" not in exclude else ret.pop("active", None)
        ret["is_editable"] = self.is_editable() if "is_editable" not in exclude else ret.pop("is_editable", None)
        ret["ontologyproperty"] = self.ontologyproperty if "ontologyproperty" not in exclude else ret.pop("ontologyproperty", None)
        ret["disabled"] = self.disabled if "disabled" not in exclude else ret.pop("disabled", None)
        ret["constraints"] = self.constraints if "constraints" not in exclude else ret.pop("constraints", None)
        if self.graph and self.graph.ontology and self.graph.isresource:
            edge = self.get_edge_to_parent()
            ret["ontologyproperty"] = edge.ontologyproperty

        # provide a models.CardXNodeXWidget model for every node
        # even if a widget hasn't been configured
        ret["widgets"] = self.widgets
        if "widgets" not in exclude:
            widgets = self.datatypes

            for node in ret["nodes"]:
                found = False
                for widget in ret["widgets"]:
                    if node.nodeid == widget.node_id:
                        found = True
                if not found:
                    widget = [widget for widget in widgets if widget.pk == node.datatype][0].defaultwidget
                    if widget:
                        widget_model = models.CardXNodeXWidget()
                        widget_model.node_id = node.nodeid
                        widget_model.card_id = self.cardid
                        widget_model.widget_id = widget.pk
                        widget_model.config = JSONSerializer().serialize(widget.defaultconfig)
                        widget_model.label = node.name
                        ret["widgets"].append(widget_model)
        else:
            ret.pop("widgets", None)

        return ret
示例#3
0
文件: card.py 项目: fargeo/arches
    def serialize(self, fields=None, exclude=None):
        """
        serialize to a different form then used by the internal class structure

        """

        exclude = [] if exclude == None else exclude
        ret = JSONSerializer().handle_model(self, fields, exclude)

        ret['cardinality'] = self.cardinality if 'cardinality' not in exclude else ret.pop('cardinality', None)
        ret['cards'] = self.cards if 'cards' not in exclude else ret.pop('cards', None)
        ret['nodes'] = list(self.nodegroup.node_set.all()) if 'nodes' not in exclude else ret.pop('nodes', None)
        ret['visible'] = self.visible if 'visible' not in exclude else ret.pop('visible', None)
        ret['active'] = self.active if 'active' not in exclude else ret.pop('active', None)
        ret['is_editable'] = self.is_editable() if 'is_editable' not in exclude else ret.pop('is_editable', None)
        ret['ontologyproperty'] = self.ontologyproperty if 'ontologyproperty' not in exclude else ret.pop('ontologyproperty', None)
        ret['disabled'] = self.disabled if 'disabled' not in exclude else ret.pop('disabled', None)

        if self.graph and self.graph.ontology and self.graph.isresource:
            edge = self.get_edge_to_parent()
            ret['ontologyproperty'] = edge.ontologyproperty

        # provide a models.CardXNodeXWidget model for every node
        # even if a widget hasn't been configured
        ret['widgets'] = self.widgets
        if 'widgets' not in exclude:
            for node in ret['nodes']:
                found = False
                for widget in ret['widgets']:
                    if node.nodeid == widget.node_id:
                        found = True
                if not found:
                    widget = models.DDataType.objects.get(pk=node.datatype).defaultwidget
                    if widget:
                        widget_model = models.CardXNodeXWidget()
                        widget_model.node_id = node.nodeid
                        widget_model.card_id = self.cardid
                        widget_model.widget_id = widget.pk
                        widget_model.config = JSONSerializer().serialize(widget.defaultconfig)
                        widget_model.label = node.name
                        ret['widgets'].append(widget_model)
        else:
            ret.pop('widgets', None)

        return ret
示例#4
0
文件: graph.py 项目: fargeo/arches
    def serialize(self, fields=None, exclude=None):
        """
        serialize to a different form then used by the internal class structure

        used to append additional values (like parent ontology properties) that
        internal objects (like models.Nodes) don't support

        """
        exclude = [] if exclude == None else exclude

        ret = JSONSerializer().handle_model(self, fields, exclude)
        ret['root'] = self.root

        if 'relatable_resource_model_ids' not in exclude:
            ret['relatable_resource_model_ids'] = [str(relatable_node.graph_id) for relatable_node in self.root.get_relatable_resources()]
        else:
            ret.pop('relatable_resource_model_ids', None)

        ret['cards'] = self.get_cards() if 'cards' not in exclude else ret.pop('cards', None)
        ret['nodegroups'] = self.get_nodegroups() if 'nodegroups' not in exclude else ret.pop('nodegroups', None)
        ret['domain_connections'] = self.get_valid_domain_ontology_classes() if 'domain_connections' not in exclude else ret.pop('domain_connections', None)
        ret['is_editable'] = self.is_editable() if 'is_editable' not in exclude else ret.pop('is_editable', None)
        ret['functions'] = models.FunctionXGraph.objects.filter(graph_id=self.graphid) if 'functions' not in exclude else ret.pop('functions', None)

        parentproperties = {
            self.root.nodeid: ''
        }

        for edge_id, edge in self.edges.iteritems():
            parentproperties[edge.rangenode_id] = edge.ontologyproperty

        ret['edges'] = [edge for key, edge in self.edges.iteritems()] if 'edges' not in exclude else ret.pop('edges', None)

        if 'nodes' not in exclude:
            ret['nodes'] = []
            for key, node in self.nodes.iteritems():
                nodeobj = JSONSerializer().serializeToPython(node)
                nodeobj['parentproperty'] = parentproperties[node.nodeid]
                ret['nodes'].append(nodeobj)
        else:
            ret.pop('nodes', None)

        res = JSONSerializer().serializeToPython(ret)

        return res