Esempio n. 1
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
Esempio n. 2
0
    def get(self, request):
        mobile_survey_models = models.MobileSurveyModel.objects.order_by("name")
        mobile_surveys = []
        serializer = JSONSerializer()
        for survey in mobile_survey_models:
            survey.deactivate_expired_survey()
            serialized_survey = serializer.serializeToPython(survey)
            serialized_survey["edited_by"] = {
                "username": survey.lasteditedby.username,
                "first": survey.lasteditedby.first_name,
                "last": survey.lasteditedby.last_name,
                "id": survey.lasteditedby.id,
            }
            serialized_survey["created_by"] = created_by = {
                "username": survey.createdby.username,
                "first": survey.createdby.first_name,
                "last": survey.createdby.last_name,
                "id": survey.createdby.id,
            }
            mobile_surveys.append(serialized_survey)

        context = self.get_context_data(
            mobile_surveys=serializer.serialize(mobile_surveys, sort_keys=False), main_script="views/mobile-survey-manager"
        )

        context["nav"]["title"] = _("Arches Collector Manager")
        context["nav"]["icon"] = "fa-globe"
        context["nav"]["help"] = {"title": _("Arches Collector Manager"), "template": "arches-collector-manager-help"}

        return render(request, "views/mobile-survey-manager.htm", context)
Esempio n. 3
0
    def get(self, request):
        mobile_survey_models = models.MobileSurveyModel.objects.order_by(
            'name')
        mobile_surveys = []
        serializer = JSONSerializer()
        for survey in mobile_survey_models:
            expired = deactivate_expired_survey(survey)
            serialized_survey = serializer.serializeToPython(survey)
            serialized_survey['expired'] = expired
            if expired is True:
                serialized_survey['active'] = False
            serialized_survey['edited_by'] = {
                'username': survey.lasteditedby.username,
                'first': survey.lasteditedby.first_name,
                'last': survey.lasteditedby.last_name,
                'id': survey.lasteditedby.id
            }
            serialized_survey['created_by'] = created_by = {
                'username': survey.createdby.username,
                'first': survey.createdby.first_name,
                'last': survey.createdby.last_name,
                'id': survey.createdby.id
            }
            mobile_surveys.append(serialized_survey)

        context = self.get_context_data(
            mobile_surveys=serializer.serialize(mobile_surveys,
                                                sort_keys=False),
            main_script='views/mobile-survey-manager',
        )

        context['nav']['title'] = _('Mobile Survey Manager')
        context['nav']['icon'] = 'fa-server'
        context['nav']['help'] = {
            'title': _('Mobile Survey Manager'),
            'template': 'mobile-survey-manager-help',
        }

        return render(request, 'views/mobile-survey-manager.htm', context)
Esempio n. 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 = 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