示例#1
0
    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
        """
        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
                # graphs.append(Graph.objects.get(pk=card.graph_id))
                graph = JSONSerializer().serializeToPython(
                    card.graph,
                    exclude=[
                        'functions', 'disable_instance_creation',
                        'deploymentdate', 'deploymentfile'
                    ])
                graph['color'] = card.graph.color
                graph['ontology_id'] = str(graph['ontology_id'])
                graphs.append(graph)
        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
示例#2
0
    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
        """
        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
                # graphs.append(Graph.objects.get(pk=card.graph_id))
                proxy_graph = Graph.objects.get(pk=card.graph_id)
                color = proxy_graph.root.config['fillColor']
                graph = card.graph
                graph = JSONSerializer().serializeToPython(graph, exclude=['functions','disable_instance_creation','deploymentdate','deploymentfile'])
                graph['color'] = color
                graphs.append(graph)
        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
示例#3
0
    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
        """
        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'] = models.CardXNodeXWidget.objects.filter(
                    card__graph=graph).distinct()
                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
示例#4
0
    def serialize_for_mobile(self):
        """
        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()
        expired = (datetime.strptime(str(self.enddate), '%Y-%m-%d') - datetime.now() + timedelta(hours=24)).days < 0
        ret = JSONSerializer().serializeToPython(obj)
        if expired:
            self.active = False
            super(MobileSurvey, self).save()
            ret['active'] = False
        graphs = []
        card_lookup = {}
        for card in self.cards.all():
            if str(card.graph_id) in card_lookup:
                card_lookup[str(card.graph_id)].append(card)
            else:
                card_lookup[str(card.graph_id)] = [card]
        for graphid, cards in iter(card_lookup.items()):
            graph = Graph.objects.get(pk=graphid)
            graph_obj = graph.serialize(exclude=['domain_connections', 'edges', 'relatable_resource_model_ids'])
            graph_obj['widgets'] = list(models.CardXNodeXWidget.objects.filter(card__graph=graph).distinct())
            nodegroupids = []
            for card in cards:
                topcard = Card.objects.get(pk=card.cardid)
                self.collect_card_widget_node_data(graph_obj, graph, topcard, nodegroupids)
            graph_obj['widgets'] = serializer.serializeToPython(graph_obj['widgets'])

            nodegroup_filters = {'nodes': 'nodegroup_id', 'cards': 'nodegroup_id', 'nodegroups': 'nodegroupid'}

            for prop, id in iter(nodegroup_filters.items()):
                relevant_items = [item for item in graph_obj[prop] if item[id] in nodegroupids]
                graph_obj[prop] = relevant_items

            relevant_cardids = [card['cardid'] for card in graph_obj['cards']]
            relevant_widgets = [widget for widget in graph_obj['widgets'] if str(widget['card_id']) in relevant_cardids]
            graph_obj['widgets'] = relevant_widgets

            graphs.append(serializer.serializeToPython(graph_obj))

        ret['graphs'] = graphs
        ret['cards'] = ordered_cards
        try:
            bounds = json.loads(ret['bounds'])
            ret['bounds'] = bounds
            if (bounds['type'] == 'MultiPolygon'):
                singlepart = GeoUtils().convert_multipart_to_singlepart(bounds)
                ret['bounds'] = singlepart
        except TypeError as e:
            print 'Could not parse', ret['bounds'], e
        return ret
示例#5
0
 def serialize(self):
     """
     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
     """
     serializer = JSONSerializer()
     serializer.geom_format = 'geojson'
     obj = serializer.handle_model(self)
     ordered_cards = self.get_ordered_cards()
     ret = JSONSerializer().serializeToPython(obj)
     ret['cards'] = ordered_cards
     return ret
示例#6
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)
     ret["cards"] = ordered_cards
     try:
         bounds = json.loads(ret["bounds"])
         ret["bounds"] = bounds
         if bounds["type"] == "MultiPolygon":
             singlepart = GeoUtils().convert_multipart_to_singlepart(bounds)
             ret["bounds"] = singlepart
     except TypeError as e:
         print("Could not parse", ret["bounds"], e)
     return ret
示例#7
0
    def serialize_for_mobile(self):
        """
        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()
        expired = False
        try:
            expired = (datetime.strptime(str(self.enddate), "%Y-%m-%d") -
                       datetime.now() + timedelta(hours=24)).days < 0
        except ValueError:
            pass
        ret = JSONSerializer().serializeToPython(obj)
        if expired is True:
            self.active = False
            super(MobileSurvey, self).save()
            ret["active"] = False
        graphs = []
        card_lookup = {}
        for card in self.cards.all():
            if str(card.graph_id) in card_lookup:
                card_lookup[str(card.graph_id)].append(card)
            else:
                card_lookup[str(card.graph_id)] = [card]
        for graphid, cards in iter(list(card_lookup.items())):
            graph = Graph.objects.get(pk=graphid)
            graph_obj = graph.serialize(exclude=[
                "domain_connections", "edges", "relatable_resource_model_ids"
            ])
            graph_obj["widgets"] = list(
                models.CardXNodeXWidget.objects.filter(
                    card__graph=graph).distinct())
            nodegroupids = []
            for card in cards:
                topcard = Card.objects.get(pk=card.cardid)
                self.collect_card_widget_node_data(graph_obj, graph, topcard,
                                                   nodegroupids)
            graph_obj["widgets"] = serializer.serializeToPython(
                graph_obj["widgets"])

            nodegroup_filters = {
                "nodes": "nodegroup_id",
                "cards": "nodegroup_id",
                "nodegroups": "nodegroupid"
            }

            for prop, id in iter(list(nodegroup_filters.items())):
                relevant_items = [
                    item for item in graph_obj[prop]
                    if item[id] in nodegroupids
                ]
                graph_obj[prop] = relevant_items

            relevant_cardids = [card["cardid"] for card in graph_obj["cards"]]
            relevant_widgets = [
                widget for widget in graph_obj["widgets"]
                if str(widget["card_id"]) in relevant_cardids
            ]
            graph_obj["widgets"] = relevant_widgets

            graphs.append(serializer.serializeToPython(graph_obj))

        ret["graphs"] = graphs
        ret["cards"] = ordered_cards
        try:
            bounds = json.loads(ret["bounds"])
            ret["bounds"] = bounds
            if bounds["type"] == "MultiPolygon":
                singlepart = GeoUtils().convert_multipart_to_singlepart(bounds)
                ret["bounds"] = singlepart
        except TypeError as e:
            logger.error("Could not parse {0}, {1}".format(ret["bounds"], e))
        return ret
示例#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