def setUp(cls):
        cls.VALUE_KEY = "@display_value"

        cls.RESOURCE_KEY = "resource"
        cls.DISPLAY_DESCRIPTION_KEY = "displaydescription"
        cls.DISPLAY_NAME_KEY = "displayname"
        cls.GRAPH_ID_KEY = "graph_id"
        cls.LEGACY_ID_KEY = "legacyid"
        cls.MAP_POPUP_KEY = "map_popup"
        cls.RESOURCE_INSTANCE_ID_KEY = "resourceinstanceid"

        cls.nodegroup = models.NodeGroup()
        cls.grouping_node = models.Node(datatype="semantic",
                                        name="Test Node Grouping",
                                        nodegroup=cls.nodegroup)
        cls.string_node = models.Node(datatype="string", name="Test Node")
        cls.grouping_tile = models.TileModel(data={},
                                             nodegroup_id=str(
                                                 cls.grouping_node.pk))
        cls.string_tile = models.TileModel(
            data={str(cls.string_node.pk): "value_1"},
            nodegroup_id=str(cls.string_node.pk))

        cls.grouping_node.nodegroupid = cls.grouping_node.nodeid
        cls.string_node.nodegroupid = cls.string_node.nodeid

        # let's mock Resource since it's minimally used
        # and complex to get `displayname`
        cls.test_resource = mock.Mock(displayname="Test Resource", tiles=[])
    def setUp(cls):
        cls.grouping_node = models.Node(datatype="semantic", name="Test Node Grouping")
        cls.string_node = models.Node(datatype="string", name="Test Node")

        cls.grouping_tile = models.TileModel(data={}, nodegroup_id=str(cls.grouping_node.pk))
        cls.string_tile = models.TileModel(data={str(cls.string_node.pk): "value_1"}, nodegroup_id=str(cls.string_node.pk))

        # le'ts mock Resource since it's minimally used
        # and complex to get `displayname`
        cls.test_resource = mock.Mock(displayname="Test Resource", tiles=[])
    def test_handles_node_with_multiple_values(self, mock_Node):
        mock_Node.objects.get.return_value = self.string_node

        duplicate_node_tile = models.TileModel(data={str(self.string_node.pk): "value_2"}, nodegroup_id=str(self.string_node.pk))

        self.test_resource.tiles.append(self.string_tile)
        self.test_resource.tiles.append(duplicate_node_tile)

        label_based_graph = LabelBasedGraph.from_resource(resource=self.test_resource, compact=False, hide_empty_nodes=False)

        self.assertEqual(
            label_based_graph,
            {
                self.string_node.name: [
                    {
                        NODE_ID_KEY: str(self.string_node.pk),
                        TILE_ID_KEY: str(self.string_tile.pk),
                        VALUE_KEY: self.string_tile.data[str(self.string_node.pk)],
                    },
                    {
                        NODE_ID_KEY: str(self.string_node.pk),
                        TILE_ID_KEY: str(duplicate_node_tile.pk),
                        VALUE_KEY: duplicate_node_tile.data[str(self.string_node.pk)],
                    },
                ],
            },
        )
    def test_handles_node_with_multiple_values(self, mock_Node,
                                               mock_NodeGroup):
        mock_Node.objects.get.return_value = self.string_node
        mock_NodeGroup.objects.filter.return_value.values.return_value = [{
            "nodegroupid":
            self.string_tile.nodegroup_id,
            "cardinality":
            "1"
        }]

        duplicate_node_tile = models.TileModel(
            data={str(self.string_node.pk): "value_2"},
            nodegroup_id=str(self.string_node.pk))

        self.test_resource.tiles.append(self.string_tile)
        self.test_resource.tiles.append(duplicate_node_tile)

        label_based_graph = LabelBasedGraph.from_resource(
            resource=self.test_resource, compact=False, hide_empty_nodes=False)

        self.assertEqual(
            label_based_graph,
            {
                self.RESOURCE_KEY: {
                    self.string_node.name: [
                        {
                            NODE_ID_KEY:
                            str(self.string_node.pk),
                            TILE_ID_KEY:
                            str(self.string_tile.pk),
                            self.VALUE_KEY:
                            self.string_tile.data[str(self.string_node.pk)],
                        },
                        {
                            NODE_ID_KEY:
                            str(self.string_node.pk),
                            TILE_ID_KEY:
                            str(duplicate_node_tile.pk),
                            self.VALUE_KEY:
                            duplicate_node_tile.data[str(self.string_node.pk)],
                        },
                    ],
                },
                self.DISPLAY_DESCRIPTION_KEY: mock.ANY,
                self.DISPLAY_NAME_KEY: mock.ANY,
                self.GRAPH_ID_KEY: mock.ANY,
                self.LEGACY_ID_KEY: mock.ANY,
                self.MAP_POPUP_KEY: mock.ANY,
                self.RESOURCE_INSTANCE_ID_KEY: mock.ANY,
            },
        )
 def update_tile_name_node(tile, name_nodegroupid, name_nodeid,
                           related_nodeid):
     # set tile for name using descriptor for rel resource
     related_resource_tile = models.TileModel.objects.get(
         resourceinstance=tile.resourceinstance,
         nodegroup_id=related_nodeid)
     if len(related_resource_tile.data[related_nodeid]) > 0:
         related_resource_ref = related_resource_tile.data[
             related_nodeid][0]
         related_resource = Resource.objects.get(
             resourceinstanceid=related_resource_ref["resourceId"])
         # create a new tile on the original resourceinstance concat "graph.name + 'of' + related_resource_name"
         new_tile = models.TileModel()
         new_tile.nodegroup = models.NodeGroup.objects.get(
             nodegroupid=name_nodegroupid)
         new_tile.resourceinstance = tile.resourceinstance
         name = tile.resourceinstance.graph.name + " of " + related_resource.displayname
         new_tile.data = {name_nodeid: name}
         new_tile_tileid = new_tile.tileid
         new_tile.save()
Beispiel #6
0
    def save(self, tile, request):
        tile_edits = json.loads(request.POST.get('data'))['data']
        if request:
            address = {
                "geometry": {
                    "spatialReference": {
                        "wkid": 102100,
                        "latestWkid": 3857
                    }
                },
                "attributes": {
                    "EAS_BaseID": None,
                    "EAS_SubID": None,
                    "CNN": None,
                    "Address": None,
                    "Address_Number": None,
                    "Address_Number_Suffix": None,
                    "Street_Name": None,
                    "Street_Type": None,
                    "Unit_Number": None,
                    "Zipcode": None,
                    "Block_Lot": None,
                    "Longitude": None,
                    "Latitude": None,
                    "Location": None
                }
            }

            payload = {
                "adds":[],
                "updates":[],
                "deletes":'',
                "attachments":[],
                "rollbackOnFailure": False,
                "useGlobalIds": False,
                "f": "pjson",
                "token": "tTzVkJ7RPpZmqmlxc7xVBaORWK8vIKQenSkbmK13OnDfIHNKaNCIaH3i6Nz1AUbdnqkEsz8HuA-QqYrndP4yyqgov0NUgabK3lOO19erL-YYPtbIhEzahbSeQ0wPkJx1TH7RVL-gJ9m3iBsV9Affr0NczrLunSdj6rsa1Kg4QI8fTWpdgj0VCy7FaANWggjI6b7kDATtb43W9-hHxmndcjEU9S7lBzCfTty1b4GnAF3dmYhoh4ZBLC-XpsLetKEJ"
            }

            field_lookup = {
                "29862afe-4746-11e8-88b1-0242ac120006": "Block_Lot",
                "1a08f610-4746-11e8-b7cc-0242ac120006": "Street Name",
                "1a08fbd8-4746-11e8-b7cc-0242ac120006": "Address_Number",
                "1a08f3cc-4746-11e8-b7cc-0242ac120006": "Address_Number_Suffix",
                "1a08f80e-4746-11e8-b7cc-0242ac120006": "CNN",
            }

            geometry_node = '2ad20702-4746-11e8-a9a0-0242ac120006'


            result_node = models.Node.objects.get(pk='1a08f3cc-4746-11e8-b7cc-0242ac120006')
            external_reference = Tile.objects.filter(nodegroup=result_node.nodegroup).filter(resourceinstance=tile.resourceinstance_id)
            tiles = Tile.objects.filter(resourceinstance=tile.resourceinstance_id)

            has_geom = False

            for tile in tiles:
                for tile_node, tile_value in tile.data.iteritems():
                    # if models.Node.objects.get(pk=tile_node).datatype == 'geojson-feature-collection':
                    if tile_node == geometry_node:
                        geom = GEOSGeometry(json.dumps(tile_value['features'][0]['geometry']), srid=4326)
                        geom.transform(3857)
                        address['geometry']['x'] = geom.x
                        address['geometry']['y'] = geom.y
                        has_geom = True
                    if tile_node in field_lookup:
                        address["attributes"][field_lookup[tile_node]] = str(tile_value)

            for edit_node, edit_value in tile_edits.iteritems():
                if edit_node == geometry_node:
                    geom = GEOSGeometry(json.dumps(edit_value['features'][0]['geometry']), srid=4326)
                    geom.transform(3857)
                    address['geometry']['x'] = geom.x
                    address['geometry']['y'] = geom.y
                    has_geom = True
                if edit_node in field_lookup:
                    address["attributes"][field_lookup[edit_node]] = str(edit_value)

            if has_geom:
                if len(external_reference) != 0:
                    address["attributes"]["FID"] = int(external_reference[0].data["1a08f3cc-4746-11e8-b7cc-0242ac120006"])
                    payload["updates"].append(address)
                else:
                    payload["adds"].append(address)
                data = urllib.urlencode(payload).replace('None', 'null')
                url = self.config['external_address_url'] + '/applyEdits'
                req = urllib2.Request(url, data)
                f = urllib2.urlopen(req)
                response = f.read()
                response = json.loads(response)
                pp(payload)
                pp(response)
                if len(response['addResults']) > 0:
                    if response['addResults'][0]['success'] == True:
                        result_tile = models.TileModel()
                        result_tile.resourceinstance = models.ResourceInstance.objects.get(pk=tile.resourceinstance_id)
                        result_tile.data = {"1a08f3cc-4746-11e8-b7cc-0242ac120006": str(response['addResults'][0]['objectId'])}
                        result_tile.nodegroup = result_node.nodegroup
                        result_tile.save()
                f.close()

        return tile