Exemplo n.º 1
0
    def collect_card_widget_node_data(self, graph_obj, graph, parentcard, nodegroupids=[]):
        nodegroupids.append(str(parentcard.nodegroup_id))
        for node in graph_obj["nodes"]:
            if node["nodegroup_id"] == str(parentcard.nodegroup_id):
                found = False
                for widget in graph_obj["widgets"]:
                    if node["nodeid"] == str(widget.node_id):
                        found = True
                        try:
                            collection_id = node["config"]["rdmCollection"]
                            concept_collection = Concept().get_child_collections_hierarchically(collection_id, offset=None)
                            widget.config["options"] = concept_collection
                        except Exception as e:
                            pass
                        break
                if not found:
                    for card in graph_obj["cards"]:
                        if card["nodegroup_id"] == node["nodegroup_id"]:
                            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 = card["cardid"]
                                widget_model.widget_id = widget.pk
                                widget_model.config = widget.defaultconfig
                                try:
                                    collection_id = node["config"]["rdmCollection"]
                                    if collection_id:
                                        concept_collection = Concept().get_child_collections_hierarchically(collection_id, offset=None)
                                        widget_model.config["options"] = concept_collection
                                except Exception as e:
                                    pass
                                widget_model.label = node["name"]
                                graph_obj["widgets"].append(widget_model)
                            break

                if node["datatype"] == "resource-instance" or node["datatype"] == "resource-instance-list":
                    if node["config"]["graphs"] is not None:
                        graph_ids = []
                        for graph in node["config"]["graphs"]:
                            graphuuid = uuid.UUID(graph["graphid"])
                            graph_ids.append(str(graphuuid))
                        node["config"]["options"] = []
                        for resource_instance in Resource.objects.filter(graph_id__in=graph_ids):
                            node["config"]["options"].append(
                                {
                                    "id": str(resource_instance.pk),
                                    "name": resource_instance.displayname,
                                    "graphid": str(resource_instance.graph_id),
                                }
                            )

        for subcard in parentcard.cards:
            self.collect_card_widget_node_data(graph_obj, graph, subcard, nodegroupids)

        return graph_obj
Exemplo n.º 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:
            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
Exemplo n.º 3
0
    def collect_card_widget_node_data(self, graph_obj, graph, parentcard, nodegroupids=[]):
        nodegroupids.append(str(parentcard.nodegroup_id))
        for node in graph_obj['nodes']:
            if node['nodegroup_id'] == str(parentcard.nodegroup_id):
                found = False
                for widget in graph_obj['widgets']:
                    if node['nodeid'] == str(widget.node_id):
                        found = True
                        try:
                            collection_id = node['config']['rdmCollection']
                            concept_collection = Concept().get_child_collections_hierarchically(collection_id, offset=None)
                            widget.config['options'] = concept_collection
                        except Exception as e:
                            pass
                        break
                if not found:
                    for card in graph_obj['cards']:
                        if card['nodegroup_id'] == node['nodegroup_id']:
                            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 = card['cardid']
                                widget_model.widget_id = widget.pk
                                widget_model.config = widget.defaultconfig
                                try:
                                    collection_id = node['config']['rdmCollection']
                                    if collection_id:
                                        concept_collection = Concept().get_child_collections_hierarchically(collection_id, offset=None)
                                        widget_model.config['options'] = concept_collection
                                except Exception as e:
                                    pass
                                widget_model.label = node['name']
                                graph_obj['widgets'].append(widget_model)
                            break

                if node['datatype'] == 'resource-instance' or node['datatype'] == 'resource-instance-list':
                    if node['config']['graphid'] is not None:
                        try:
                            graphuuid = uuid.UUID(node['config']['graphid'][0])
                            graph_id = unicode(graphuuid)
                        except ValueError as e:
                            graphuuid = uuid.UUID(node['config']['graphid'])
                            graph_id = unicode(graphuuid)
                        node['config']['options'] = []
                        for resource_instance in Resource.objects.filter(graph_id=graph_id):
                            node['config']['options'].append({'id': str(resource_instance.pk), 'name': resource_instance.displayname})

        for subcard in parentcard.cards:
            self.collect_card_widget_node_data(graph_obj, graph, subcard, nodegroupids)

        return graph_obj
Exemplo n.º 4
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
Exemplo n.º 5
0
    def serialize(self):
        """
        serialize to a different form then used by the internal class structure

        """

        ret = JSONSerializer().handle_model(self)
        ret['cardinality'] = self.cardinality
        ret['cards'] = self.cards
        ret['nodes'] = list(self.nodegroup.node_set.all())
        ret['visible'] = self.visible
        ret['active'] = self.active
        ret['widgets'] = self.widgets
        ret['groups'] = self.groups
        ret['users'] = self.users
        ret['ontologyproperty'] = self.ontologyproperty

        if 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
        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)

        return ret
Exemplo n.º 6
0
    def __init__(self, *args, **kwargs):
        """
        Init a Card from a dictionary representation of from a model method call

        init this object by using Django query syntax, eg:
        .. code-block:: python

            Card.objects.get(pk=some_card_id)
            # or
            Card.objects.filter(name=some_value_to_filter_by)

        OR, init this object with a dictionary, eg:
        .. code-block:: python

            Card({
                name:'some name',
                cardid: '12341234-1234-1234-1324-1234123433433',
                ...
            })

        Arguments:
        args -- a dictionary of properties repsenting a Card object
        kwargs -- unused

        """

        super(Card, self).__init__(*args, **kwargs)
        # from models.CardModel
        # self.cardid
        # self.name
        # self.description
        # self.instructions
        # self.helpenabled
        # self.helptitle
        # self.helptext
        # self.nodegroup
        # self.graph
        # self.active
        # self.visible
        # self.sortorder
        # self.cssclass
        # end from models.CardModel
        self.disabled = False
        self.cardinality = ''
        self.cards = []
        self.widgets = []
        self.nodes = []
        self.ontologyproperty = None
        self.constraints = []

        if args:
            if isinstance(args[0], dict):
                for key, value in args[0].items():
                    if key not in ('cards', 'widgets', 'nodes', 'is_editable',
                                   'nodegroup', 'constraints'):
                        setattr(self, key, value)

                if 'cards' in args[0]:
                    for card in args[0]["cards"]:
                        self.cards.append(Card(card))

                if 'constraints':
                    self.update_constraints(args[0]["constraints"])

                if 'widgets' in args[0]:
                    for widget in args[0]["widgets"]:
                        cardxnodexwidgetid = widget.get('id', None)
                        node_id = widget.get('node_id', None)
                        card_id = widget.get('card_id', None)
                        widget_id = widget.get('widget_id', None)
                        if cardxnodexwidgetid is None and (
                                node_id is not None and card_id is not None
                                and widget_id is not None):
                            try:
                                wm = models.CardXNodeXWidget.objects.get(
                                    node_id=node_id, card_id=card_id)
                                cardxnodexwidgetid = wm.pk
                            except:
                                pass
                        widget_model = models.CardXNodeXWidget()
                        widget_model.pk = cardxnodexwidgetid
                        widget_model.node_id = node_id
                        widget_model.card_id = card_id
                        widget_model.widget_id = widget_id
                        widget_model.config = widget.get('config', {})
                        widget_model.label = widget.get('label', '')
                        widget_model.visible = widget.get('visible', None)
                        widget_model.sortorder = widget.get('sortorder', None)
                        if widget_model.pk is None:
                            widget_model.save()
                        self.widgets.append(widget_model)

                if 'nodes' in args[0]:
                    for node in args[0]["nodes"]:
                        nodeid = node.get('nodeid', None)
                        if nodeid is not None:
                            node_model = models.Node.objects.get(nodeid=nodeid)
                            node_model.config = node.get('config', None)
                            node_model.isrequired = node.get(
                                'isrequired', node_model.isrequired)
                            self.nodes.append(node_model)

            else:
                self.widgets = list(self.cardxnodexwidget_set.all())

                sub_groups = models.NodeGroup.objects.filter(
                    parentnodegroup=self.nodegroup)
                for sub_group in sub_groups:
                    self.cards.extend(Card.objects.filter(nodegroup=sub_group))

                self.cardinality = self.nodegroup.cardinality
Exemplo n.º 7
0
    def __init__(self, *args, **kwargs):
        """
        Init a Card from a dictionary representation of from a model method call

        init this object by using Django query syntax, eg:
        .. code-block:: python

            Card.objects.get(pk=some_card_id)
            # or
            Card.objects.filter(name=some_value_to_filter_by)

        OR, init this object with a dictionary, eg:
        .. code-block:: python

            Card({
                name:'some name',
                cardid: '12341234-1234-1234-1324-1234123433433',
                ...
            })

        Arguments:
        args -- a dictionary of properties repsenting a Card object
        kwargs -- unused

        """

        super(Card, self).__init__(*args, **kwargs)
        # from models.CardModel
        # self.cardid
        # self.name
        # self.description
        # self.instructions
        # self.helpenabled
        # self.helptitle
        # self.helptext
        # self.nodegroup
        # self.graph
        # self.active
        # self.visible
        # self.sortorder
        # end from models.CardModel
        self.cardinality = ''
        self.cards = []
        self.widgets = []
        self.nodes = []
        self.ontologyproperty = None
        self.groups = []
        self.users = []
        self.perm_cache = {}

        if args:
            if isinstance(args[0], dict):
                for key, value in args[0].iteritems():
                    if not (key == 'cards' or key == 'widgets'
                            or key == 'nodes'):
                        setattr(self, key, value)

                for card in args[0]["cards"]:
                    self.cards.append(Card(card))

                for widget in args[0]["widgets"]:
                    widget_model = models.CardXNodeXWidget()
                    widget_model.pk = widget.get('id', None)
                    widget_model.node_id = widget.get('node_id', None)
                    widget_model.card_id = widget.get('card_id', None)
                    widget_model.widget_id = widget.get('widget_id', None)
                    widget_model.config = widget.get('config', {})
                    widget_model.label = widget.get('label', '')
                    widget_model.sortorder = widget.get('sortorder', None)
                    if widget_model.pk == None:
                        widget_model.save()
                    self.widgets.append(widget_model)

                for node in args[0]["nodes"]:
                    nodeid = node.get('nodeid', None)
                    if nodeid is not None:
                        node_model = models.Node.objects.get(nodeid=nodeid)
                        node_model.config = node.get('config', None)
                        self.nodes.append(node_model)

            else:
                self.widgets = list(self.cardxnodexwidget_set.all())

                sub_groups = models.NodeGroup.objects.filter(
                    parentnodegroup=self.nodegroup)
                for sub_group in sub_groups:
                    self.cards.extend(Card.objects.filter(nodegroup=sub_group))

                self.cardinality = self.nodegroup.cardinality
                self.groups = self.get_group_permissions(self.nodegroup)
                self.users = self.get_user_permissions(self.nodegroup)

        self.graph = Graph.objects.get(graphid=self.graph_id)
Exemplo n.º 8
0
 def serialize(self, fields=None, exclude=None):
     """
     serialize to a different form than used by the internal class structure
     used to append additional values (like parent ontology properties) that
     internal objects (like models.Nodes) don't support
     """
     serializer = JSONSerializer()
     serializer.geom_format = 'geojson'
     obj = serializer.handle_model(self)
     ordered_cards = self.get_ordered_cards()
     ret = JSONSerializer().serializeToPython(obj)
     graphs = []
     graphids = []
     for card in self.cards.all():
         if card.graph_id not in graphids:
             graphids.append(card.graph_id)
             # we may want the full proxy model at some point,
             # but for now just the root node color
             graph = Graph.objects.get(pk=card.graph_id)
             graph_obj = graph.serialize(exclude=[
                 'domain_connections', 'edges',
                 'relatable_resource_model_ids'
             ])
             graph_obj['widgets'] = list(
                 models.CardXNodeXWidget.objects.filter(
                     card__graph=graph).distinct())
             for node in graph_obj['nodes']:
                 found = False
                 for widget in graph_obj['widgets']:
                     if node['nodeid'] == str(widget.node_id):
                         found = True
                         try:
                             collection_id = node['config']['rdmCollection']
                             concept_collection = Concept(
                             ).get_child_collections_hierarchically(
                                 collection_id)
                             widget.config['options'] = concept_collection
                         except Exception as e:
                             pass
                         break
                 if not found:
                     for card in graph_obj['cards']:
                         if card['nodegroup_id'] == node['nodegroup_id']:
                             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 = card['cardid']
                                 widget_model.widget_id = widget.pk
                                 widget_model.config = widget.defaultconfig
                                 try:
                                     collection_id = node['config'][
                                         'rdmCollection']
                                     if collection_id:
                                         concept_collection = Concept(
                                         ).get_child_collections_hierarchically(
                                             collection_id)
                                         widget_model.config[
                                             'options'] = concept_collection
                                 except Exception as e:
                                     pass
                                 widget_model.label = node['name']
                                 graph_obj['widgets'].append(widget_model)
                             break
             graphs.append(graph_obj)
     ret['graphs'] = graphs
     ret['cards'] = ordered_cards
     try:
         ret['bounds'] = json.loads(ret['bounds'])
     except TypeError as e:
         print 'Could not parse', ret['bounds'], e
     return ret