Exemplo n.º 1
0
 def get_scheme_id(self):
     se = SearchEngineFactory().create()
     result = se.search(index='concept_labels', id=self.id)
     if result['found']:
         return Concept(result['_type'])
     else:
         return None
Exemplo n.º 2
0
    def get_related_resources(self, lang='en-US', limit=1000, start=0):
        """
        Returns an object that lists the related resources, the relationship types, and a reference to the current resource

        """

        ret = {
            'resource_instance': self,
            'resource_relationships': [],
            'related_resources': []
        }
        se = SearchEngineFactory().create()
        query = Query(se, limit=limit, start=start)
        bool_filter = Bool()
        bool_filter.should(Terms(field='resourceinstanceidfrom', terms=self.resourceinstanceid))
        bool_filter.should(Terms(field='resourceinstanceidto', terms=self.resourceinstanceid))
        query.add_query(bool_filter)
        resource_relations = query.search(index='resource_relations', doc_type='all')
        ret['total'] = resource_relations['hits']['total']
        instanceids = set()
        for relation in resource_relations['hits']['hits']:
            relation['_source']['preflabel'] = get_preflabel_from_valueid(relation['_source']['relationshiptype'], lang)
            ret['resource_relationships'].append(relation['_source'])
            instanceids.add(relation['_source']['resourceinstanceidto'])
            instanceids.add(relation['_source']['resourceinstanceidfrom'])
        if len(instanceids) > 0:
            instanceids.remove(str(self.resourceinstanceid))

        related_resources = se.search(index='resource', doc_type='_all', id=list(instanceids))
        if related_resources:
            for resource in related_resources['docs']:
                ret['related_resources'].append(resource['_source'])

        return ret
Exemplo n.º 3
0
 def get_scheme_id(self):
     se = SearchEngineFactory().create()
     result = se.search(index='strings', doc_type='concept', id=self.id)
     if result['found']:
         return Concept(result['top_concept'])
     else:
         return None
Exemplo n.º 4
0
    def get(self, request, resourceid=None):
        if Resource.objects.filter(pk=resourceid).exists():
            try:
                resource = Resource.objects.get(pk=resourceid)
                se = SearchEngineFactory().create()
                document = se.search(index='resources', id=resourceid)
                return JSONResponse({
                    'graphid':
                    document['_source']['graph_id'],
                    'graph_name':
                    resource.graph.name,
                    'displaydescription':
                    document['_source']['displaydescription'],
                    'map_popup':
                    document['_source']['map_popup'],
                    'displayname':
                    document['_source']['displayname'],
                    'geometries':
                    document['_source']['geometries'],
                })
            except Exception as e:
                logger.exception(
                    _('Failed to fetch resource instance descriptors'))

        return HttpResponseNotFound()
Exemplo n.º 5
0
 def get_scheme_id(self):
     se = SearchEngineFactory().create()
     result = se.search(index='strings', doc_type='concept', id=self.id)
     if result['found']:
         return Concept(result['top_concept'])
     else:
         return None
Exemplo n.º 6
0
    def get(self, request, resourceid=None):
        if Resource.objects.filter(pk=resourceid).exclude(
                pk=settings.SYSTEM_SETTINGS_RESOURCE_ID).exists():
            try:
                resource = Resource.objects.get(pk=resourceid)
                se = SearchEngineFactory().create()
                document = se.search(index="resources", id=resourceid)
                return JSONResponse({
                    "graphid":
                    document["_source"]["graph_id"],
                    "graph_name":
                    resource.graph.name,
                    "displaydescription":
                    document["_source"]["displaydescription"],
                    "map_popup":
                    document["_source"]["map_popup"],
                    "displayname":
                    document["_source"]["displayname"],
                    "geometries":
                    document["_source"]["geometries"],
                })
            except Exception as e:
                logger.exception(
                    _("Failed to fetch resource instance descriptors"))

        return HttpResponseNotFound()
Exemplo n.º 7
0
def get_related_resources(resourceid, lang, limit=1000, start=0):
    ret = {
        'resource_relationships': [],
        'related_resources': []
    }
    se = SearchEngineFactory().create()

    query = Query(se, limit=limit, start=start)
    query.add_filter(Terms(field='entityid1', terms=resourceid).dsl, operator='or')
    query.add_filter(Terms(field='entityid2', terms=resourceid).dsl, operator='or')
    resource_relations = query.search(index='resource_relations', doc_type='all')
    ret['total'] = resource_relations['hits']['total']

    entityids = set()
    for relation in resource_relations['hits']['hits']:
        relation['_source']['preflabel'] = get_preflabel_from_valueid(relation['_source']['relationshiptype'], lang)
        ret['resource_relationships'].append(relation['_source'])
        entityids.add(relation['_source']['entityid1'])
        entityids.add(relation['_source']['entityid2'])
    if len(entityids) > 0:
        entityids.remove(resourceid)   

    related_resources = se.search(index='entity', doc_type='_all', id=list(entityids))
    if related_resources:
        for resource in related_resources['docs']:
            ret['related_resources'].append(resource['_source'])

    return ret
Exemplo n.º 8
0
def get_related_resources(resourceid, lang, limit=1000, start=0):
    ret = {'resource_relationships': [], 'related_resources': []}
    se = SearchEngineFactory().create()

    query = Query(se, limit=limit, start=start)
    query.add_filter(Terms(field='entityid1', terms=resourceid).dsl,
                     operator='or')
    query.add_filter(Terms(field='entityid2', terms=resourceid).dsl,
                     operator='or')
    resource_relations = query.search(index='resource_relations',
                                      doc_type='all')
    ret['total'] = resource_relations['hits']['total']

    entityids = set()
    for relation in resource_relations['hits']['hits']:
        relation['_source']['preflabel'] = get_preflabel_from_valueid(
            relation['_source']['relationshiptype'], lang)
        ret['resource_relationships'].append(relation['_source'])
        entityids.add(relation['_source']['entityid1'])
        entityids.add(relation['_source']['entityid2'])
    if len(entityids) > 0:
        entityids.remove(resourceid)

    related_resources = se.search(index='entity',
                                  doc_type='_all',
                                  id=list(entityids))
    if related_resources:
        for resource in related_resources['docs']:
            ret['related_resources'].append(resource['_source'])

    return ret
Exemplo n.º 9
0
def get_preflabel_from_valueid(valueid, lang):

    se = SearchEngineFactory().create()
    concept_label = se.search(index='concept_labels', id=valueid)
    if concept_label['found']:
#         print "ConceptID from ValueID: %s" % get_concept_label_from_valueid(valueid)
        return get_preflabel_from_conceptid(get_concept_label_from_valueid(valueid)['conceptid'], lang)
Exemplo n.º 10
0
 def get_scheme_id(self):
     se = SearchEngineFactory().create()
     result = se.search(index='concept_labels', id=self.id)
     if result['found']:
         return Concept(result['_type'])
     else:
         return None
Exemplo n.º 11
0
def get_preflabel_from_valueid(valueid, lang):

    se = SearchEngineFactory().create()
    concept_label = se.search(index='concept_labels', id=valueid)
    if concept_label['found']:
        #         print "ConceptID from ValueID: %s" % get_concept_label_from_valueid(valueid)
        return get_preflabel_from_conceptid(
            get_concept_label_from_valueid(valueid)['conceptid'], lang)
Exemplo n.º 12
0
    def get_related_resources(self, lang='en-US', limit=settings.RELATED_RESOURCES_EXPORT_LIMIT, start=0, page=0):
        """
        Returns an object that lists the related resources, the relationship types, and a reference to the current resource

        """
        graphs = models.GraphModel.objects.all().exclude(
            pk=settings.SYSTEM_SETTINGS_RESOURCE_MODEL_ID).exclude(isresource=False)
        graph_lookup = {str(graph.graphid): {
            'name': graph.name, 'iconclass': graph.iconclass, 'fillColor': graph.color} for graph in graphs}
        ret = {
            'resource_instance': self,
            'resource_relationships': [],
            'related_resources': [],
            'node_config_lookup': graph_lookup
        }
        se = SearchEngineFactory().create()

        if page > 0:
            limit = settings.RELATED_RESOURCES_PER_PAGE
            start = limit*int(page-1)

        def get_relations(resourceinstanceid, start, limit):
            query = Query(se, start=start, limit=limit)
            bool_filter = Bool()
            bool_filter.should(
                Terms(field='resourceinstanceidfrom', terms=resourceinstanceid))
            bool_filter.should(
                Terms(field='resourceinstanceidto', terms=resourceinstanceid))
            query.add_query(bool_filter)
            return query.search(index='resource_relations')

        resource_relations = get_relations(
            self.resourceinstanceid, start, limit)
        ret['total'] = resource_relations['hits']['total']
        instanceids = set()

        for relation in resource_relations['hits']['hits']:
            try:
                preflabel = get_preflabel_from_valueid(
                    relation['_source']['relationshiptype'], lang)
                relation['_source']['relationshiptype_label'] = preflabel['value']
            except:
                relation['_source']['relationshiptype_label'] = relation['_source']['relationshiptype']

            ret['resource_relationships'].append(relation['_source'])
            instanceids.add(relation['_source']['resourceinstanceidto'])
            instanceids.add(relation['_source']['resourceinstanceidfrom'])
        if len(instanceids) > 0:
            instanceids.remove(str(self.resourceinstanceid))

        if len(instanceids) > 0:
            related_resources = se.search(index='resources', id=list(instanceids))
            if related_resources:
                for resource in related_resources['docs']:
                    relations = get_relations(resource['_id'], 0, 0)
                    resource['_source']['total_relations'] = relations['hits']['total']
                    ret['related_resources'].append(resource['_source'])
        return ret
Exemplo n.º 13
0
 def get_resource_names(self, nodevalue):
     resource_names = set([])
     se = SearchEngineFactory().create()
     id_list = self.get_id_list(nodevalue)
     for resourceid in id_list:
         print resourceid
         resource_document = se.search(index='resource', doc_type='_all', id=resourceid)
         resource_names.add(resource_document['_source']['displayname'])
     return resource_names
Exemplo n.º 14
0
    def get_related_resources(
        self, lang="en-US", limit=settings.RELATED_RESOURCES_EXPORT_LIMIT, start=0, page=0,
    ):
        """
        Returns an object that lists the related resources, the relationship types, and a reference to the current resource

        """
        graphs = (
            models.GraphModel.objects.all()
            .exclude(pk=settings.SYSTEM_SETTINGS_RESOURCE_MODEL_ID)
            .exclude(isresource=False)
            .exclude(isactive=False)
        )
        graph_lookup = {
            str(graph.graphid): {"name": graph.name, "iconclass": graph.iconclass, "fillColor": graph.color} for graph in graphs
        }
        ret = {"resource_instance": self, "resource_relationships": [], "related_resources": [], "node_config_lookup": graph_lookup}
        se = SearchEngineFactory().create()

        if page > 0:
            limit = settings.RELATED_RESOURCES_PER_PAGE
            start = limit * int(page - 1)

        def get_relations(resourceinstanceid, start, limit):
            query = Query(se, start=start, limit=limit)
            bool_filter = Bool()
            bool_filter.should(Terms(field="resourceinstanceidfrom", terms=resourceinstanceid))
            bool_filter.should(Terms(field="resourceinstanceidto", terms=resourceinstanceid))
            query.add_query(bool_filter)
            return query.search(index="resource_relations")

        resource_relations = get_relations(self.resourceinstanceid, start, limit)
        ret["total"] = resource_relations["hits"]["total"]
        instanceids = set()

        for relation in resource_relations["hits"]["hits"]:
            try:
                preflabel = get_preflabel_from_valueid(relation["_source"]["relationshiptype"], lang)
                relation["_source"]["relationshiptype_label"] = preflabel["value"]
            except:
                relation["_source"]["relationshiptype_label"] = relation["_source"]["relationshiptype"]

            ret["resource_relationships"].append(relation["_source"])
            instanceids.add(relation["_source"]["resourceinstanceidto"])
            instanceids.add(relation["_source"]["resourceinstanceidfrom"])
        if len(instanceids) > 0:
            instanceids.remove(str(self.resourceinstanceid))

        if len(instanceids) > 0:
            related_resources = se.search(index="resources", id=list(instanceids))
            if related_resources:
                for resource in related_resources["docs"]:
                    relations = get_relations(resource["_id"], 0, 0)
                    resource["_source"]["total_relations"] = relations["hits"]["total"]
                    ret["related_resources"].append(resource["_source"])
        return ret
Exemplo n.º 15
0
 def get_resource_names(self, nodevalue):
     resource_names = set([])
     es = Elasticsearch()
     se = SearchEngineFactory().create()
     id_list = self.get_id_list(nodevalue)
     for resourceid in id_list:
         print resourceid
         resource_document = se.search(index='resource', doc_type='_all', id=resourceid)
         resource_names.add(resource_document['_source']['displayname'])
     return resource_names
Exemplo n.º 16
0
    def get_related_resources(self, lang='en-US', limit=1000, start=0):
        """
        Returns an object that lists the related resources, the relationship types, and a reference to the current resource

        """

        ret = {
            'resource_instance': self,
            'resource_relationships': [],
            'related_resources': []
        }
        se = SearchEngineFactory().create()

        def get_relations(resourceinstanceid, start, limit):
            query = Query(se, limit=limit, start=start)
            bool_filter = Bool()
            bool_filter.should(
                Terms(field='resourceinstanceidfrom',
                      terms=resourceinstanceid))
            bool_filter.should(
                Terms(field='resourceinstanceidto', terms=resourceinstanceid))
            query.add_query(bool_filter)
            return query.search(index='resource_relations', doc_type='all')

        resource_relations = get_relations(self.resourceinstanceid, start,
                                           limit)
        ret['total'] = resource_relations['hits']['total']
        instanceids = set()

        for relation in resource_relations['hits']['hits']:
            try:
                preflabel = get_preflabel_from_valueid(
                    relation['_source']['relationshiptype'], lang)
                relation['_source']['relationshiptype_label'] = preflabel[
                    'value']
            except:
                relation['_source']['relationshiptype_label'] = relation[
                    '_source']['relationshiptype']

            ret['resource_relationships'].append(relation['_source'])
            instanceids.add(relation['_source']['resourceinstanceidto'])
            instanceids.add(relation['_source']['resourceinstanceidfrom'])
        if len(instanceids) > 0:
            instanceids.remove(str(self.resourceinstanceid))

        related_resources = se.search(index='resource',
                                      doc_type='_all',
                                      id=list(instanceids))
        if related_resources:
            for resource in related_resources['docs']:
                relations = get_relations(resource['_id'], 0, 0)
                resource['_source']['total_relations'] = relations['hits'][
                    'total']
                ret['related_resources'].append(resource['_source'])
        return ret
Exemplo n.º 17
0
def get_related_resources(resourceid, lang, limit=1000, start=0, allowedtypes=[], is_anon=False):

    ret = {
        'resource_relationships': [],
        'related_resources': []
    }
    se = SearchEngineFactory().create()

    query = Query(se, limit=limit, start=start)
    query.add_filter(Terms(field='entityid1', terms=resourceid).dsl, operator='or')
    query.add_filter(Terms(field='entityid2', terms=resourceid).dsl, operator='or')
    resource_relations = query.search(index='resource_relations', doc_type="all")

    entityids = set()
    for relation in resource_relations['hits']['hits']:
        relation['_source']['preflabel'] = get_preflabel_from_valueid(relation['_source']['relationshiptype'], lang)
        ret['resource_relationships'].append(relation['_source'])
        entityids.add(relation['_source']['entityid1'])
        entityids.add(relation['_source']['entityid2'])
    if len(entityids) > 0:
        entityids.remove(resourceid)

    # can't figure why passing allowed types to doc_type param doesn't work,
    # so filter is carried out later
    related_resources = se.search(index='entity', doc_type='_all', id=list(entityids))

    filtered_ids = []
    if related_resources:
        for resource in related_resources['docs']:
            if not resource['_type'] in allowedtypes:
                filtered_ids.append(resource['_source']['entityid'])
                continue
            
            if is_anon:
                # filter out protected resources if user is anonymous
                # (this is basically a subset of the get_protected_entityids below
                # they should be combined probably)
                from search import get_protection_conceptids
                protect_id = get_protection_conceptids(settings.PROTECTION_LEVEL_NODE)
                conceptids = [d['conceptid'] for d in resource['_source']['domains']]
                if protect_id in conceptids:
                    filtered_ids.append(resource['_source']['entityid'])
                    continue
            ret['related_resources'].append(resource['_source'])
    
    if len(filtered_ids) > 0:
        # remove all relationships in ret that match a filtered id (this lc is yuge but I think concise)
        filtered_relationships = [rel for rel in ret['resource_relationships'] if not rel['entityid1'] in filtered_ids and not rel['entityid2'] in filtered_ids]
        
        # update ret values
        ret['resource_relationships'] = filtered_relationships
        
    ret['total'] = len(ret['resource_relationships'])
    
    return ret
Exemplo n.º 18
0
def map_layers(request, entitytypeid='all', get_centroids=False):
    data = []
    geom_param = request.GET.get('geom', None)

    bbox = request.GET.get('bbox', '')
    limit = request.GET.get('limit', settings.MAP_LAYER_FEATURE_LIMIT)
    entityids = request.GET.get('entityid', '')
    geojson_collection = {"type": "FeatureCollection", "features": []}

    se = SearchEngineFactory().create()
    query = Query(se, limit=limit)

    args = {'index': 'maplayers'}
    if entitytypeid != 'all':
        args['doc_type'] = entitytypeid
    if entityids != '':
        for entityid in entityids.split(','):
            geojson_collection['features'].append(
                se.search(index='maplayers', id=entityid)['_source'])
        return JSONResponse(geojson_collection)

    data = query.search(**args)
    if not data:
        return JSONResponse({})
    for item in data['hits']['hits']:
        # Ce uporabnik ni avtenticiran, prikazemo le veljavne (to je verjetno potrebno se dodelati (mogoce da vidijo le svoje???)!!!)
        if (not request.user.username != 'anonymous'):
            if (item['_source']['properties']['ewstatus'] !=
                    settings.PUBLISHED_LABEL):
                continue
        if get_centroids:
            item['_source']['geometry'] = item['_source']['properties'][
                'centroid']
            #item['_source'].pop('properties', None)
            item['_source']['properties'].pop('extent', None)
            item['_source']['properties'].pop('elements', None)
            item['_source']['properties'].pop('entitytypeid', None)
            item['_source']['properties'].pop('constructions', None)
            item['_source']['properties'].pop('centroid', None)
            item['_source']['properties'].pop('ewstatus', None)
            item['_source']['properties'].pop('address', None)
            item['_source']['properties'].pop('designations', None)
            item['_source']['properties'].pop('primaryname', None)
            item['_source']['properties'].pop('resource_type', None)
        elif geom_param != None:
            item['_source']['geometry'] = item['_source']['properties'][
                geom_param]
            item['_source']['properties'].pop('extent', None)
            item['_source']['properties'].pop(geom_param, None)
        else:
            item['_source']['properties'].pop('extent', None)
            item['_source']['properties'].pop('centroid', None)
        geojson_collection['features'].append(item['_source'])
    return JSONResponse(geojson_collection)
Exemplo n.º 19
0
def map_layers(request, entitytypeid='all', get_centroids=False):
    data = []
    geom_param = request.GET.get('geom', None)

    bbox = request.GET.get('bbox', '')
    limit = request.GET.get('limit', settings.MAP_LAYER_FEATURE_LIMIT)
    entityids = request.GET.get('entityid', '')
    geojson_collection = {
      "type": "FeatureCollection",
      "features": []
    }
    
    se = SearchEngineFactory().create()
    query = Query(se, limit=limit)

    args = { 'index': 'maplayers' }
    if entitytypeid != 'all':
        args['doc_type'] = entitytypeid
    if entityids != '':
        for entityid in entityids.split(','):
            geojson_collection['features'].append(se.search(index='maplayers', id=entityid)['_source'])
        return JSONResponse(geojson_collection)

    data = query.search(**args)
    if not data:
        return JSONResponse({})
    for item in data['hits']['hits']:
        # Ce uporabnik ni avtenticiran, prikazemo le veljavne (to je verjetno potrebno se dodelati (mogoce da vidijo le svoje???)!!!)
        if (not request.user.username != 'anonymous'):
            if (item['_source']['properties']['ewstatus'] != settings.PUBLISHED_LABEL):
                continue
        if get_centroids:
            item['_source']['geometry'] = item['_source']['properties']['centroid']
            #item['_source'].pop('properties', None)
            item['_source']['properties'].pop('extent', None)
            item['_source']['properties'].pop('elements', None)
            item['_source']['properties'].pop('entitytypeid', None)
            item['_source']['properties'].pop('constructions', None)
            item['_source']['properties'].pop('centroid', None)
            item['_source']['properties'].pop('ewstatus', None)
            item['_source']['properties'].pop('address', None)
            item['_source']['properties'].pop('designations', None)
            item['_source']['properties'].pop('primaryname', None)
            item['_source']['properties'].pop('resource_type', None)
        elif geom_param != None:
            item['_source']['geometry'] = item['_source']['properties'][geom_param]
            item['_source']['properties'].pop('extent', None)
            item['_source']['properties'].pop(geom_param, None)
        else:
            item['_source']['properties'].pop('extent', None)
            item['_source']['properties'].pop('centroid', None)
        geojson_collection['features'].append(item['_source'])
    return JSONResponse(geojson_collection)  
Exemplo n.º 20
0
def map_layers(request, entitytypeid='all', get_centroids=False):
    data = []

    geom_param = request.GET.get('geom', None)

    bbox = request.GET.get('bbox', '')
    limit = request.GET.get('limit', settings.MAP_LAYER_FEATURE_LIMIT)
    entityids = request.GET.get('entityid', '')
    geojson_collection = {"type": "FeatureCollection", "features": []}

    se = SearchEngineFactory().create()
    query = Query(se, limit=limit)

    args = {'index': 'maplayers'}
    if entitytypeid != 'all':
        args['doc_type'] = entitytypeid
    if entityids != '':
        for entityid in entityids.split(','):
            geojson_collection['features'].append(
                se.search(index='maplayers', id=entityid)['_source'])
        return JSONResponse(geojson_collection)

    if get_centroids:
        # If we are just fetching the centroids, we can do a slightly optimised query by having elasticsearch pull out relevant fields
        args['fields'] = [
            'properties.centroid.coordinates', 'type', '_source.id'
        ]
        data = query.search(**args)
        geojson_collection['features'] = [{
            "geometry": {
                "type": "Point",
                "coordinates":
                item['fields']['properties.centroid.coordinates']
            },
            "type": "Feature",
            "id": item['_id']
        } for item in data['hits']['hits']]

    else:
        # We need the full data for each record
        data = query.search(**args)
        for item in data['hits']['hits']:
            if geom_param != None:
                item['_source']['geometry'] = item['_source']['properties'][
                    geom_param]
                item['_source']['properties'].pop('extent', None)
                item['_source']['properties'].pop(geom_param, None)
            else:
                item['_source']['properties'].pop('extent', None)
                item['_source']['properties'].pop('centroid', None)
            geojson_collection['features'].append(item['_source'])

    return JSONResponse(geojson_collection)
Exemplo n.º 21
0
def map_layers(request, entitytypeid='all', get_centroids=False):

    data = []

    geom_param = request.GET.get('geom', None)

    bbox = request.GET.get('bbox', '')
    limit = request.GET.get('limit', settings.MAP_LAYER_FEATURE_LIMIT)
    entityids = request.GET.get('entityid', '')
    geojson_collection = {
      "type": "FeatureCollection",
      "features": []
    }
    
    se = SearchEngineFactory().create()
    query = Query(se, limit=limit)

    args = { 'index': 'maplayers' }
    if entitytypeid != 'all':
        args['doc_type'] = entitytypeid
    if entityids != '':
        for entityid in entityids.split(','):
            geojson_collection['features'].append(se.search(index='maplayers', id=entityid)['_source'])
        return JSONResponse(geojson_collection)

    data = query.search(**args)
    
    # if anonymous user, get list of protected entity ids to be excluded from map
    protected = []
    
    if request.user.username == 'anonymous':
        protected = get_protected_entityids()
        print protected

    for item in data['hits']['hits']:
        if item['_id'] in protected:
            print "hide this one"
            print json.dumps(item,indent=2)
            continue
        if get_centroids:
            item['_source']['geometry'] = item['_source']['properties']['centroid']
            item['_source'].pop('properties', None)
        elif geom_param != None:
            item['_source']['geometry'] = item['_source']['properties'][geom_param]
            item['_source']['properties'].pop('extent', None)
            item['_source']['properties'].pop(geom_param, None)
        else:
            item['_source']['properties'].pop('extent', None)
            item['_source']['properties'].pop('centroid', None)
        geojson_collection['features'].append(item['_source'])

    return JSONResponse(geojson_collection)
Exemplo n.º 22
0
    def get(self, request, resourceid=None):
        if resourceid is not None:
            se = SearchEngineFactory().create()
            document = se.search(index='resource', doc_type='_all', id=resourceid)
            resource = Resource.objects.get(pk=resourceid)
            return JSONResponse({
                'graphid': document['_source']['graph_id'],
                'graph_name': resource.graph.name,
                'displaydescription': document['_source']['displaydescription'],
                'map_popup': document['_source']['map_popup'],
                'displayname': document['_source']['displayname'],
                'geometries': document['_source']['geometries'],
            })

        return HttpResponseNotFound()
Exemplo n.º 23
0
    def get(self, request, resourceid=None):
        if resourceid is not None:
            se = SearchEngineFactory().create()
            document = se.search(index='resource', doc_type='_all', id=resourceid)
            resource = Resource.objects.get(pk=resourceid)
            return JSONResponse({
                'graphid': document['_source']['graph_id'],
                'graph_name': resource.graph.name,
                'displaydescription': document['_source']['displaydescription'],
                'map_popup': document['_source']['map_popup'],
                'displayname': document['_source']['displayname'],
                'geometries': document['_source']['geometries'],
            })

        return HttpResponseNotFound()
Exemplo n.º 24
0
def get_protected_entityids():
    '''returns list of entity ids for protected resources'''
    
    from search import get_protection_conceptids
    protect_id = get_protection_conceptids(settings.PROTECTION_LEVEL_NODE)
    filtered_ids = []
    se = SearchEngineFactory().create()
    
    # for some reason doc_type must be speficied with INFORMATION RESOURCE in order for that type
    # to be queried. right now this is ok, because it's the only type with protection levels,
    # but this is very strange.
    all_resources = se.search(index='entity', doc_type="INFORMATION_RESOURCE.E73")['hits']['hits']
    for resource in all_resources:
        conceptids = [d['conceptid'] for d in resource['_source']['domains']]
        if protect_id in conceptids:
            filtered_ids.append(resource['_source']['entityid'])

    return filtered_ids
Exemplo n.º 25
0
    def get(self, request, resourceid=None):
        if Resource.objects.filter(pk=resourceid).exists():
            try:
                active_cons_node_list = {
                    "Geospatial Location": "8d41e4d6-a250-11e9-accd-00224800b26d",
                    "Name": "8d41e4ab-a250-11e9-87d1-00224800b26d",
                    "Consultation Type": "8d41e4dd-a250-11e9-9032-00224800b26d",
                    "Proposal": "8d41e4bd-a250-11e9-89e8-00224800b26d",
                    "Casework Officer": "8d41e4d4-a250-11e9-a3ff-00224800b26d",
                    "Target Date": "8d41e4cb-a250-11e9-9cf2-00224800b26d",
                    "Consultation Log Date": "8d41e4cf-a250-11e9-a86d-00224800b26d",
                    "Completion Date": "8d41e4cd-a250-11e9-a25b-00224800b26d",
                    "Development Type": "8d41e4cc-a250-11e9-87b3-00224800b26d",
                    "Application Type": "8d41e4d5-a250-11e9-b968-00224800b26d",
                    "Application Area": "8d41e4de-a250-11e9-973b-00224800b26d",
                    "Casework Officer": "8d41e4d4-a250-11e9-a3ff-00224800b26d",
                    "Planning Officer": "8d41e4d7-a250-11e9-83c2-00224800b26d",
                    "Owner": "8d41e4e1-a250-11e9-8d14-00224800b26d",
                    "Applicant": "8d41e4ce-a250-11e9-b83c-00224800b26d",
                    "Agent": "8d41e4d9-a250-11e9-82dc-00224800b26d",
                }
                resource = Resource.objects.get(pk=resourceid)
                se = SearchEngineFactory().create()
                document = se.search(index='resources', id=resourceid, doc_type='_doc')
                datatype_factory = DataTypeFactory()
                additional_data = {}
                if document['_source']['graph_id'] == '8d41e49e-a250-11e9-9eab-00224800b26d':
                    tiles = build_resource_dict([resource], active_cons_node_list, datatype_factory)
                    additional_data = tiles[0]
                ret = {
                    'graphid': document['_source']['graph_id'],
                    'graph_name': resource.graph.name,
                    'displaydescription': document['_source']['displaydescription'],
                    'map_popup': document['_source']['map_popup'],
                    'displayname': document['_source']['displayname'],
                    'geometries': document['_source']['geometries']
                }
                ret.update(additional_data)
                return JSONResponse(ret)
            except Exception as e:
                logger.exception(_('Failed to fetch resource instance descriptors'))

        return HttpResponseNotFound()
Exemplo n.º 26
0
def map_layers(request, entitytypeid='all', get_centroids=False):
    data = []

    geom_param = request.GET.get('geom', None)

    bbox = request.GET.get('bbox', '')
    limit = request.GET.get('limit', settings.MAP_LAYER_FEATURE_LIMIT)
    entityids = request.GET.get('entityid', '')
    geojson_collection = {"type": "FeatureCollection", "features": []}

    se = SearchEngineFactory().create()
    query = Query(se, limit=limit)

    args = {'index': 'maplayers'}
    if entitytypeid != 'all':
        args['doc_type'] = entitytypeid
    if entityids != '':
        for entityid in entityids.split(','):
            geojson_collection['features'].append(
                se.search(index='maplayers', id=entityid)['_source'])
        return JSONResponse(geojson_collection)

    data = query.search(**args)

    for item in data['hits']['hits']:
        if get_centroids:
            item['_source']['geometry'] = item['_source']['properties'][
                'centroid']
            item['_source'].pop('properties', None)
        elif geom_param != None:
            item['_source']['geometry'] = item['_source']['properties'][
                geom_param]
            item['_source']['properties'].pop('extent', None)
            item['_source']['properties'].pop(geom_param, None)
        else:
            item['_source']['properties'].pop('extent', None)
            item['_source']['properties'].pop('centroid', None)
        geojson_collection['features'].append(item['_source'])

    return JSONResponse(geojson_collection)
Exemplo n.º 27
0
def map_layers(request, entitytypeid='all', get_centroids=False):
    data = []

    geom_param = request.GET.get('geom', None)

    bbox = request.GET.get('bbox', '')
    limit = request.GET.get('limit', settings.MAP_LAYER_FEATURE_LIMIT)
    entityids = request.GET.get('entityid', '')
    geojson_collection = {
      "type": "FeatureCollection",
      "features": []
    }
    
    se = SearchEngineFactory().create()
    query = Query(se, limit=limit)

    args = { 'index': 'maplayers' }
    if entitytypeid != 'all':
        args['doc_type'] = entitytypeid
    if entityids != '':
        for entityid in entityids.split(','):
            geojson_collection['features'].append(se.search(index='maplayers', id=entityid)['_source'])
        return JSONResponse(geojson_collection)

    data = query.search(**args)

    for item in data['hits']['hits']:
        if get_centroids:
            item['_source']['geometry'] = item['_source']['properties']['centroid']
            item['_source'].pop('properties', None)
        elif geom_param != None:
            item['_source']['geometry'] = item['_source']['properties'][geom_param]
            item['_source']['properties'].pop('extent', None)
            item['_source']['properties'].pop(geom_param, None)
        else:
            item['_source']['properties'].pop('extent', None)
            item['_source']['properties'].pop('centroid', None)
        geojson_collection['features'].append(item['_source'])

    return JSONResponse(geojson_collection)
Exemplo n.º 28
0
def act_a_layer(request, boundtype=''):

    data = []
    geom_param = request.GET.get('geom', None)

    bbox = request.GET.get('bbox', '')
    limit = request.GET.get('limit', settings.MAP_LAYER_FEATURE_LIMIT)
    geojson_collection = {
      "type": "FeatureCollection",
      "features": []
    }

    se = SearchEngineFactory().create()
    query = Query(se, limit=limit)

    data = se.search(index='resource', doc_type='ACTIVITY_A.E7')
    
    pre_collection = []
    
    for item in data['hits']['hits']:
        if "PLACE_E53" in item['_source']['graph']:
            for geom in item['_source']['graph']['PLACE_E53']:
                
                if geom['SPATIAL_COORDINATES_GEOMETRY_E47'][0]["ACTIVITY_GEOMETRY_TYPE_E55__label"] == "Project Area":
                    wkt = geom['SPATIAL_COORDINATES_GEOMETRY_E47'][0]['SPATIAL_COORDINATES_GEOMETRY_E47__value']
                    g1 = shapely.wkt.loads(wkt)
                    feat = geojson.Feature(geometry=g1, properties={})
                    feat['properties']['type'] = "Project Area"
                    feat['id'] = item['_source']['entityid']
                    geojson_collection['features'].append(feat)
                
                if geom['SPATIAL_COORDINATES_GEOMETRY_E47'][0]["ACTIVITY_GEOMETRY_TYPE_E55__label"] == "Area of Potential Effect":
                    wkt = geom['SPATIAL_COORDINATES_GEOMETRY_E47'][0]['SPATIAL_COORDINATES_GEOMETRY_E47__value']
                    g1 = shapely.wkt.loads(wkt)
                    feat = geojson.Feature(geometry=g1, properties={})
                    feat['properties']['type'] = "Area of Potential Effect"
                    feat['id'] = item['_source']['entityid']
                    geojson_collection['features'].append(feat)

    return JSONResponse(geojson_collection)
Exemplo n.º 29
0
def report(request, resourceid):
    lang = request.GET.get('lang', settings.LANGUAGE_CODE)
    se = SearchEngineFactory().create()
    report_info = se.search(index='resource', id=resourceid)
    report_info['source'] = report_info['_source']
    report_info['type'] = report_info['_type']
    report_info['source']['graph'] = report_info['source']['graph']
    del report_info['_source']
    del report_info['_type']

    def get_evaluation_path(valueid):
        value = models.Values.objects.get(pk=valueid)
        concept_graph = Concept().get(id=value.conceptid_id, include_subconcepts=False, 
            include_parentconcepts=True, include_relatedconcepts=False, up_depth_limit=None, lang=lang)
        
        paths = []
        for path in concept_graph.get_paths(lang=lang)[0]:
            if path['label'] != 'Arches' and path['label'] != 'Evaluation Criteria Type':
                paths.append(path['label'])
        return '; '.join(paths)

    current_status = 'None'
    user_can_edit_document = False
    resourcetypeid = report_info['type']
        
    # Pravice preverjamo zaenkrat le preko grup
    # Uporabnik mora imeti dodeljeno grupo z nazivom tipa resourca
    print request.user.username
    if (request.user.username != 'anonymous'):
        user = User.objects.get(username=request.user.username)
        user_groups = user.groups.values_list('name', flat=True)
    
        for entity in report_info['source']['graph']:
            if entity == 'EW_STATUS_E55':
                print report_info['source']['graph']["EW_STATUS_E55"]
                for value in report_info['source']['graph']["EW_STATUS_E55"]:
                    current_status = value["EW_STATUS_E55__label"]
        print "Current status for report: "
        print current_status
        user_can_edit_document = get_user_can_edit_document(current_status, 'same_group', user, resourcetypeid, user_groups, 'same_group')
        
    else:
        user_groups = []
        for entity in report_info['source']['graph']:
            if entity == 'EW_STATUS_E55':
                print report_info['source']['graph']["EW_STATUS_E55"]
                for value in report_info['source']['graph']["EW_STATUS_E55"]:
                    current_status = value["EW_STATUS_E55__label"]
        if current_status != settings.PUBLISHED_LABEL:
    		raise UserNotAuthorized('Unauthenticated users can view only published resources!')
    
    concept_label_ids = set()
    uuid_regex = re.compile('[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}')
    # gather together all uuid's referenced in the resource graph
    def crawl(items):
        for item in items:
            for key in item:
                if isinstance(item[key], list):
                    crawl(item[key])
                else:
                    if uuid_regex.match(item[key]):
                        if key == 'EVALUATION_CRITERIA_TYPE_E55__value':
                            item[key] = get_evaluation_path(item[key])
                        concept_label_ids.add(item[key])

    crawl([report_info['source']['graph']])

    # get all the concept labels from the uuid's
    concept_labels = se.search(index='concept_labels', id=list(concept_label_ids))

    # convert all labels to their localized prefLabel
    temp = {}
    if concept_labels != None:
        for concept_label in concept_labels['docs']:
            #temp[concept_label['_id']] = concept_label
            if concept_label['found']:
                # the resource graph already referenced the preferred label in the desired language
                if concept_label['_source']['type'] == 'prefLabel' and concept_label['_source']['language'] == lang:
                    temp[concept_label['_id']] = concept_label['_source']
                else: 
                    # the resource graph referenced a non-preferred label or a label not in our target language, so we need to get the right label
                    temp[concept_label['_id']] = get_preflabel_from_conceptid(concept_label['_source']['conceptid'], lang)

    # replace the uuid's in the resource graph with their preferred and localized label                    
    def crawl_again(items):
        for item in items:
            for key in item:
                if isinstance(item[key], list):
                    crawl_again(item[key])
                else:
                    if uuid_regex.match(item[key]):
                        try:
                            item[key] = temp[item[key]]['value']
                        except:
                            pass

    crawl_again([report_info['source']['graph']])
    # Podatke na visjih nivojih, ki zdruzujejo vec razlicnih nivojev, prestavimo na prvi nivo
    # To je potrebno zato, ker sicer ne moremo skrivati posameznih sklopov iz skupinske veje
    keys = ['REGION_E55','SETTLEMENT_TYPE_E55', 'CONTEXT_E55',
            'PLACE_ADDRESS_E45','PLACE_CADASTRAL_REFERENCE_E53',
            'ADMINISTRATIVE_SUBDIVISION_E48']
    old_key = 'PLACE_E53'
    for key in keys:
        if old_key in report_info['source']['graph']: 
            for data in report_info['source']['graph'][old_key]:
                if key in data:
                    if key not in report_info['source']['graph']:
                       report_info['source']['graph'][key] = []
                    report_info['source']['graph'][key].append(data.pop(key)[0])
    keys = ['SETTING_TYPE_E55','DESCRIPTION_OF_LOCATION_E62']
    old_key = 'PLACE_SITE_LOCATION_E53'
    old_key1 = 'PLACE_E53'
    for key in keys:
        if old_key1 in report_info['source']['graph']: 
            for data in report_info['source']['graph'][old_key1]:
                if old_key in data:
                    if key in data[old_key][0]:
                        if key not in report_info['source']['graph']:
                            report_info['source']['graph'][key] = []
                        report_info['source']['graph'][key].append(data[old_key][0].pop(key)[0])
    keys = ['DESCRIPTION_OF_LOCATION_E62']
    old_key = 'SPATIAL_COVERAGE_E53'
    old_key1 = 'PLACE_E53'
    for key in keys:
        if old_key1 in report_info['source']['graph']: 
            for data in report_info['source']['graph'][old_key1]:
                if old_key in data:
                    if key in data[old_key][0]:
                        if key not in report_info['source']['graph']:
                            report_info['source']['graph'][key] = []
                        report_info['source']['graph'][key].append(data[old_key][0].pop(key)[0])
    
    # PLY koncnico spremenimo za potrebe reporta v NXS
    if 'FILE_PATH_E62' in report_info['source']['graph']:
        report_info['source']['graph']['FILE_PATH_E62'][0]['FILE_PATH_E62__value'] = report_info['source']['graph']['FILE_PATH_E62'][0]['FILE_PATH_E62__label'].replace(".ply", ".nxs")
        report_info['source']['graph']['FILE_PATH_E62'][0]['FILE_PATH_E62__value'] = report_info['source']['graph']['FILE_PATH_E62'][0]['FILE_PATH_E62__value'].replace(".PLY", ".nxs")
        print 'Koncni path: ' + report_info['source']['graph']['FILE_PATH_E62'][0]['FILE_PATH_E62__value']

    #print report_info['source']['graph']
    
    #return JSONResponse(report_info, indent=4)

    related_resource_dict = {
        'HERITAGE_RESOURCE': [],
        'HERITAGE_RESOURCE_GROUP': [],
        'ACTIVITY': [],
        'ACTOR': [],
        'HISTORICAL_EVENT': [],
        'INFORMATION_RESOURCE_IMAGE': [],
        'INFORMATION_RESOURCE_DOCUMENT': [],
        'INFORMATION_RESOURCE_JSC3D': [],
        'INFORMATION_RESOURCE_3DHOP': []
    }

    related_resource_info = get_related_resources(resourceid, lang)

    # parse the related entities into a dictionary by resource type
    for related_resource in related_resource_info['related_resources']:
        information_resource_type = 'DOCUMENT'
        related_resource['relationship'] = []
        if related_resource['entitytypeid'] == 'HERITAGE_RESOURCE.E18':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'RESOURCE_TYPE_CLASSIFICATION.E55':
                    related_resource['relationship'].append(get_preflabel_from_valueid(entity['value'], lang)['value'])
        elif related_resource['entitytypeid'] == 'HERITAGE_RESOURCE_GROUP.E27':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'RESOURCE_TYPE_CLASSIFICATION.E55':
                    related_resource['relationship'].append(get_preflabel_from_valueid(entity['value'], lang)['value'])
        elif related_resource['entitytypeid'] == 'ACTIVITY.E7':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'ACTIVITY_TYPE.E55':
                    related_resource['relationship'].append(get_preflabel_from_valueid(entity['value'], lang)['value'])
        elif related_resource['entitytypeid'] == 'ACTOR.E39':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'ACTOR_TYPE.E55':
                    related_resource['relationship'].append(get_preflabel_from_conceptid(entity['conceptid'], lang)['value'])
                    related_resource['actor_relationshiptype'] = ''
        elif related_resource['entitytypeid'] == 'HISTORICAL_EVENT.E5':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'HISTORICAL_EVENT_TYPE.E55':
                    related_resource['relationship'].append(get_preflabel_from_conceptid(entity['conceptid'], lang)['value'])
        elif related_resource['entitytypeid'] == 'INFORMATION_RESOURCE.E73':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'INFORMATION_RESOURCE_TYPE.E55':
                    related_resource['relationship'].append(get_preflabel_from_valueid(entity['value'], lang)['value'])
            for entity in related_resource['child_entities']:
                if entity['entitytypeid'] == 'FILE_PATH.E62':
                    related_resource['file_path'] = settings.MEDIA_URL + entity['label']
                    
                    related_resource['file_path'] = settings.MEDIA_URL + entity['label']
                    # PLY koncnico spremenimo za potrebe reporta v NXS
                    if related_resource['file_path'][-3:].lower() == 'ply':
                        related_resource['file_path'] = related_resource['file_path'].replace(".ply", ".nxs")
                        related_resource['file_path'] = related_resource['file_path'].replace(".PLY", ".nxs")
                        information_resource_type = '3DHOP'
                    if related_resource['file_path'][-3:].lower() == 'stl' or related_resource['file_path'][-3:].lower() == 'obj':
                        information_resource_type = 'JSC3D'
                        
                if entity['entitytypeid'] == 'THUMBNAIL.E62':
                    related_resource['thumbnail'] = settings.MEDIA_URL + entity['label']
                    information_resource_type = 'IMAGE'
            
        # get the relationship between the two entities
        for relationship in related_resource_info['resource_relationships']:
            if relationship['entityid1'] == related_resource['entityid'] or relationship['entityid2'] == related_resource['entityid']: 
                related_resource['relationship'].append(get_preflabel_from_valueid(relationship['relationshiptype'], lang)['value'])
                if relationship['notes'] != '':
                    related_resource['relationship'].append(html2text.html2text(relationship['notes']))

        if len(related_resource['relationship']) > 0:
            related_resource['relationship'] = '(%s)' % (', '.join(related_resource['relationship']))
        else:
            related_resource['relationship'] = ''

        entitytypeidkey = related_resource['entitytypeid'].split('.')[0]
        if entitytypeidkey == 'INFORMATION_RESOURCE':
            entitytypeidkey = '%s_%s' % (entitytypeidkey, information_resource_type)
        related_resource_dict[entitytypeidkey].append(related_resource)

    resource_type_config = settings.RESOURCE_TYPE_CONFIGS()[resourcetypeid]
    return render_to_response('resource-report.htm', {
            'geometry': JSONSerializer().serialize(report_info['source']['geometry']),
            'resourceid': resourceid,
            'report_template': 'views/reports/' + report_info['type'] + '.htm',
            'report_info': report_info,
            'related_resource_dict': related_resource_dict,
            'main_script': 'resource-report',
            'active_page': 'ResourceReport',
            'RESOURCE_TYPE_CONFIGS': resource_type_config,
            'user_groups': user_groups,
            'current_status': current_status,
            'user_can_edit_document': user_can_edit_document,
            'help' : settings.HELP['report']
        },
        context_instance=RequestContext(request))   
Exemplo n.º 30
0
def main():

    sql = """ALTER TABLE concepts.concepts
            ALTER COLUMN conceptid DROP DEFAULT;

    ALTER TABLE concepts.concepts
            ALTER COLUMN legacyoid SET NOT NULL;

    ALTER TABLE concepts.concepts
            DROP CONSTRAINT IF EXISTS unique_concepts_legacyoid;

    ALTER TABLE concepts.concepts
            ADD CONSTRAINT unique_concepts_legacyoid UNIQUE (legacyoid);


    CREATE OR REPLACE VIEW ontology.vw_export_nodes AS 
            SELECT foo.assettype,
                    foo.node AS label,
                    (foo.assettype || ':'::text) || foo.node AS id,
                    foo.mergenodeid AS mergenode,
                    foo.businesstable AS businesstablename
            FROM ( SELECT m.entitytypeidfrom AS assettype,
                            r.entitytypedomain AS node,
                            m.mergenodeid,
                            ( SELECT entity_types.businesstablename
                                         FROM data.entity_types
                                        WHERE entity_types.entitytypeid = r.entitytypedomain) AS businesstable
                         FROM ontology.mapping_steps ms
                             JOIN ontology.mappings m ON m.mappingid = ms.mappingid
                             JOIN ontology.rules r ON r.ruleid = ms.ruleid
                    UNION
                     SELECT m.entitytypeidfrom,
                            r.entitytyperange AS node,
                            m.mergenodeid,
                            ( SELECT entity_types.businesstablename
                                         FROM data.entity_types
                                        WHERE entity_types.entitytypeid = r.entitytyperange) AS businesstable
                         FROM ontology.mapping_steps ms
                             JOIN ontology.mappings m ON m.mappingid = ms.mappingid
                             JOIN ontology.rules r ON r.ruleid = ms.ruleid) foo
            WHERE (foo.node <> ALL (ARRAY['ARCHES_RECORD.E31'::text, 'CREATION_EVENT.E65'::text, 'UPDATE_EVENT.E65'::text, 'COMPILER.E82'::text, 'COMPILER_PERSON.E21'::text, 'REFERENCE_NUMBER_(INTERNAL).E42'::text, 'TIME-SPAN_UPDATE_EVENT.E52'::text, 'TIME-SPAN_CREATION_EVENT.E52'::text, 'DATE_OF_COMPILATION.E50'::text, 'DATE_OF_LAST_UPDATE.E50'::text])) AND foo.node <> foo.assettype
            ORDER BY foo.assettype, foo.node;

            ALTER TABLE ontology.vw_export_nodes
                OWNER TO postgres;


    CREATE OR REPLACE VIEW ontology.vw_export_edges AS 
            SELECT m.entitytypeidfrom AS assettype,
                    (m.entitytypeidfrom || ':'::text) || r.entitytypedomain AS source,
                    (m.entitytypeidfrom || ':'::text) || r.entitytyperange AS target,
                    r.propertyid AS label
            FROM ontology.mapping_steps ms
                    JOIN ontology.mappings m ON m.mappingid = ms.mappingid
                    JOIN ontology.rules r ON r.ruleid = ms.ruleid
            WHERE (m.entitytypeidfrom <> ALL (ARRAY['ARCHES_RECORD.E31'::text, 'CREATION_EVENT.E65'::text, 'UPDATE_EVENT.E65'::text, 'COMPILER.E82'::text, 'COMPILER_PERSON.E21'::text, 'REFERENCE_NUMBER_(INTERNAL).E42'::text, 'TIME-SPAN_UPDATE_EVENT.E52'::text, 'TIME-SPAN_CREATION_EVENT.E52'::text, 'DATE_OF_COMPILATION.E50'::text, 'DATE_OF_LAST_UPDATE.E50'::text])) AND (r.entitytypedomain <> ALL (ARRAY['ARCHES_RECORD.E31'::text, 'CREATION_EVENT.E65'::text, 'UPDATE_EVENT.E65'::text, 'COMPILER.E82'::text, 'COMPILER_PERSON.E21'::text, 'REFERENCE_NUMBER_(INTERNAL).E42'::text, 'TIME-SPAN_UPDATE_EVENT.E52'::text, 'TIME-SPAN_CREATION_EVENT.E52'::text, 'DATE_OF_COMPILATION.E50'::text, 'DATE_OF_LAST_UPDATE.E50'::text])) AND (r.entitytyperange <> ALL (ARRAY['ARCHES_RECORD.E31'::text, 'CREATION_EVENT.E65'::text, 'UPDATE_EVENT.E65'::text, 'COMPILER.E82'::text, 'COMPILER_PERSON.E21'::text, 'REFERENCE_NUMBER_(INTERNAL).E42'::text, 'TIME-SPAN_UPDATE_EVENT.E52'::text, 'TIME-SPAN_CREATION_EVENT.E52'::text, 'DATE_OF_COMPILATION.E50'::text, 'DATE_OF_LAST_UPDATE.E50'::text])) AND m.entitytypeidto = r.entitytyperange
            ORDER BY m.entitytypeidfrom;

    ALTER TABLE ontology.vw_export_edges
        OWNER TO postgres;

    INSERT INTO concepts.d_valuetypes SELECT 'sortorder', 'undefined', null, 'arches', 'text'
            WHERE NOT EXISTS (SELECT 1 FROM concepts.d_valuetypes WHERE valuetype = 'sortorder'); 


    CREATE OR REPLACE FUNCTION concepts.concpets_ins()
            RETURNS trigger AS
            $BODY$
            DECLARE
             v_uuid uuid = public.uuid_generate_v1mc();

            BEGIN
            --Provides CONCEPTID for RDM inserts and cases where ETL conceptid is not a UUID
                IF NEW.CONCEPTID IS NULL THEN
                     NEW.CONCEPTID := v_uuid;
                END IF;

             -- Supports RDM where no concpetid or legacyoid is fed in
                IF NEW.CONCEPTID IS NULL AND (NEW.LEGACYOID IS NULL OR NEW.LEGACYOID = '') THEN
                     NEW.LEGACYOID = v_uuid::text;
                END IF;   


            -- I would assume that two cases below are handled in python code by being explicit about insert values for both columns... just coding defensively here. ABL.
            -- Supports where ETL provided conceptid is a UUID and will be kept, but no LEGACYOID provided.
                IF NEW.CONCEPTID IS NOT NULL and (NEW.LEGACYOID is null or NEW.LEGACYOID = '') THEN
                     NEW.LEGACYOID = NEW.CONCEPTID::text;     
                END IF;   

            -- Supports where ETL'ed conceptid is not a UUID.  Populates original "concpetid" as LEGACYOID.
                IF NEW.LEGACYOID IS NOT NULL OR NEW.LEGACYOID != '' then
                     NEW.LEGACYOID = NEW.LEGACYOID;     
                END IF;   

            RETURN NEW;
            END$BODY$
                LANGUAGE plpgsql VOLATILE
                COST 100;

    ALTER FUNCTION concepts.concpets_ins()
        OWNER TO postgres;

        
        
    -- Trigger: concepts_ins_tgr on concepts.concepts

    DROP TRIGGER IF EXISTS concepts_ins_tgr ON concepts.concepts;

    CREATE TRIGGER concepts_ins_tgr
        BEFORE INSERT
        ON concepts.concepts
        FOR EACH ROW
        EXECUTE PROCEDURE concepts.concpets_ins();"""


    with transaction.atomic():
        #import arches.management.patches.upgrade_to_v3_0_4

        cursor = connection.cursor()
        cursor.execute(sql)

        anonymous_user, created = User.objects.get_or_create(username='******')
        if created:
                anonymous_user.set_password('')

        read_group, created = Group.objects.get_or_create(name='read')
        anonymous_user.groups.add(read_group)

        edit_group, created = Group.objects.get_or_create(name='edit')
        admin_user = User.objects.get(username='******')
        admin_user.groups.add(edit_group)
        admin_user.groups.add(read_group)

        print '\nINSTALLING PYSHP MODULE'
        print '-----------------------'
        pip.main(['install', 'pyshp'])


        print '\nUPDATING ENTITY INDEX'
        print '---------------------'

        # Add numbers array to resources that do not have them. Move numbers data from child_entities to numbers array in index.
        resourceid_sql = "SELECT entityid FROM data.entities WHERE entitytypeid IN (SELECT distinct(entitytypeid) FROM data.entity_types WHERE isresource =True);"
        cursor.execute(resourceid_sql)
        resourceids = []
        for val in cursor.fetchall():
            resourceids.append(val[0])
        
        start = time.time()
        records = 0
        se = SearchEngineFactory().create()
        for resourceid in resourceids:
            indexed_resource = se.search(index='entity', id=resourceid)        

            if 'numbers' not in indexed_resource['_source']:
                indexed_resource['_source']['numbers'] = []
            else:
                pass
                
            for child_entity in indexed_resource['_source']['child_entities']:
                if child_entity['businesstablename'] == 'numbers':
                    index_resource['_source']['numbers'].append(child_entity)
                    indexed_resource['_source']['child_entities'].remove(child_entity)
        
            ## Reindex resource here.
            se.index_data(index='entity',doc_type=indexed_resource['_type'], body=indexed_resource['_source'], id=indexed_resource['_id'])
            records+=1
            # if records%500 == 0:
            #     print '%s records processed'%str(records)

        print '%s records updated' % str(records)

        # print 'Patch took %s seconds to run.'%str(time.time() - start)

        print "\npatch '%s' successfully applied." % __name__
Exemplo n.º 31
0
    def get_related_resources(self,
                              lang='en-US',
                              limit=settings.RELATED_RESOURCES_EXPORT_LIMIT,
                              start=0,
                              page=0):
        """
        Returns an object that lists the related resources, the relationship types, and a reference to the current resource

        """
        root_nodes = models.Node.objects.filter(istopnode=True)
        node_config_lookup = {}

        for node in root_nodes:
            graph_id = unicode(node.graph_id)
            if node.config != None:
                node_config_lookup[graph_id] = node.config
                node_config_lookup[graph_id][
                    'iconclass'] = node.graph.iconclass
                node_config_lookup[graph_id]['name'] = node.graph.name

        ret = {
            'resource_instance':
            self,
            'resource_relationships': [],
            'related_resources': [],
            'root_node_config':
            models.Node.objects.filter(graph_id=self.graph.graphid).filter(
                istopnode=True)[0].config,
            'node_config_lookup':
            node_config_lookup
        }
        se = SearchEngineFactory().create()

        if page > 0:
            limit = settings.RELATED_RESOURCES_PER_PAGE
            start = limit * int(page - 1)

        def get_relations(resourceinstanceid, start, limit):
            query = Query(se, start=start, limit=limit)
            bool_filter = Bool()
            bool_filter.should(
                Terms(field='resourceinstanceidfrom',
                      terms=resourceinstanceid))
            bool_filter.should(
                Terms(field='resourceinstanceidto', terms=resourceinstanceid))
            query.add_query(bool_filter)
            return query.search(index='resource_relations', doc_type='all')

        resource_relations = get_relations(self.resourceinstanceid, start,
                                           limit)
        ret['total'] = resource_relations['hits']['total']
        instanceids = set()

        for relation in resource_relations['hits']['hits']:
            try:
                preflabel = get_preflabel_from_valueid(
                    relation['_source']['relationshiptype'], lang)
                relation['_source']['relationshiptype_label'] = preflabel[
                    'value']
            except:
                relation['_source']['relationshiptype_label'] = relation[
                    '_source']['relationshiptype']

            ret['resource_relationships'].append(relation['_source'])
            instanceids.add(relation['_source']['resourceinstanceidto'])
            instanceids.add(relation['_source']['resourceinstanceidfrom'])
        if len(instanceids) > 0:
            instanceids.remove(str(self.resourceinstanceid))

        if len(instanceids) > 0:
            related_resources = se.search(index='resource',
                                          doc_type='_all',
                                          id=list(instanceids))
            if related_resources:
                for resource in related_resources['docs']:
                    relations = get_relations(resource['_id'], 0, 0)
                    resource['_source']['total_relations'] = relations['hits'][
                        'total']
                    ret['related_resources'].append(resource['_source'])
        return ret
Exemplo n.º 32
0
def map_layers(request, entitytypeid='all', get_centroids=False):
    lang = request.GET.get('lang', request.LANGUAGE_CODE)
    if lang == 'en':
       lang = 'en-US'
    data = []
    geom_param = request.GET.get('geom', None)
    print 'map_layers: ' + entitytypeid
    #print request.method
    bbox = request.GET.get('bbox', '')
    limit = request.GET.get('limit', settings.MAP_LAYER_FEATURE_LIMIT)
    if request.method == 'GET':
        entityids = request.GET.get('entityid', '')
    elif request.method == 'POST':
        entityids = request.POST.get('entityid', '')
    #print entityids 
    geojson_collection = {
      "type": "FeatureCollection",
      "features": []
    }
    #print request.META
    url =  request.META.get('HTTP_REFERER')
    searchType = 'Search'
    if not url:
        return JSONResponse(geojson_collection)
    if url.find('searchType')>0:
        parsed = urlparse.urlparse(url)
        searchType =  urlparse.parse_qs(parsed.query)['searchType'][0]
    else:
        if url.find('search_sites')>0:
            searchType = 'Site'
            entitytypeid = 'SITE.E18'
        elif url.find('search_graves')>0:
            searchType = 'Grave'
            entitytypeid = 'GRAVE.E18'
        elif url.find('search_objects')>0:
            searchType = 'Object' 
            entitytypeid = 'OBJECT.E18'  
    #print searchType
    se = SearchEngineFactory().create()
    query = Query(se, limit=limit)
    args = { 'index': 'maplayers' }
    if entitytypeid != 'all':
        args['doc_type'] = entitytypeid
    if entityids != '':
        for entityid in entityids.split(','):
            item = se.search(index='maplayers', id=entityid)
            #print item
            # Prevodi
            #print 'Result_item'
            #print item['_source']['properties']
            concept_label_ids = set()
            uuid_regex = re.compile('[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}')
            # gather together all uuid's referenced in the resource graph
            def crawl(items):
                for item in items:
                    if isinstance(item, dict):
                        for key in item:
                            if isinstance(item[key], list):
                                crawl(item[key])
                            else:
                                if isinstance(item[key], basestring) and uuid_regex.match(item[key]):
                                    concept_label_ids.add(item[key])

            crawl([item['_source']['properties']])
            
            # get all the concept labels from the uuid's
            concept_labels = se.search(index='concept_labels', id=list(concept_label_ids))

            # convert all labels to their localized prefLabel
            temp = {}
            if concept_labels != None:
                for concept_label in concept_labels['docs']:
                    #temp[concept_label['_id']] = concept_label
                    if concept_label['found']:
                        # the resource graph already referenced the preferred label in the desired language
                        if concept_label['_source']['type'] == 'prefLabel' and concept_label['_source']['language'] == lang:
                            temp[concept_label['_id']] = concept_label['_source']
                        else: 
                            # the resource graph referenced a non-preferred label or a label not in our target language, so we need to get the right label
                            temp[concept_label['_id']] = get_preflabel_from_conceptid(concept_label['_source']['conceptid'], lang)
            
            # replace the uuid's in the resource graph with their preferred and localized label                    
            def crawl_again(items):
                for item in items:
                    if isinstance(item, dict):
                        for key in item:
                            if isinstance(item[key], list):
                                crawl_again(item[key])
                            else:
                                if isinstance(item[key], basestring) and uuid_regex.match(item[key]):
                                    try:
                                        item[key] = temp[item[key]]['value']
                                    except:
                                        pass

            crawl_again([item['_source']['properties']])
            #print 'crawl_again'
            #print item['_source']['properties']
            
            geojson_collection['features'].append(item['_source'])
            #geojson_collection['features'].append(se.search(index='maplayers', id=entityid)['_source'])
            
            #Poiskus pridobitve slik - pridobiti se jih da, vendar jih je potem problem prikazati, zato jih tu ne bomo prikazovali
            #related_resources = get_related_resources(entityid, lang, start=0, limit=15)
            #if related_resources['related_resources']:
            #    thumbnails = {'thumbnail': [] }
            #    for entity in related_resources['related_resources'][0]['child_entities']:
                    #print entity
            #        if entity['entitytypeid']=='THUMBNAIL.E62':
            #            thumbnails['thumbnail'].append(entity['value'])
            #    item['_source']['properties']['thumbnails'] = thumbnails
        #print item['_source']['properties']
        geojson_collection['features'] = sorted(geojson_collection['features'], key=lambda k: (k['properties']['primaryname'].lower())) 
        return JSONResponse(geojson_collection)
    data = query.search(**args)
    for item in data['hits']['hits']:
        # Ce nismo na splosnem searchu, upostevamo samo ustrezne tipe resourcov
        if (searchType != 'Search'):
            #print item
            #print item['_source']['properties']['searchType']
            if (item['_source']['properties']['searchType'] != searchType):
                continue
            #print 'Je'
        
        if get_centroids:
            item['_source']['geometry'] = item['_source']['properties']['centroid']
            item['_source'].pop('properties', None)
        elif geom_param != None:
            item['_source']['geometry'] = item['_source']['properties'][geom_param]
            item['_source']['properties'].pop('extent', None)
            item['_source']['properties'].pop(geom_param, None)
        else:
            item['_source']['properties'].pop('extent', None)
            item['_source']['properties'].pop('centroid', None)
        geojson_collection['features'].append(item['_source'])
    print 'St. zapisov: '
    print len(data['hits']['hits'])
    return JSONResponse(geojson_collection)   
Exemplo n.º 33
0
def report(request, resourceid):
    lang = request.GET.get('lang', request.LANGUAGE_CODE)
    print 'Jezik:'
    if lang == 'en':
       lang = 'en-US'
    print lang
    se = SearchEngineFactory().create()
    try:
        report_info = se.search(index='resource', id=resourceid)
    except elasticsearch.ElasticsearchException as es1:
        if es1[0] == 404:
            result = JSONDeserializer().deserialize(es1[1])
            if not result['found']:
                return render_to_response('404.htm', {
                    'main_script': '404',
                    'active_page': '404'
                },
                context_instance=RequestContext(request))
    #print report_info
    report_info['source'] = report_info['_source']
    report_info['type'] = report_info['_type']
    report_info['source']['graph'] = report_info['source']['graph']
    del report_info['_source']
    del report_info['_type']

    def get_evaluation_path(valueid):
        value = models.Values.objects.get(pk=valueid)
        concept_graph = Concept().get(id=value.conceptid_id, include_subconcepts=False, 
            include_parentconcepts=True, include_relatedconcepts=False, up_depth_limit=None, lang=lang)
        
        paths = []
        for path in concept_graph.get_paths(lang=lang)[0]:
            if path['label'] != 'Arches' and path['label'] != 'Evaluation Criteria Type':
                paths.append(path['label'])
        return '; '.join(paths)


    concept_label_ids = set()
    uuid_regex = re.compile('[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}')
    # gather together all uuid's referenced in the resource graph
    def crawl(items):
        for item in items:
            for key in item:
                if isinstance(item[key], list):
                    crawl(item[key])
                else:
                    if isinstance(item[key], basestring) and uuid_regex.match(item[key]):
                        if key == 'EVALUATION_CRITERIA_TYPE_E55__value':
                            item[key] = get_evaluation_path(item[key])
                        concept_label_ids.add(item[key])

    crawl([report_info['source']['graph']])

    # get all the concept labels from the uuid's
    concept_labels = se.search(index='concept_labels', id=list(concept_label_ids))

    # convert all labels to their localized prefLabel
    temp = {}
    if concept_labels != None:
        for concept_label in concept_labels['docs']:
            #temp[concept_label['_id']] = concept_label
            if concept_label['found']:
                # the resource graph already referenced the preferred label in the desired language
                if concept_label['_source']['type'] == 'prefLabel' and concept_label['_source']['language'] == lang:
                    temp[concept_label['_id']] = concept_label['_source']
                else: 
                    # the resource graph referenced a non-preferred label or a label not in our target language, so we need to get the right label
                    temp[concept_label['_id']] = get_preflabel_from_conceptid(concept_label['_source']['conceptid'], lang)
         
    # replace the uuid's in the resource graph with their preferred and localized label                    
    def crawl_again(items):
        for item in items:
            for key in item:
                if isinstance(item[key], list):
                    crawl_again(item[key])
                else:
                    if isinstance(item[key], basestring) and uuid_regex.match(item[key]):
                        try:
                            item[key] = temp[item[key]]['value']
                        except:
                            pass

    crawl_again([report_info['source']['graph']])
    
    # Podatke na visjih nivojih, ki zdruzujejo vec razlicnih nivojev, prestavimo na prvi nivo
    # To je potrebno zato, ker sicer ne moremo skrivati posameznih sklopov iz skupinske veje
    keys = ['REGION_E55','COUNTRY_E55','OTHER_NAME_E48','SETTLEMENT_E48','TOPOGRAPHICAL_UNIT_E48', 'TOPOGRAPHICAL_AREA_E48',
            'PLACE_ADDRESS_E45','PLACE_CADASTRAL_REFERENCE_E53',
            'ADMINISTRATIVE_SUBDIVISION_E48']
    old_key = 'PLACE_E53'
    for key in keys:
        if old_key in report_info['source']['graph']: 
            for data in report_info['source']['graph'][old_key]:
                if key in data:
                    if key not in report_info['source']['graph']:
                       report_info['source']['graph'][key] = []
                    report_info['source']['graph'][key].append(data.pop(key)[0])
    keys = ['SETTING_TYPE_E55','DESCRIPTION_OF_LOCATION_E62']
    old_key = 'PLACE_SITE_LOCATION_E53'
    old_key1 = 'PLACE_E53'
    for key in keys:
        if old_key1 in report_info['source']['graph']: 
            for data in report_info['source']['graph'][old_key1]:
                if old_key in data:
                    if key in data[old_key][0]:
                        if key not in report_info['source']['graph']:
                            report_info['source']['graph'][key] = []
                        report_info['source']['graph'][key].append(data[old_key][0].pop(key)[0])
    keys = ['DESCRIPTION_OF_LOCATION_E62']
    old_key = 'SPATIAL_COVERAGE_E53'
    old_key1 = 'PLACE_E53'
    for key in keys:
        if old_key1 in report_info['source']['graph']: 
            for data in report_info['source']['graph'][old_key1]:
                if old_key in data:
                    if key in data[old_key][0]:
                        if key not in report_info['source']['graph']:
                            report_info['source']['graph'][key] = []
                        report_info['source']['graph'][key].append(data[old_key][0].pop(key)[0])
    keys = ['BODY_CODE_E42','AGE_MIN_E16','AGE_MAX_E16','BODY_FEATURE_E55','BODY_DESCRIPTION_E62']
    old_key = 'BODY_E21'
    for key in keys:
        if old_key in report_info['source']['graph']: 
            for data in report_info['source']['graph'][old_key]:
                if key in data:
                    if key not in report_info['source']['graph']:
                       report_info['source']['graph'][key] = []
                    report_info['source']['graph'][key].append(data.pop(key)[0])
    
    # Vsem stevilcnim podatkom spremenimo pike v vejice (Arches shrani podatke v string, ceprav je v grafu number!)
    #keys = ['GRAVE_MEASUREMENT_TYPE_E55','OBJECT_MEASUREMENT_TYPE_E55']
    #numbers = ['VALUE_OF_MEASUREMENT_E60__value']
    #for key in keys:
    #    if key in report_info['source']['graph']: 
    #        for data in report_info['source']['graph'][key]:
    #            for number in numbers:
    #                if number in data:
    #                    data[number] = data[number].replace('.',',')

    # Vse datumske podatke spremenimo le v leta 
    if 'BEGINNING_OF_EXISTENCE_E63' in report_info['source']['graph']:
        for data in report_info['source']['graph']['BEGINNING_OF_EXISTENCE_E63']:
            for data1 in data['BEGINNING_OF_EXISTENCE_TIME___SPAN_E52']:
                if 'START_DATE_OF_EXISTENCE_E49' in data1:
                    for data2 in data1['START_DATE_OF_EXISTENCE_E49']:
                        if 'START_DATE_OF_EXISTENCE_E49__value' in data2:
                            data2['START_DATE_OF_EXISTENCE_E49__value'] = data2['START_DATE_OF_EXISTENCE_E49__value'][:4].lstrip("0")
    if 'END_OF_EXISTENCE_E64' in report_info['source']['graph']:
        for data in report_info['source']['graph']['END_OF_EXISTENCE_E64']:
            for data1 in data['END_OF_EXISTENCE_TIME___SPAN_E52']:
                if 'END_DATE_OF_EXISTENCE_E49' in data1:
                    for data2 in data1['END_DATE_OF_EXISTENCE_E49']:
                        if 'END_DATE_OF_EXISTENCE_E49__value' in data2:
                            data2['END_DATE_OF_EXISTENCE_E49__value'] = data2['END_DATE_OF_EXISTENCE_E49__value'][:4].lstrip("0")

    
    #print report_info
    #return JSONResponse(report_info, indent=4)

    related_resource_dict = {
        'HERITAGE_RESOURCE': [],
        'HERITAGE_RESOURCE_GROUP': [],
        'ACTIVITY': [],
        'ACTOR': [],
        'HISTORICAL_EVENT': [],
        'INFORMATION_RESOURCE_IMAGE': [],
        'INFORMATION_RESOURCE_DOCUMENT': [],
        'SITE': [],
        'GRAVE': [],
        'OBJECT': []
    }

    related_resource_info = get_related_resources(resourceid, lang)

    # parse the related entities into a dictionary by resource type
    for related_resource in related_resource_info['related_resources']:
        information_resource_type = 'DOCUMENT'
        related_resource['relationship'] = []
        # Predpostavka, da so v information resources samo zahtevani podatki, sicer je potrebno parsati tudi to
        #print 'Leto izdaje:'
        #if related_resource['dates']:
            #print related_resource['dates'][0]['value'][:4]
            #related_resource['pub_year'] = related_resource['dates'][0]['value'][:4]
        for child in related_resource['child_entities']:
            if child['entitytypeid'] == 'TITLE.E41':
                #print 'Naslov:'
                #print child['label']
                related_resource['title'] = child['label']
            elif child['entitytypeid'] == 'PUBLICATION_TITLE.E41':
                #print 'Naslov publikacije:'
                #print child['label']
                related_resource['pub_title'] = child['label']
            elif child['entitytypeid'] == 'PLACE_OF_CREATION.E48':
                #print 'Kraj izdaje:'
                #print child['label']
                related_resource['pub_place'] = child['label']
            elif child['entitytypeid'] == 'YEAR_OF_CREATION.E62':
                #print 'Leto izdaje:'
                #print child['label']
                related_resource['pub_year'] = child['label']
            elif child['entitytypeid'] == 'CREATOR_APPELLATION.E82':
                #print 'Avtor:'
                #print child['label']      
                related_resource['author'] = child['label']         
                
        if related_resource['entitytypeid'] == 'HERITAGE_RESOURCE.E18':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'RESOURCE_TYPE_CLASSIFICATION.E55':
                    related_resource['relationship'].append(get_preflabel_from_valueid(entity['value'], lang)['value'])
        elif related_resource['entitytypeid'] == 'HERITAGE_RESOURCE_GROUP.E27':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'RESOURCE_TYPE_CLASSIFICATION.E55':
                    related_resource['relationship'].append(get_preflabel_from_valueid(entity['value'], lang)['value'])
        elif related_resource['entitytypeid'] == 'ACTIVITY.E7':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'ACTIVITY_TYPE.E55':
                    related_resource['relationship'].append(get_preflabel_from_valueid(entity['value'], lang)['value'])
        elif related_resource['entitytypeid'] == 'ACTOR.E39':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'ACTOR_TYPE.E55':
                    related_resource['relationship'].append(get_preflabel_from_conceptid(entity['conceptid'], lang)['value'])
                    related_resource['actor_relationshiptype'] = ''
        elif related_resource['entitytypeid'] == 'HISTORICAL_EVENT.E5':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'HISTORICAL_EVENT_TYPE.E55':
                    related_resource['relationship'].append(get_preflabel_from_conceptid(entity['conceptid'], lang)['value'])
        elif related_resource['entitytypeid'] == 'INFORMATION_RESOURCE.E73':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'INFORMATION_RESOURCE_TYPE.E55':
                    related_resource['relationship'].append(get_preflabel_from_valueid(entity['value'], lang)['value'])
            for entity in related_resource['child_entities']:
                if entity['entitytypeid'] == 'FILE_PATH.E62':
                    related_resource['file_path'] = settings.MEDIA_URL + entity['label']
                if entity['entitytypeid'] == 'THUMBNAIL.E62':
                    related_resource['thumbnail'] = settings.MEDIA_URL + entity['label']
                    information_resource_type = 'IMAGE'
            
        # get the relationship between the two entities
        for relationship in related_resource_info['resource_relationships']:
            if relationship['entityid1'] == related_resource['entityid'] or relationship['entityid2'] == related_resource['entityid']: 
                related_resource['relationship'].append(get_preflabel_from_valueid(relationship['relationshiptype'], lang)['value'])
                related_resource['notes'] = relationship['notes']

        entitytypeidkey = related_resource['entitytypeid'].split('.')[0]
        if entitytypeidkey == 'INFORMATION_RESOURCE':
            entitytypeidkey = '%s_%s' % (entitytypeidkey, information_resource_type)
        related_resource_dict[entitytypeidkey].append(related_resource)
    
    #related_resource_dict['INFORMATION_RESOURCE_DOCUMENT'] = sorted(related_resource_dict['INFORMATION_RESOURCE_DOCUMENT'], key=lambda k: k['pub_year']) 
    related_resource_dict['INFORMATION_RESOURCE_DOCUMENT'] = sorted(related_resource_dict['INFORMATION_RESOURCE_DOCUMENT'], key=lambda k: ('pub_year' not in k, k.get('pub_year', None)))
    print 'Report...'
    
    return render_to_response('resource-report.htm', {
            'geometry': JSONSerializer().serialize(report_info['source']['geometry']),
            'resourceid': resourceid,
            'report_template': 'views/reports/' + report_info['type'] + '.htm',
            'report_info': report_info,
            'related_resource_dict': related_resource_dict,
            'main_script': 'resource-report-zbiva',
            'active_page': 'ResourceReport'
        },
        context_instance=RequestContext(request))     
Exemplo n.º 34
0
def user_activity_data(request, userid):
    se = SearchEngineFactory().create()
    ret = []
    ret_summary = {}
    current = None
    index = -1
    start = request.GET.get('start', 0)
    limit = request.GET.get('limit', 99)
    if userid != '':
        dates = models.EditLog.objects.filter(userid=userid).values_list(
            'timestamp', flat=True).order_by('-timestamp').distinct(
                'timestamp')[start:limit]

        for log in models.EditLog.objects.filter(
                userid=userid, timestamp__in=dates).values().order_by(
                    '-timestamp', 'attributeentitytypeid'):
            if str(log['timestamp']) != current:
                current = str(log['timestamp'])
                ret.append({
                    'date':
                    str(log['timestamp'].date()),
                    'time':
                    str(log['timestamp'].time().replace(
                        microsecond=0).isoformat()),
                    'log': []
                })
                index = index + 1

            ret[index]['log'].append(log)

            try:
                resource = se.search(index='resource', id=log['resourceid'])
                ret[index]['resourceid'] = log['resourceid']
                ret[index]['name'] = resource['_source']['primaryname']
            except:

                ret[index]['name'] = _('deleted item')

            if str(log['timestamp'].date()) not in ret_summary:
                ret_summary[str(log['timestamp'].date())] = {}

            if log['resourceid'] not in ret_summary[str(
                    log['timestamp'].date())]:
                ret_summary[str(
                    log['timestamp'].date())][log['resourceid']] = {
                        'create': 0,
                        'update': 0,
                        'insert': 0,
                        'delete': 0
                    }

            ret_summary[str(log['timestamp'].date())][log['resourceid']][
                log['edittype']] = 1

        head_text = _('no data for this user')
        if len(ret):
            head_text = ret[0]['log'][0]['user_firstname'] + ' ' + ret[0][
                'log'][0]['user_lastname']
            if head_text != ' ' and ret[0]['log'][0]['user_email'] != '':
                head_text += ', '
            head_text += ret[0]['log'][0]['user_email']

        return JSONResponse({
            'head_text': head_text,
            'activity': ret,
            'activity_summary': ret_summary,
        })
Exemplo n.º 35
0
def report(request, resourceid):
    logging.warning("Viewing Report. User=%s", request.user)

    # Redirect non-logged-in users to the login screen
    if request.user.is_anonymous:
        redirect('/auth')

    lang = request.GET.get('lang', request.LANGUAGE_CODE)
    page = request.GET.get('page', 1)
    se = SearchEngineFactory().create()
    report_info = se.search(index='resource', id=resourceid)
    primaryname = se.search(index='entity', id=resourceid)
    report_info['source'] = report_info['_source']
    report_info['type'] = report_info['_type']
    report_info['source']['graph'] = report_info['source']['graph']
    if primaryname['_source']['primaryname']:
        report_info['source']['primaryname'] = primaryname['_source'][
            'primaryname']
    del report_info['_source']
    del report_info['_type']
    geometry = JSONSerializer().serialize(report_info['source']['geometry'])
    GeoCrypt = Crypter(settings.ENCODING_KEY)
    iv, encrypted = GeoCrypt.encrypt(geometry, GeoCrypt.KEY)
    ciphertext = binascii.b2a_base64(encrypted).rstrip()
    if geometry != 'null':
        result = {
            'editor':
            'true' if 'edit' in request.user.user_groups else 'false',
            'key': GeoCrypt.KEY,
            'iv': iv,
            'ciphertext': ciphertext
        }
    else:
        result = None

    if report_info[
            'type'] == "INFORMATION_RESOURCE.E73":  # These clauses produce subtypes for Imagery, Shared Dataset and Cartography Info Resources, with the aim of producing different Report pages for each of these Resource Types
        report_info['subtype'] = ''
        report_info['filepath'] = ''
        report_info['has_image'] = ''
        if 'ACQUISITION_ASSIGNMENT_E17' in report_info['source'][
                'graph'] or 'CATALOGUE_ID_E42' in report_info['source'][
                    'graph']:
            report_info['subtype'] = 'Imagery'
        if 'RESOURCE_CREATION_EVENT_E65' in report_info['source']['graph']:
            for value in report_info['source']['graph'][
                    'RESOURCE_CREATION_EVENT_E65']:
                if 'CREATOR_E39' in value:
                    for subvalue in value['CREATOR_E39']:
                        if 'IMAGERY_CREATOR_APPELLATION_E82' in subvalue:
                            report_info['subtype'] = 'Imagery'
                        elif 'SHARED_DATA_SOURCE_CREATOR_APPELLATION_E82' in subvalue:
                            report_info['subtype'] = 'Shared'
                if 'SHARED_DATA_SOURCE_SHARER_E39' in value:
                    report_info['subtype'] = 'Shared'
        if 'PUBLICATION_EVENT_E12' in report_info['source']['graph']:
            for value in report_info['source']['graph'][
                    'PUBLICATION_EVENT_E12']:
                if 'PUBLICATION_ASSIGNMENT_E17' in value:
                    for subvalue in value['PUBLICATION_ASSIGNMENT_E17']:
                        if 'TILE_SQUARE_APPELLATION_E44' in subvalue or 'TILE_SQUARE_DETAILS_E44' in subvalue:
                            report_info['subtype'] = 'Cartography'
                        elif 'IMAGERY_SOURCE_TYPE_E55' in subvalue:
                            report_info['subtype'] = 'Imagery'
        if 'FILE_PATH_E62' in report_info['source']['graph']:
            report_info['filepath'] = report_info['source']['graph'][
                'FILE_PATH_E62'][0]
        if 'THUMBNAIL_E62' in report_info['source']['graph']:
            report_info['has_image'] = report_info['source']['graph'][
                'THUMBNAIL_E62'][0]
        if 'URL_E51' in report_info['source'][
                'graph']:  #If the resource has a URL, it verifies that it is an APAAME json string, in which case it retrieves the url of the photo from the json string and passes it to the report
            flickr_feed = report_info['source']['graph']['URL_E51'][0][
                'URL_E51__value'][:-1] if report_info['source']['graph'][
                    'URL_E51'][0]['URL_E51__value'].endswith(
                        '/') else report_info['source']['graph']['URL_E51'][0][
                            'URL_E51__value']
            try:
                response = urllib.urlopen(
                    'https://www.flickr.com/services/oembed?url=' +
                    flickr_feed + '&format=json')
                data = response.read().decode("utf-8")
                flickr_feed = json.loads(data)
                report_info['filepath'], report_info[
                    'has_image'] = flickr_feed['url'], True
            except:
                pass

    def get_evaluation_path(valueid):
        value = models.Values.objects.get(pk=valueid)
        concept_graph = Concept().get(id=value.conceptid_id,
                                      include_subconcepts=False,
                                      include_parentconcepts=True,
                                      include_relatedconcepts=False,
                                      up_depth_limit=None,
                                      lang=lang)

        paths = []
        for path in concept_graph.get_paths(lang=lang)[0]:
            if path['label'] != 'Arches' and path[
                    'label'] != 'Evaluation Criteria Type':
                paths.append(path['label'])
        return '; '.join(paths)

    concept_label_ids = set()
    uuid_regex = re.compile(
        '[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}'
    )

    # gather together all uuid's referenced in the resource graph
    def crawl(items):
        for item in items:
            for key in item:
                if isinstance(item[key], list):
                    crawl(item[key])
                else:
                    if isinstance(item[key], basestring) and uuid_regex.match(
                            item[key]):
                        if key == 'EVALUATION_CRITERIA_TYPE_E55__value':
                            item[key] = get_evaluation_path(item[key])
                        concept_label_ids.add(item[key])

    crawl([report_info['source']['graph']])

    # get all the concept labels from the uuid's
    concept_labels = se.search(index='concept_labels',
                               id=list(concept_label_ids))

    # convert all labels to their localized prefLabel
    temp = {}
    if concept_labels != None:
        for concept_label in concept_labels['docs']:
            #temp[concept_label['_id']] = concept_label
            if concept_label['found']:
                # the resource graph already referenced the preferred label in the desired language
                if concept_label['_source'][
                        'type'] == 'prefLabel' and concept_label['_source'][
                            'language'] == lang:
                    temp[concept_label['_id']] = concept_label['_source']
                else:
                    # the resource graph referenced a non-preferred label or a label not in our target language, so we need to get the right label
                    temp[concept_label['_id']] = get_preflabel_from_conceptid(
                        concept_label['_source']['conceptid'], lang)

    # replace the uuid's in the resource graph with their preferred and localized label
    def crawl_again(items):
        for item in items:
            for key in item:
                if isinstance(item[key], list):
                    crawl_again(item[key])
                else:
                    if isinstance(item[key], basestring) and uuid_regex.match(
                            item[key]):
                        try:
                            item[key] = temp[item[key]]['value']
                        except:
                            pass

    crawl_again([report_info['source']['graph']])

    #return JSONResponse(report_info, indent=4)

    related_resource_dict = {
        'HERITAGE_RESOURCE': [],
        'HERITAGE_RESOURCE_GROUP': [],
        'HERITAGE_FEATURE': [],
        'HERITAGE_COMPONENT': [],
        'ACTIVITY': [],
        'ACTOR': [],
        'HISTORICAL_EVENT': [],
        'INFORMATION_RESOURCE_IMAGE': [],
        'INFORMATION_RESOURCE_DOCUMENT': [],
        'INFORMATION_RESOURCE_MAP': [],
        'INFORMATION_RESOURCE_SATELLITE': [],
        'INFORMATION_RESOURCE_SHARED': []
    }

    related_resource_info = get_related_resources(resourceid, lang)
    # parse the related entities into a dictionary by resource type
    for related_resource in related_resource_info['related_resources']:
        VirtualGlobeName = []
        OtherImageryName = []
        SharedDataset = []
        VirtualGlobe = False
        OtherImagery = True
        information_resource_type = 'DOCUMENT'
        related_res = {}
        related_res['relationship'] = []
        related_res['datefrom'] = []
        related_res['dateto'] = []
        related_res['notes'] = []
        related_res['date'] = []
        related_res['identifier'] = []
        related_res['entitytypeid'] = related_resource['entitytypeid']
        related_res['entityid'] = related_resource['entityid']
        related_res['primaryname'] = related_resource['primaryname']
        if related_resource['entitytypeid'] == 'HERITAGE_PLACE.E27':
            print JSONResponse(related_resource, indent=4)
            for entity in related_resource['child_entities']:
                if entity['entitytypeid'] == 'NAME.E41':
                    related_res['identifier'].append(entity['value'])
        elif related_resource['entitytypeid'] == 'HERITAGE_FEATURE.E24':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'INTERPRETATION_TYPE.I4':
                    related_res['identifier'].append(
                        get_preflabel_from_valueid(entity['value'],
                                                   lang)['value'])
        elif related_resource['entitytypeid'] == 'HERITAGE_COMPONENT.B2':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'COMPONENT_TYPE.E55':
                    related_res['identifier'].append(
                        get_preflabel_from_valueid(entity['value'],
                                                   lang)['value'])
        elif related_resource['entitytypeid'] == 'ACTIVITY.E7':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'ACTIVITY_TYPE.E55':
                    related_res['relationship'].append(
                        get_preflabel_from_valueid(entity['value'],
                                                   lang)['value'])
        elif related_resource['entitytypeid'] == 'ACTOR.E39':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'ACTOR_TYPE.E55':
                    related_res['relationship'].append(
                        get_preflabel_from_conceptid(entity['conceptid'],
                                                     lang)['value'])
                    related_resource['actor_relationshiptype'] = ''
            for entity in related_resource['child_entities']:
                if entity['entitytypeid'] == 'ACTOR_APPELLATION.E82':
                    related_resource['primaryname'] = entity['value']
        elif related_resource['entitytypeid'] == 'HISTORICAL_EVENT.E5':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'HISTORICAL_EVENT_TYPE.E55':
                    related_res['relationship'].append(
                        get_preflabel_from_conceptid(entity['conceptid'],
                                                     lang)['value'])
        elif related_resource['entitytypeid'] == 'INFORMATION_RESOURCE.E73':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'INFORMATION_RESOURCE_TYPE.E55':
                    related_res['relationship'].append(
                        get_preflabel_from_valueid(entity['value'],
                                                   lang)['value'])

            for entity in related_resource['child_entities']:
                if entity['entitytypeid'] == 'FILE_PATH.E62':
                    related_res[
                        'file_path'] = settings.MEDIA_URL + entity['label']
                if entity['entitytypeid'] == 'THUMBNAIL.E62':
                    related_res[
                        'thumbnail'] = settings.MEDIA_URL + entity['label']
                    information_resource_type = 'IMAGE'
                if entity['entitytypeid'] == 'TILE_SQUARE_DETAILS.E44' or entity[
                        'entitytypeid'] == 'TILE_SQUARE_APPELLATION.E44':  #If this node is populated, the Info resource is assumed to be a Map and its default name is set to Sheet Name
                    related_res['primaryname'] = entity['label']
                    information_resource_type = 'MAP'
                elif entity['entitytypeid'] == 'SHARED_DATA_SOURCE_APPELLATION.E82' or entity[
                        'entitytypeid'] == 'SHARED_DATA_SOURCE_AFFILIATION.E82' or entity[
                            'entitytypeid'] == 'SHARED_DATA_SOURCE_CREATOR_APPELLATION.E82':  #If this node is populated, the Info resource is assumed to be a Shared Dataset and its default name is set to Shared Dated Source
                    SharedDataset.append(entity['label'])
                    information_resource_type = 'SHARED'
                elif entity[
                        'entitytypeid'] == 'CATALOGUE_ID.E42':  #If this node is populated, the Info resource is assumed to be Imagery other than VirtualGlobe type
                    OtherImageryName.append(entity['label'])
                    OtherImagery = False
                    information_resource_type = 'SATELLITE'
                elif entity[
                        'entitytypeid'] == 'IMAGERY_CREATOR_APPELLATION.E82':  #If this node is populated, and Catalogue_ID.E42 is not (checked by bool OtherImagery), the Info resource is assumed to be a VirtualGlobe
                    VirtualGlobe = True
                    VirtualGlobeName.append(entity['label'])
                    information_resource_type = 'SATELLITE'
                elif entity['entitytypeid'] == 'TITLE.E41':
                    related_res['primaryname'] = entity['value']

            for entity in related_resource['dates']:
                if entity['entitytypeid'] == 'DATE_OF_ACQUISITION.E50':
                    related_res['date'] = validatedates(entity['label'])
            if VirtualGlobe == True and OtherImagery == True:  #This routine creates the concatenated primary name for a Virtual Globe related resource
                for entity in related_resource['domains']:
                    if entity['entitytypeid'] == 'IMAGERY_SOURCE_TYPE.E55':
                        VirtualGlobeName.append(entity['label'])
                for entity in related_resource['dates']:
                    if entity['entitytypeid'] == 'DATE_OF_ACQUISITION.E50':
                        VirtualGlobeName.append(entity['label'])
                related_res['primaryname'] = " - ".join(VirtualGlobeName)
            elif OtherImagery == False:  #This routine creates the concatenated primary name for Imagery related resource
                for entity in related_resource['dates']:
                    if entity['entitytypeid'] == 'DATE_OF_ACQUISITION.E50':
                        OtherImageryName.append(entity['label'])
                related_res['primaryname'] = " - ".join(OtherImageryName)
            if information_resource_type == 'SHARED':  #This routine creates the concatenated primary name for a Shared dataset
                related_res['primaryname'] = " - ".join(SharedDataset)
        # get the relationship between the two entities as well as the notes and dates, if the exist

        for relationship in related_resource_info['resource_relationships']:
            if relationship['entityid1'] == related_resource[
                    'entityid'] or relationship[
                        'entityid2'] == related_resource['entityid']:
                related_res['relationship'].append(
                    get_preflabel_from_valueid(
                        relationship['relationshiptype'], lang)['value'])
                if relationship['datestarted']:
                    related_res['datefrom'] = relationship['datestarted']
                if relationship['dateended']:
                    related_res['dateto'] = relationship['dateended']
                if relationship['notes']:
                    related_res['notes'] = relationship['notes']
        entitytypeidkey = related_resource['entitytypeid'].split('.')[0]
        if entitytypeidkey == 'INFORMATION_RESOURCE':
            entitytypeidkey = '%s_%s' % (entitytypeidkey,
                                         information_resource_type)
        related_resource_dict[entitytypeidkey].append(related_res)
    return render_to_response(
        'resource-report.htm',
        {
            'geometry': JSONSerializer().serialize(result),
            #             'geometry': JSONSerializer().serialize(report_info['source']['geometry']),
            'resourceid': resourceid,
            'report_template': 'views/reports/' + report_info['type'] + '.htm',
            'report_info': report_info,
            'related_resource_dict': related_resource_dict,
            'main_script': 'resource-report',
            'active_page': 'ResourceReport',
            'BingDates': getdates(report_info['source']['geometry']
                                  )  # Retrieving the dates of Bing Imagery
        },
        context_instance=RequestContext(request))
Exemplo n.º 36
0
def get_preflabel_from_valueid(valueid, lang):
    se = SearchEngineFactory().create()
    concept_label = se.search(index='strings', doc_type='concept', id=valueid)
    if concept_label['found']:
        return get_preflabel_from_conceptid(
            get_concept_label_from_valueid(valueid)['conceptid'], lang)
Exemplo n.º 37
0
def report(request, resourceid):
    logging.warning("Viewing Report. User=%s", request.user)

    # Redirect non-logged-in users to the login screen
    if request.user.is_anonymous:
	redirect('/auth')

    lang = request.GET.get('lang', request.LANGUAGE_CODE)
    page = request.GET.get('page', 1)
    se = SearchEngineFactory().create()
    report_info = se.search(index='resource', id=resourceid)
    primaryname = se.search(index='entity', id = resourceid)
    report_info['source'] = report_info['_source']
    report_info['type'] = report_info['_type']
    report_info['source']['graph'] = report_info['source']['graph']
    if primaryname['_source']['primaryname']:
        report_info['source']['primaryname'] = primaryname['_source']['primaryname'] 
    del report_info['_source']
    del report_info['_type']            
    geometry = JSONSerializer().serialize(report_info['source']['geometry'])
    GeoCrypt = Crypter(settings.ENCODING_KEY)
    iv, encrypted = GeoCrypt.encrypt(geometry, GeoCrypt.KEY)
    ciphertext = binascii.b2a_base64(encrypted).rstrip()
    if geometry !='null':
        result = {
          'editor': 'true' if 'edit' in request.user.user_groups else 'false',
          'key': GeoCrypt.KEY,
          'iv': iv,
          'ciphertext': ciphertext
          
        }
    else:
        result = None
    
    if report_info['type'] == "INFORMATION_RESOURCE.E73": # These clauses produce subtypes for Imagery, Shared Dataset and Cartography Info Resources, with the aim of producing different Report pages for each of these Resource Types
        report_info['subtype'] = ''
        report_info['filepath'] =''
        report_info['has_image'] =''
        if 'ACQUISITION_ASSIGNMENT_E17' in report_info['source']['graph'] or 'CATALOGUE_ID_E42' in report_info['source']['graph']:
            report_info['subtype'] = 'Imagery'
        if 'RESOURCE_CREATION_EVENT_E65' in report_info['source']['graph']:
            for value in report_info['source']['graph']['RESOURCE_CREATION_EVENT_E65']:
                if 'CREATOR_E39' in value:
                    for subvalue in value['CREATOR_E39']:
                        if 'IMAGERY_CREATOR_APPELLATION_E82' in subvalue:
                            report_info['subtype'] = 'Imagery'
                        elif 'SHARED_DATA_SOURCE_CREATOR_APPELLATION_E82' in subvalue:
                            report_info['subtype'] = 'Shared'
                if 'SHARED_DATA_SOURCE_SHARER_E39' in value:
                    report_info['subtype'] = 'Shared'
        if 'PUBLICATION_EVENT_E12' in report_info['source']['graph']:
            for value in report_info['source']['graph']['PUBLICATION_EVENT_E12']:
                if 'PUBLICATION_ASSIGNMENT_E17' in value:
                    for subvalue in value['PUBLICATION_ASSIGNMENT_E17']:
                        if 'TILE_SQUARE_APPELLATION_E44' in subvalue or 'TILE_SQUARE_DETAILS_E44' in subvalue:
                            report_info['subtype'] = 'Cartography'
                        elif 'IMAGERY_SOURCE_TYPE_E55' in subvalue:
                            report_info['subtype'] = 'Imagery'
        if 'FILE_PATH_E62' in report_info['source']['graph']:
            report_info['filepath'] = report_info['source']['graph']['FILE_PATH_E62'][0]
        if 'THUMBNAIL_E62' in report_info['source']['graph']:
            report_info['has_image'] = report_info['source']['graph']['THUMBNAIL_E62'][0]
        if 'URL_E51' in report_info['source']['graph']: #If the resource has a URL, it verifies that it is an APAAME json string, in which case it retrieves the url of the photo from the json string and passes it to the report
            flickr_feed = report_info['source']['graph']['URL_E51'][0]['URL_E51__value'][:-1] if report_info['source']['graph']['URL_E51'][0]['URL_E51__value'].endswith('/') else report_info['source']['graph']['URL_E51'][0]['URL_E51__value']
            try:
                response = urllib.urlopen('https://www.flickr.com/services/oembed?url='+flickr_feed+'&format=json')          
                data = response.read().decode("utf-8")
                flickr_feed = json.loads(data)
                report_info['filepath'], report_info['has_image'] = flickr_feed['url'], True
            except:
                pass
                
    def get_evaluation_path(valueid):
        value = models.Values.objects.get(pk=valueid)
        concept_graph = Concept().get(id=value.conceptid_id, include_subconcepts=False, 
            include_parentconcepts=True, include_relatedconcepts=False, up_depth_limit=None, lang=lang)
        
        paths = []
        for path in concept_graph.get_paths(lang=lang)[0]:
            if path['label'] != 'Arches' and path['label'] != 'Evaluation Criteria Type':
                paths.append(path['label'])
        return '; '.join(paths)


    concept_label_ids = set()
    uuid_regex = re.compile('[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}')
    # gather together all uuid's referenced in the resource graph
    def crawl(items):
        for item in items:
            for key in item:
                if isinstance(item[key], list):
                    crawl(item[key])
                else:
                    if isinstance(item[key], basestring) and uuid_regex.match(item[key]):
                        if key == 'EVALUATION_CRITERIA_TYPE_E55__value':
                            item[key] = get_evaluation_path(item[key])
                        concept_label_ids.add(item[key])

    crawl([report_info['source']['graph']])

    # get all the concept labels from the uuid's
    concept_labels = se.search(index='concept_labels', id=list(concept_label_ids))

    # convert all labels to their localized prefLabel
    temp = {}
    if concept_labels != None:
        for concept_label in concept_labels['docs']:
            #temp[concept_label['_id']] = concept_label
            if concept_label['found']:
                # the resource graph already referenced the preferred label in the desired language
                if concept_label['_source']['type'] == 'prefLabel' and concept_label['_source']['language'] == lang:
                    temp[concept_label['_id']] = concept_label['_source']
                else: 
                    # the resource graph referenced a non-preferred label or a label not in our target language, so we need to get the right label
                    temp[concept_label['_id']] = get_preflabel_from_conceptid(concept_label['_source']['conceptid'], lang)

    # replace the uuid's in the resource graph with their preferred and localized label                    
    def crawl_again(items):
        for item in items:
            for key in item:
                if isinstance(item[key], list):
                    crawl_again(item[key])
                else:
                    if isinstance(item[key], basestring) and uuid_regex.match(item[key]):
                        try:
                            item[key] = temp[item[key]]['value']
                        except:
                            pass

    crawl_again([report_info['source']['graph']])

    #return JSONResponse(report_info, indent=4)

    related_resource_dict = {
        'HERITAGE_RESOURCE': [],
        'HERITAGE_RESOURCE_GROUP': [],
        'ACTIVITY': [],
        'ACTOR': [],
        'HISTORICAL_EVENT': [],
        'INFORMATION_RESOURCE_IMAGE': [],
        'INFORMATION_RESOURCE_DOCUMENT': [],
        'INFORMATION_RESOURCE_MAP': [],
        'INFORMATION_RESOURCE_SATELLITE': [],
        'INFORMATION_RESOURCE_SHARED': []
    }

    related_resource_info = get_related_resources(resourceid, lang) 
    # parse the related entities into a dictionary by resource type
    for related_resource in related_resource_info['related_resources']:
        VirtualGlobeName = []
        OtherImageryName = []
        SharedDataset = []
        VirtualGlobe = False
        OtherImagery = True
        information_resource_type = 'DOCUMENT'
        related_resource['relationship'] = []
        related_resource['datefrom'] = []
        related_resource['dateto'] = []
        related_resource['notes'] = []
        related_resource['date'] = []
        if related_resource['entitytypeid'] == 'HERITAGE_RESOURCE.E18':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'RESOURCE_TYPE_CLASSIFICATION.E55':
                    related_resource['relationship'].append(get_preflabel_from_valueid(entity['value'], lang)['value'])
        elif related_resource['entitytypeid'] == 'HERITAGE_RESOURCE_GROUP.E27':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'SITE_FUNCTION_TYPE.E55':
                    related_resource['relationship'].append(get_preflabel_from_valueid(entity['value'], lang)['value'])
        elif related_resource['entitytypeid'] == 'ACTIVITY.E7':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'ACTIVITY_TYPE.E55':
                    related_resource['relationship'].append(get_preflabel_from_valueid(entity['value'], lang)['value'])
        elif related_resource['entitytypeid'] == 'ACTOR.E39':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'ACTOR_TYPE.E55':
                    related_resource['relationship'].append(get_preflabel_from_conceptid(entity['conceptid'], lang)['value'])
                    related_resource['actor_relationshiptype'] = ''
            for entity in related_resource['child_entities']:
                if entity['entitytypeid'] == 'ACTOR_APPELLATION.E82':
                    related_resource['primaryname'] = entity['value']
        elif related_resource['entitytypeid'] == 'HISTORICAL_EVENT.E5':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'HISTORICAL_EVENT_TYPE.E55':
                    related_resource['relationship'].append(get_preflabel_from_conceptid(entity['conceptid'], lang)['value'])
        elif related_resource['entitytypeid'] == 'INFORMATION_RESOURCE.E73':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'INFORMATION_RESOURCE_TYPE.E55':                            
                    related_resource['relationship'].append(get_preflabel_from_valueid(entity['value'], lang)['value'])
  
            for entity in related_resource['child_entities']:
                if entity['entitytypeid'] == 'FILE_PATH.E62':
                    related_resource['file_path'] = settings.MEDIA_URL + entity['label']
                if entity['entitytypeid'] == 'THUMBNAIL.E62':
                    related_resource['thumbnail'] = settings.MEDIA_URL + entity['label']
                    information_resource_type = 'IMAGE'
                if entity['entitytypeid'] == 'TILE_SQUARE_DETAILS.E44' or entity['entitytypeid'] == 'TILE_SQUARE_APPELLATION.E44': #If this node is populated, the Info resource is assumed to be a Map and its default name is set to Sheet Name
                    related_resource['primaryname'] = entity['label']
                    information_resource_type = 'MAP'                      
                elif entity['entitytypeid'] == 'SHARED_DATA_SOURCE_APPELLATION.E82' or entity['entitytypeid'] == 'SHARED_DATA_SOURCE_AFFILIATION.E82' or entity['entitytypeid'] == 'SHARED_DATA_SOURCE_CREATOR_APPELLATION.E82': #If this node is populated, the Info resource is assumed to be a Shared Dataset and its default name is set to Shared Dated Source
                    SharedDataset.append(entity['label'])
                    information_resource_type = 'SHARED'        
                elif entity['entitytypeid'] == 'CATALOGUE_ID.E42': #If this node is populated, the Info resource is assumed to be Imagery other than VirtualGlobe type
                    OtherImageryName.append(entity['label'])
                    OtherImagery = False
                    information_resource_type = 'SATELLITE'
                elif entity['entitytypeid'] == 'IMAGERY_CREATOR_APPELLATION.E82': #If this node is populated, and Catalogue_ID.E42 is not (checked by bool OtherImagery), the Info resource is assumed to be a VirtualGlobe
                    VirtualGlobe = True
                    VirtualGlobeName.append(entity['label'])
                    information_resource_type = 'SATELLITE'
                elif entity['entitytypeid'] == 'TITLE.E41':
                    related_resource['primaryname'] = entity['value']

            for entity in related_resource['dates']:
                if entity['entitytypeid'] == 'DATE_OF_ACQUISITION.E50':                 
                    related_resource['date'] = validatedates(entity['label'])                                
            if VirtualGlobe == True and OtherImagery == True: #This routine creates the concatenated primary name for a Virtual Globe related resource
                for entity in related_resource['domains']:
                    if entity['entitytypeid'] == 'IMAGERY_SOURCE_TYPE.E55':
                        VirtualGlobeName.append(entity['label'])
                for entity in related_resource['dates']:
                    if entity['entitytypeid'] == 'DATE_OF_ACQUISITION.E50':
                        VirtualGlobeName.append(entity['label'])
                related_resource['primaryname'] = " - ".join(VirtualGlobeName)
            elif OtherImagery == False: #This routine creates the concatenated primary name for Imagery related resource
                for entity in related_resource['dates']:
                    if entity['entitytypeid'] == 'DATE_OF_ACQUISITION.E50':
                        OtherImageryName.append(entity['label'])
                related_resource['primaryname'] = " - ".join(OtherImageryName)
            if  information_resource_type == 'SHARED':  #This routine creates the concatenated primary name for a Shared dataset
                related_resource['primaryname'] = " - ".join(SharedDataset)
        # get the relationship between the two entities as well as the notes and dates, if the exist
        
        for relationship in related_resource_info['resource_relationships']:
            if relationship['entityid1'] == related_resource['entityid'] or relationship['entityid2'] == related_resource['entityid']: 
                related_resource['relationship'].append(get_preflabel_from_valueid(relationship['relationshiptype'], lang)['value'])
                if relationship['datestarted']: related_resource['datefrom'] = relationship['datestarted']
                if relationship['dateended']: related_resource['dateto'] = relationship['dateended']
                if relationship['notes']: related_resource['notes'] = relationship['notes']
        entitytypeidkey = related_resource['entitytypeid'].split('.')[0]
        if entitytypeidkey == 'INFORMATION_RESOURCE':
            entitytypeidkey = '%s_%s' % (entitytypeidkey, information_resource_type)
        related_resource_dict[entitytypeidkey].append(related_resource)

    return render_to_response('resource-report.htm', {
            'geometry': JSONSerializer().serialize(result),
#             'geometry': JSONSerializer().serialize(report_info['source']['geometry']),
            'resourceid': resourceid,
            'report_template': 'views/reports/' + report_info['type'] + '.htm',
            'report_info': report_info,
            'related_resource_dict': related_resource_dict,
            'main_script': 'resource-report',
            'active_page': 'ResourceReport',
            'BingDates': getdates(report_info['source']['geometry']) # Retrieving the dates of Bing Imagery
        },
        context_instance=RequestContext(request))        
Exemplo n.º 38
0
def get_preflabel_from_valueid(valueid, lang):
    se = SearchEngineFactory().create()
    concept_label = se.search(index='strings', doc_type='concept', id=valueid)
    if concept_label['found']:
        return get_preflabel_from_conceptid(get_concept_label_from_valueid(valueid)['conceptid'], lang)
Exemplo n.º 39
0
def get_concept_label_from_valueid(valueid):
    se = SearchEngineFactory().create()
    concept_label = se.search(index='concept_labels', id=valueid)
    if concept_label['found']:
        return concept_label['_source']
Exemplo n.º 40
0
def get_preflabel_from_valueid(valueid, lang):
    se = SearchEngineFactory().create()
    concept_label = se.search(index="concept_labels", id=valueid)
    if concept_label["found"]:
        return get_preflabel_from_conceptid(get_concept_label_from_valueid(valueid)["conceptid"], lang)
Exemplo n.º 41
0
def get_concept_label_from_valueid(valueid):
    se = SearchEngineFactory().create()
    concept_label = se.search(index='concept_labels', id=valueid)
    if concept_label['found']:
        return concept_label['_source']
Exemplo n.º 42
0
def report(request, resourceid):
    lang = request.GET.get('lang', settings.LANGUAGE_CODE)
    se = SearchEngineFactory().create()
    report_info = se.search(index='resource', id=resourceid)
    report_info['source'] = report_info['_source']
    report_info['type'] = report_info['_type']
    report_info['source']['graph'] = report_info['source']['graph']
    del report_info['_source']
    del report_info['_type']

    def get_evaluation_path(valueid):
        value = models.Values.objects.get(pk=valueid)
        concept_graph = Concept().get(id=value.conceptid_id,
                                      include_subconcepts=False,
                                      include_parentconcepts=True,
                                      include_relatedconcepts=False,
                                      up_depth_limit=None,
                                      lang=lang)

        paths = []
        for path in concept_graph.get_paths(lang=lang)[0]:
            if path['label'] != 'Arches' and path[
                    'label'] != 'Evaluation Criteria Type':
                paths.append(path['label'])
        return '; '.join(paths)

    current_status = 'None'
    user_can_edit_document = False
    resourcetypeid = report_info['type']

    # Pravice preverjamo zaenkrat le preko grup
    # Uporabnik mora imeti dodeljeno grupo z nazivom tipa resourca
    print request.user.username
    if (request.user.username != 'anonymous'):
        user = User.objects.get(username=request.user.username)
        user_groups = user.groups.values_list('name', flat=True)

        for entity in report_info['source']['graph']:
            if entity == 'EW_STATUS_E55':
                print report_info['source']['graph']["EW_STATUS_E55"]
                for value in report_info['source']['graph']["EW_STATUS_E55"]:
                    current_status = value["EW_STATUS_E55__label"]
        print "Current status for report: "
        print current_status
        user_can_edit_document = get_user_can_edit_document(
            current_status, 'same_group', user, resourcetypeid, user_groups,
            'same_group')

    else:
        user_groups = []
        for entity in report_info['source']['graph']:
            if entity == 'EW_STATUS_E55':
                print report_info['source']['graph']["EW_STATUS_E55"]
                for value in report_info['source']['graph']["EW_STATUS_E55"]:
                    current_status = value["EW_STATUS_E55__label"]
        if current_status != settings.PUBLISHED_LABEL:
            raise UserNotAuthorized(
                'Unauthenticated users can view only published resources!')

    concept_label_ids = set()
    uuid_regex = re.compile(
        '[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}'
    )

    # gather together all uuid's referenced in the resource graph
    def crawl(items):
        for item in items:
            for key in item:
                if isinstance(item[key], list):
                    crawl(item[key])
                else:
                    if uuid_regex.match(item[key]):
                        if key == 'EVALUATION_CRITERIA_TYPE_E55__value':
                            item[key] = get_evaluation_path(item[key])
                        concept_label_ids.add(item[key])

    crawl([report_info['source']['graph']])

    # get all the concept labels from the uuid's
    concept_labels = se.search(index='concept_labels',
                               id=list(concept_label_ids))

    # convert all labels to their localized prefLabel
    temp = {}
    if concept_labels != None:
        for concept_label in concept_labels['docs']:
            #temp[concept_label['_id']] = concept_label
            if concept_label['found']:
                # the resource graph already referenced the preferred label in the desired language
                if concept_label['_source'][
                        'type'] == 'prefLabel' and concept_label['_source'][
                            'language'] == lang:
                    temp[concept_label['_id']] = concept_label['_source']
                else:
                    # the resource graph referenced a non-preferred label or a label not in our target language, so we need to get the right label
                    temp[concept_label['_id']] = get_preflabel_from_conceptid(
                        concept_label['_source']['conceptid'], lang)

    # replace the uuid's in the resource graph with their preferred and localized label
    def crawl_again(items):
        for item in items:
            for key in item:
                if isinstance(item[key], list):
                    crawl_again(item[key])
                else:
                    if uuid_regex.match(item[key]):
                        try:
                            item[key] = temp[item[key]]['value']
                        except:
                            pass

    crawl_again([report_info['source']['graph']])
    # Podatke na visjih nivojih, ki zdruzujejo vec razlicnih nivojev, prestavimo na prvi nivo
    # To je potrebno zato, ker sicer ne moremo skrivati posameznih sklopov iz skupinske veje
    keys = [
        'REGION_E55', 'SETTLEMENT_TYPE_E55', 'CONTEXT_E55',
        'PLACE_ADDRESS_E45', 'PLACE_CADASTRAL_REFERENCE_E53',
        'ADMINISTRATIVE_SUBDIVISION_E48'
    ]
    old_key = 'PLACE_E53'
    for key in keys:
        if old_key in report_info['source']['graph']:
            for data in report_info['source']['graph'][old_key]:
                if key in data:
                    if key not in report_info['source']['graph']:
                        report_info['source']['graph'][key] = []
                    report_info['source']['graph'][key].append(
                        data.pop(key)[0])
    keys = ['SETTING_TYPE_E55', 'DESCRIPTION_OF_LOCATION_E62']
    old_key = 'PLACE_SITE_LOCATION_E53'
    old_key1 = 'PLACE_E53'
    for key in keys:
        if old_key1 in report_info['source']['graph']:
            for data in report_info['source']['graph'][old_key1]:
                if old_key in data:
                    if key in data[old_key][0]:
                        if key not in report_info['source']['graph']:
                            report_info['source']['graph'][key] = []
                        report_info['source']['graph'][key].append(
                            data[old_key][0].pop(key)[0])
    keys = ['DESCRIPTION_OF_LOCATION_E62']
    old_key = 'SPATIAL_COVERAGE_E53'
    old_key1 = 'PLACE_E53'
    for key in keys:
        if old_key1 in report_info['source']['graph']:
            for data in report_info['source']['graph'][old_key1]:
                if old_key in data:
                    if key in data[old_key][0]:
                        if key not in report_info['source']['graph']:
                            report_info['source']['graph'][key] = []
                        report_info['source']['graph'][key].append(
                            data[old_key][0].pop(key)[0])

    # PLY koncnico spremenimo za potrebe reporta v NXS
    if 'FILE_PATH_E62' in report_info['source']['graph']:
        report_info['source']['graph']['FILE_PATH_E62'][0][
            'FILE_PATH_E62__value'] = report_info['source']['graph'][
                'FILE_PATH_E62'][0]['FILE_PATH_E62__label'].replace(
                    ".ply", ".nxs")
        report_info['source']['graph']['FILE_PATH_E62'][0][
            'FILE_PATH_E62__value'] = report_info['source']['graph'][
                'FILE_PATH_E62'][0]['FILE_PATH_E62__value'].replace(
                    ".PLY", ".nxs")
        print 'Koncni path: ' + report_info['source']['graph'][
            'FILE_PATH_E62'][0]['FILE_PATH_E62__value']

    #print report_info['source']['graph']

    #return JSONResponse(report_info, indent=4)

    related_resource_dict = {
        'HERITAGE_RESOURCE': [],
        'HERITAGE_RESOURCE_GROUP': [],
        'ACTIVITY': [],
        'ACTOR': [],
        'HISTORICAL_EVENT': [],
        'INFORMATION_RESOURCE_IMAGE': [],
        'INFORMATION_RESOURCE_DOCUMENT': [],
        'INFORMATION_RESOURCE_JSC3D': [],
        'INFORMATION_RESOURCE_3DHOP': []
    }

    related_resource_info = get_related_resources(resourceid, lang)

    # parse the related entities into a dictionary by resource type
    for related_resource in related_resource_info['related_resources']:
        information_resource_type = 'DOCUMENT'
        related_resource['relationship'] = []
        if related_resource['entitytypeid'] == 'HERITAGE_RESOURCE.E18':
            for entity in related_resource['domains']:
                if entity[
                        'entitytypeid'] == 'RESOURCE_TYPE_CLASSIFICATION.E55':
                    related_resource['relationship'].append(
                        get_preflabel_from_valueid(entity['value'],
                                                   lang)['value'])
        elif related_resource['entitytypeid'] == 'HERITAGE_RESOURCE_GROUP.E27':
            for entity in related_resource['domains']:
                if entity[
                        'entitytypeid'] == 'RESOURCE_TYPE_CLASSIFICATION.E55':
                    related_resource['relationship'].append(
                        get_preflabel_from_valueid(entity['value'],
                                                   lang)['value'])
        elif related_resource['entitytypeid'] == 'ACTIVITY.E7':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'ACTIVITY_TYPE.E55':
                    related_resource['relationship'].append(
                        get_preflabel_from_valueid(entity['value'],
                                                   lang)['value'])
        elif related_resource['entitytypeid'] == 'ACTOR.E39':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'ACTOR_TYPE.E55':
                    related_resource['relationship'].append(
                        get_preflabel_from_conceptid(entity['conceptid'],
                                                     lang)['value'])
                    related_resource['actor_relationshiptype'] = ''
        elif related_resource['entitytypeid'] == 'HISTORICAL_EVENT.E5':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'HISTORICAL_EVENT_TYPE.E55':
                    related_resource['relationship'].append(
                        get_preflabel_from_conceptid(entity['conceptid'],
                                                     lang)['value'])
        elif related_resource['entitytypeid'] == 'INFORMATION_RESOURCE.E73':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'INFORMATION_RESOURCE_TYPE.E55':
                    related_resource['relationship'].append(
                        get_preflabel_from_valueid(entity['value'],
                                                   lang)['value'])
            for entity in related_resource['child_entities']:
                if entity['entitytypeid'] == 'FILE_PATH.E62':
                    related_resource[
                        'file_path'] = settings.MEDIA_URL + entity['label']

                    related_resource[
                        'file_path'] = settings.MEDIA_URL + entity['label']
                    # PLY koncnico spremenimo za potrebe reporta v NXS
                    if related_resource['file_path'][-3:].lower() == 'ply':
                        related_resource['file_path'] = related_resource[
                            'file_path'].replace(".ply", ".nxs")
                        related_resource['file_path'] = related_resource[
                            'file_path'].replace(".PLY", ".nxs")
                        information_resource_type = '3DHOP'
                    if related_resource['file_path'][-3:].lower(
                    ) == 'stl' or related_resource['file_path'][-3:].lower(
                    ) == 'obj':
                        information_resource_type = 'JSC3D'

                if entity['entitytypeid'] == 'THUMBNAIL.E62':
                    related_resource[
                        'thumbnail'] = settings.MEDIA_URL + entity['label']
                    information_resource_type = 'IMAGE'

        # get the relationship between the two entities
        for relationship in related_resource_info['resource_relationships']:
            if relationship['entityid1'] == related_resource[
                    'entityid'] or relationship[
                        'entityid2'] == related_resource['entityid']:
                related_resource['relationship'].append(
                    get_preflabel_from_valueid(
                        relationship['relationshiptype'], lang)['value'])
                if relationship['notes'] != '':
                    related_resource['relationship'].append(
                        html2text.html2text(relationship['notes']))

        if len(related_resource['relationship']) > 0:
            related_resource['relationship'] = '(%s)' % (', '.join(
                related_resource['relationship']))
        else:
            related_resource['relationship'] = ''

        entitytypeidkey = related_resource['entitytypeid'].split('.')[0]
        if entitytypeidkey == 'INFORMATION_RESOURCE':
            entitytypeidkey = '%s_%s' % (entitytypeidkey,
                                         information_resource_type)
        related_resource_dict[entitytypeidkey].append(related_resource)

    resource_type_config = settings.RESOURCE_TYPE_CONFIGS()[resourcetypeid]
    return render_to_response('resource-report.htm', {
        'geometry':
        JSONSerializer().serialize(report_info['source']['geometry']),
        'resourceid':
        resourceid,
        'report_template':
        'views/reports/' + report_info['type'] + '.htm',
        'report_info':
        report_info,
        'related_resource_dict':
        related_resource_dict,
        'main_script':
        'resource-report',
        'active_page':
        'ResourceReport',
        'RESOURCE_TYPE_CONFIGS':
        resource_type_config,
        'user_groups':
        user_groups,
        'current_status':
        current_status,
        'user_can_edit_document':
        user_can_edit_document,
        'help':
        settings.HELP['report']
    },
                              context_instance=RequestContext(request))
Exemplo n.º 43
0
    def get_related_resources(self, lang='en-US', limit=settings.RELATED_RESOURCES_EXPORT_LIMIT, start=0, page=0):
        """
        Returns an object that lists the related resources, the relationship types, and a reference to the current resource

        """
        root_nodes = models.Node.objects.filter(istopnode=True)
        node_config_lookup = {}
        graphs = models.GraphModel.objects.all().exclude(pk=settings.SYSTEM_SETTINGS_RESOURCE_MODEL_ID).exclude(isresource=False)
        graph_lookup = {str(graph.graphid): {'name':graph.name, 'iconclass': graph.iconclass} for graph in graphs}
        for node in root_nodes:
            graph_id = unicode(node.graph_id)
            if node.config != None and graph_id in graph_lookup:
                node_config_lookup[graph_id] = node.config
                node_config_lookup[graph_id]['iconclass'] = graph_lookup[graph_id]['iconclass']
                node_config_lookup[graph_id]['name'] = graph_lookup[graph_id]['name']

        ret = {
            'resource_instance': self,
            'resource_relationships': [],
            'related_resources': [],
            'node_config_lookup': node_config_lookup
        }
        se = SearchEngineFactory().create()

        if page > 0:
            limit = settings.RELATED_RESOURCES_PER_PAGE
            start = limit*int(page-1)

        def get_relations(resourceinstanceid, start, limit):
            query = Query(se, start=start, limit=limit)
            bool_filter = Bool()
            bool_filter.should(Terms(field='resourceinstanceidfrom', terms=resourceinstanceid))
            bool_filter.should(Terms(field='resourceinstanceidto', terms=resourceinstanceid))
            query.add_query(bool_filter)
            return query.search(index='resource_relations', doc_type='all')

        resource_relations = get_relations(self.resourceinstanceid, start, limit)
        ret['total'] = resource_relations['hits']['total']
        instanceids = set()

        for relation in resource_relations['hits']['hits']:
            try:
                preflabel = get_preflabel_from_valueid(relation['_source']['relationshiptype'], lang)
                relation['_source']['relationshiptype_label'] = preflabel['value']
            except:
                relation['_source']['relationshiptype_label'] = relation['_source']['relationshiptype']

            ret['resource_relationships'].append(relation['_source'])
            instanceids.add(relation['_source']['resourceinstanceidto'])
            instanceids.add(relation['_source']['resourceinstanceidfrom'])
        if len(instanceids) > 0:
            instanceids.remove(str(self.resourceinstanceid))

        if len(instanceids) > 0:
            related_resources = se.search(index='resource', doc_type='_all', id=list(instanceids))
            if related_resources:
                for resource in related_resources['docs']:
                    relations = get_relations(resource['_id'], 0, 0)
                    resource['_source']['total_relations'] = relations['hits']['total']
                    ret['related_resources'].append(resource['_source'])
        return ret
Exemplo n.º 44
0
def arch_layer(request, boundtype=''):

    data = []
    geom_param = request.GET.get('geom', None)

    bbox = request.GET.get('bbox', '')
    limit = request.GET.get('limit', settings.MAP_LAYER_FEATURE_LIMIT)
    geojson_collection = {
      "type": "FeatureCollection",
      "features": []
    }

    se = SearchEngineFactory().create()
    query = Query(se, limit=limit)

    args = {
        'index':'resource',
        '_type':'ARCHAEOLOGICAL_ZONE.E53',
    }

    #data = query.search(**args)
    
    data = se.search(index='resource', doc_type='ARCHAEOLOGICAL_ZONE.E53')
    
    pre_collection = []
    
    for item in data['hits']['hits']:
        if "PLACE_E53" in item['_source']['graph']:
            for geom in item['_source']['graph']['PLACE_E53']:
                
                if "AREA_OF_PROBABILITY_GEOMETRY_E47" in geom:

                    wkt = geom['AREA_OF_PROBABILITY_GEOMETRY_E47'][0]['AREA_OF_PROBABILITY_GEOMETRY_E47__value']
                    g1 = shapely.wkt.loads(wkt)
                    g2 = geojson.Feature(geometry=g1, properties={})
                    
                    feat = {
                        'geometry':g2.geometry,
                        'type':"Feature",
                        'id':item['_source']['entityid'],
                        'properties':{
                            'type':geom['AREA_OF_PROBABILITY_GEOMETRY_E47'][0]['AREA_OF_PROBABILITY_GEOMETRY_TYPE_E55__label']
                        }
                    }

                    geojson_collection['features'].append(feat)

                if "ARCHAEOLOGICAL_ZONE_BOUNDARY_GEOMETRY_E47" in geom:
                    # for geom in item['_source']['graph']['PLACE_E53'][0]['ARCHAEOLOGICAL_ZONE_BOUNDARY_GEOMETRY_E47']:
                    wkt = geom['ARCHAEOLOGICAL_ZONE_BOUNDARY_GEOMETRY_E47'][0]['ARCHAEOLOGICAL_ZONE_BOUNDARY_GEOMETRY_E47__value']
                    # if item['_source']['primaryname'] == "Zone 7":
                        # print "yes"
                        # print wkt
                        
                    #print wkt
                    
                    g1 = shapely.wkt.loads(wkt)
                    g2 = geojson.Feature(geometry=g1, properties={})
                    #json_geom = g2.geometry
                    # feat_type = 'boundary'
                    # pre_collection.append((json_geom,feat_type))
                    feat = {
                        'geometry':g2.geometry,
                        'type':"Feature",
                        'id':item['_source']['entityid'],
                        'properties':{
                            'type':'boundary'
                        }
                    }
                    geojson_collection['features'].append(feat)
                
        if "SHOVEL_TEST_E7" in item['_source']['graph']:
            for st in item['_source']['graph']['SHOVEL_TEST_E7']:
                wkt = st['SHOVEL_TEST_GEOMETRY_E47'][0]['SHOVEL_TEST_GEOMETRY_E47__value']

                g1 = shapely.wkt.loads(wkt)
                g2 = geojson.Feature(geometry=g1, properties={})
                # json_geom = g2.geometry
                # feat_type = "shovel test"
                # pre_collection.append((json_geom,feat_type))
                feat = {
                    'geometry':g2.geometry,
                    'type':"Feature",
                    'id':item['_source']['entityid'],
                    'properties':{
                        'type':"shovel test"
                    }
                }
                geojson_collection['features'].append(feat)
    return JSONResponse(geojson_collection)
Exemplo n.º 45
0
def get_concept_label_from_valueid(valueid):
    se = SearchEngineFactory().create()
    concept_label = se.search(index="concept_labels", id=valueid)
    if concept_label["found"]:
        return concept_label["_source"]
Exemplo n.º 46
0
def report(request, resourceid):
    lang = request.GET.get('lang', settings.LANGUAGE_CODE)
    se = SearchEngineFactory().create()
    report_info = se.search(index='resource', id=resourceid)
    report_info['source'] = report_info['_source']
    report_info['type'] = report_info['_type']
    report_info['typename'] = settings.RESOURCE_TYPE_CONFIGS()[report_info['type']]['name']
    report_info['source']['graph'] = report_info['source']['graph']
    del report_info['_source']
    del report_info['_type']

    def get_evaluation_path(valueid):
        value = models.Values.objects.get(pk=valueid)
        concept_graph = Concept().get(id=value.conceptid_id, include_subconcepts=False, 
            include_parentconcepts=True, include_relatedconcepts=False, up_depth_limit=None, lang=lang)
        
        paths = []
        for path in concept_graph.get_paths(lang=lang)[0]:
            if path['label'] != 'Arches' and path['label'] != 'Evaluation Criteria Type':
                paths.append(path['label'])
        return '; '.join(paths)


    concept_label_ids = set()
    uuid_regex = re.compile('[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}')
    # gather together all uuid's referenced in the resource graph
    def crawl(items):
        for item in items:
            for key in item:
                if isinstance(item[key], list):
                    crawl(item[key])
                else:
                    if isinstance(item[key], basestring) and uuid_regex.match(item[key]):
                        if key == 'EVALUATION_CRITERIA_TYPE_E55__value':
                            item[key] = get_evaluation_path(item[key])
                        concept_label_ids.add(item[key])

    crawl([report_info['source']['graph']])

    # get all the concept labels from the uuid's
    concept_labels = se.search(index='concept_labels', id=list(concept_label_ids))

    # convert all labels to their localized prefLabel
    temp = {}
    if concept_labels != None:
        for concept_label in concept_labels['docs']:
            #temp[concept_label['_id']] = concept_label
            if concept_label['found']:
                # the resource graph already referenced the preferred label in the desired language
                if concept_label['_source']['type'] == 'prefLabel' and concept_label['_source']['language'] == lang:
                    temp[concept_label['_id']] = concept_label['_source']
                else: 
                    # the resource graph referenced a non-preferred label or a label not in our target language, so we need to get the right label
                    temp[concept_label['_id']] = get_preflabel_from_conceptid(concept_label['_source']['conceptid'], lang)

    # replace the uuid's in the resource graph with their preferred and localized label                    
    def crawl_again(items):
        for item in items:
            for key in item:
                if isinstance(item[key], list):
                    crawl_again(item[key])
                else:
                    if isinstance(item[key], basestring) and uuid_regex.match(item[key]):
                        try:
                            item[key] = temp[item[key]]['value']
                        except:
                            pass

    crawl_again([report_info['source']['graph']])

    #return JSONResponse(report_info, indent=4)

    related_resource_dict = {
        'INVENTORY_RESOURCE': [],
        'CHARACTER_AREA': [],
        'MASTER_PLAN_ZONE': [],
        'ARCHAEOLOGICAL_ZONE': [],
        'HISTORIC_AREA': [],
        'ACTOR': [],
        'INFORMATION_RESOURCE_DOCUMENT': [],
        'INFORMATION_RESOURCE_IMAGE': [],
        'ACTIVITY_A': [],
        'ACTIVITY_B': []  
    }

    allowedtypes = get_allowed_types(request)
    anon = request.user.username == "anonymous"
    related_resource_info = get_related_resources(resourceid, lang, allowedtypes=allowedtypes,is_anon=anon)

    # parse the related entities into a dictionary by resource type
    for related_resource in related_resource_info['related_resources']:
        information_resource_type = 'DOCUMENT'
        related_resource['relationship'] = []
        if related_resource['entitytypeid'] == 'INVENTORY_RESOURCE.E18':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'RESOURCE_TYPE_CLASSIFICATION.E55':
                    related_resource['relationship'].append(get_preflabel_from_valueid(entity['value'], lang)['value'])
        elif related_resource['entitytypeid'] == 'CHARACTER_AREA.E53':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'RESOURCE_TYPE_CLASSIFICATION.E55':
                    related_resource['relationship'].append(get_preflabel_from_valueid(entity['value'], lang)['value'])
        elif related_resource['entitytypeid'] == 'MASTER_PLAN_ZONE.E53':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'ACTIVITY_TYPE.E55':
                    related_resource['relationship'].append(get_preflabel_from_valueid(entity['value'], lang)['value'])
        elif related_resource['entitytypeid'] == 'ARCHAEOLOGICAL_ZONE.E53':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'ACTOR_TYPE.E55':
                    related_resource['relationship'].append(get_preflabel_from_conceptid(entity['conceptid'], lang)['value'])
                    related_resource['actor_relationshiptype'] = ''
        elif related_resource['entitytypeid'] == 'HISTORIC_AREA.E53':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'HISTORICAL_EVENT_TYPE.E55':
                    related_resource['relationship'].append(get_preflabel_from_conceptid(entity['conceptid'], lang)['value'])
        elif related_resource['entitytypeid'] == 'ACTOR.E39':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'ACTOR_TYPE.E55':
                    related_resource['relationship'].append(get_preflabel_from_conceptid(entity['conceptid'], lang)['value'])
                    related_resource['actor_relationshiptype'] = ''
        elif related_resource['entitytypeid'] == 'ACTIVITY_A.E7':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'HISTORICAL_EVENT_TYPE.E55':
                    related_resource['relationship'].append(get_preflabel_from_conceptid(entity['conceptid'], lang)['value'])
        elif related_resource['entitytypeid'] == 'ACTIVITY_B.E7':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'ACTIVITY_B.E7':
                    related_resource['relationship'].append(get_preflabel_from_conceptid(entity['conceptid'], lang)['value'])
        elif related_resource['entitytypeid'] == 'INFORMATION_RESOURCE.E73':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'INFORMATION_RESOURCE_TYPE.E55':
                    related_resource['relationship'].append(get_preflabel_from_valueid(entity['value'], lang)['value'])
            for entity in related_resource['child_entities']:
                if entity['entitytypeid'] == 'FILE_PATH.E62':
                    related_resource['file_path'] = settings.MEDIA_URL + entity['label']
                if entity['entitytypeid'] == 'THUMBNAIL.E62':
                    related_resource['thumbnail'] = settings.MEDIA_URL + entity['label']
                    information_resource_type = 'IMAGE'
            
        # get the relationship between the two entities
        for relationship in related_resource_info['resource_relationships']:
            if relationship['entityid1'] == related_resource['entityid'] or relationship['entityid2'] == related_resource['entityid']: 
                related_resource['relationship'].append(get_preflabel_from_valueid(relationship['relationshiptype'], lang)['value'])

        entitytypeidkey = related_resource['entitytypeid'].split('.')[0]
        if entitytypeidkey == 'INFORMATION_RESOURCE':
            entitytypeidkey = '%s_%s' % (entitytypeidkey, information_resource_type)
        related_resource_dict[entitytypeidkey].append(related_resource)

    # set boolean to trigger display of related resource graph
    related_resource_flag = False
    for k,v in related_resource_dict.iteritems():
        if len(v) > 0:
            related_resource_flag = True
            break
            
    
    
    # create a few log files to help with debugging
    if settings.DEBUG:
        related_dict_log_path = os.path.join(settings.PACKAGE_ROOT,'logs','current_related_resource_dict.log')
        with open(related_dict_log_path,"w") as log:
            print >> log, json.dumps(related_resource_dict, sort_keys=True,indent=2, separators=(',', ': '))
            
        related_info_log_path = os.path.join(settings.PACKAGE_ROOT,'logs','current_related_resource_info.log')
        with open(related_info_log_path,"w") as log:
            print >> log, json.dumps(related_resource_info, sort_keys=True,indent=2, separators=(',', ': '))
            
        graph_log_path = os.path.join(settings.PACKAGE_ROOT,'logs','current_graph.json')
        with open(graph_log_path,"w") as log:
            print >> log, json.dumps(report_info['source']['graph'], sort_keys=True,indent=2, separators=(',', ': '))
            
    response_dict = {
        'geometry': JSONSerializer().serialize(report_info['source']['geometry']),
        'resourceid': resourceid,
        'report_template': 'views/reports/' + report_info['type'] + '.htm',
        'report_info': report_info,
        'related_resource_dict': related_resource_dict,
        'related_resource_flag': related_resource_flag,
        'main_script': 'resource-report',
        'active_page': 'ResourceReport'
    }
    
    # mine the result for specific geometry in certain cases and add to response dictionary
    if report_info['type'] == "ACTIVITY_A.E7":
        pa_geoms, ape_geoms = [], []
        if "PLACE_E53" in report_info['source']['graph']:
            for place in report_info['source']['graph']['PLACE_E53']:
                if "SPATIAL_COORDINATES_GEOMETRY_E47" in place:
                    wkt = place['SPATIAL_COORDINATES_GEOMETRY_E47'][0]['SPATIAL_COORDINATES_GEOMETRY_E47__value']
                    
                    g1 = shapely.wkt.loads(wkt)
                    g2 = geojson.Feature(geometry=g1, properties={})
                    json_geom = g2.geometry
                    
                    feat_type = place['SPATIAL_COORDINATES_GEOMETRY_E47'][0]['ACTIVITY_GEOMETRY_TYPE_E55__label']
                    if feat_type == "Project Area":
                        pa_geoms.append(json_geom)
                    if feat_type == "Area of Potential Effect":
                        ape_geoms.append(json_geom)
                    
                    
        if len(pa_geoms) > 0:
            pa_dict = {'type':'GeometryCollection','geometries':pa_geoms}
            response_dict['pa_geom'] = JSONSerializer().serialize(pa_dict)
        else:
            response_dict['pa_geom'] = 'null'
        if len(ape_geoms) > 0:
            ape_dict = {'type':'GeometryCollection','geometries':ape_geoms}
            response_dict['ape_geom'] = JSONSerializer().serialize(ape_dict)
        else:
            response_dict['ape_geom'] = 'null'
            
    if report_info['type'] == "ARCHAEOLOGICAL_ZONE.E53":
        bounds = []
        probs = {
            "Historic Resources":[],
            "Native American Resources":[],
            "Paleosols":[],
            "Disturbed Area":[]
        }
        if "PLACE_E53" in report_info['source']['graph']:
            for place in report_info['source']['graph']['PLACE_E53']:
                if "AREA_OF_PROBABILITY_GEOMETRY_E47" in place:
                    wkt = place['AREA_OF_PROBABILITY_GEOMETRY_E47'][0]['AREA_OF_PROBABILITY_GEOMETRY_E47__value']
                    
                    g1 = shapely.wkt.loads(wkt)
                    g2 = geojson.Feature(geometry=g1, properties={})
                    json_geom = g2.geometry
                    
                    feat_type = place['AREA_OF_PROBABILITY_GEOMETRY_E47'][0]['AREA_OF_PROBABILITY_GEOMETRY_TYPE_E55__label']
                    if feat_type in probs.keys():
                        probs[feat_type].append(json_geom)
                        
                if "ARCHAEOLOGICAL_ZONE_BOUNDARY_GEOMETRY_E47" in place:
                    wkt = place['ARCHAEOLOGICAL_ZONE_BOUNDARY_GEOMETRY_E47'][0]['ARCHAEOLOGICAL_ZONE_BOUNDARY_GEOMETRY_E47__value']
                    
                    g1 = shapely.wkt.loads(wkt)
                    g2 = geojson.Feature(geometry=g1, properties={})
                    json_geom = g2.geometry
                    
                    bounds.append(json_geom)
        
        hr_dict = {'type':'GeometryCollection','geometries':probs["Historic Resources"]}
        response_dict['prob_hr'] = JSONSerializer().serialize(hr_dict)
        na_dict = {'type':'GeometryCollection','geometries':probs["Native American Resources"]}
        response_dict['prob_na'] = JSONSerializer().serialize(na_dict)
        p_dict = {'type':'GeometryCollection','geometries':probs["Paleosols"]}
        response_dict['prob_p'] = JSONSerializer().serialize(p_dict)
        da_dict = {'type':'GeometryCollection','geometries':probs["Disturbed Area"]}
        response_dict['prob_da'] = JSONSerializer().serialize(da_dict)
        bounds_dict = {'type':'GeometryCollection','geometries':bounds}
        response_dict['geometry'] = JSONSerializer().serialize(bounds_dict)
    
    ## THIS WAS AN ILL-FATED ATTEMPT TO NAME NAMES TO THE GEOMETRIES, NO WAIT, FEATURES, THAT ARE PASSED TO THE REPORTS
    ## FIX IF STATEMENT TO TRY AGAIN
    if report_info['type'] == "FIELD_INVLESTIGATION.E7":
        # return a FeatureCollection instead of a GeometryCollection for the field investigation report
        features = []
        if "SHOVEL_TEST_E7" in report_info['source']['graph']:
            for place in report_info['source']['graph']['SHOVEL_TEST_E7']:
                if "TEST_PIT_LOCATIONS_GEOMETRY_E47" in place:
                    wkt = place['TEST_PIT_LOCATIONS_GEOMETRY_E47'][0]['TEST_PIT_LOCATIONS_GEOMETRY_E47__value']
                    try:
                        feat_name = place['TEST_PIT_LOCATIONS_GEOMETRY_E47'][0]['SHOVEL_TEST_ID_E42__label']
                    except:
                        feat_name = ""
                    
                    g1 = shapely.wkt.loads(wkt)
                    g2 = geojson.Feature(geometry=g1, properties={"name":feat_name})
                    features.append(g2)
                    # print json.dumps(g2,indent=2)
                    # json_geom = g2.geometry
                    # json_geom['properties'] = {"name":feat_name}

                    # points.append(json_geom)
                    
        # print json.dumps(points[0],indent=2)
        points_dict = {'type':'FeatureCollection','features':features}
        response_dict['geometry'] = JSONSerializer().serialize(points_dict)
    
    return render_to_response('resource-report.htm', response_dict,
        context_instance=RequestContext(request))        
Exemplo n.º 47
0
    def __init__(self, *args, **kwargs):
        super(Resource, self).__init__(*args, **kwargs)
        description_group = {
            'id': 'resource-description',
            'icon':'fa-folder',
            'name': _('Resource Description'),
            'forms': []
        }
        review_group = {
            'id': 'resource-reviews',
            'icon':'fa-clipboard',
            'name': _('Reviews'),
            'forms': []
        }
        manage_group = {
            'id': 'manage-resource',
            'icon': 'fa-wrench',
            'name': _('Manage Resource'),
            'forms': [
                EditHistory.get_info(),
                DeleteResourceForm.get_info()
            ]
        }

        if self.entitytypeid == 'INVENTORY_RESOURCE.E18':
            description_group['forms'] = [
                summary.InventorySummaryForm.get_info(), 
                description.InventoryDescriptionForm.get_info(),
                other.FunctionAndUseForm.get_info(),
                other.FormDimensionForm.get_info(),
                other.ComponentForm.get_info(),
                location.LocationForm.get_info(),
                wizard.ConditionForm.get_info(),
                wizard.RelatedFilesForm.get_info(),
                other.InventoryEvaluationForm.get_info(),
                other.ExternalReferenceForm.get_info(),
            ]

        if self.entitytypeid == 'CHARACTER_AREA.E53':
            description_group['forms'] = [
                summary.CharAreaSummaryForm.get_info(),
                description.CharAreaDescriptionForm.get_info(),
                other.CharAreaGuidelinesForm.get_info(),
                location.SimpleLocationForm.get_info(),
            ]
            
        if self.entitytypeid == 'MASTER_PLAN_ZONE.E53':
            description_group['forms'] = [
                summary.MPZoneSummaryForm.get_info(),
                description.InventoryDescriptionForm.get_info(),
                location.SimpleLocationForm.get_info(),
                wizard.MPZoneGuidelinesForm.get_info(),
            ]
            
        if self.entitytypeid == 'ARCHAEOLOGICAL_ZONE.E53':
            description_group['forms'] = [
                summary.ArchZoneSummaryForm.get_info(),
                location.SimpleLocationForm.get_info(),
                location.ProbabilityAreaForm.get_info(),
                ## deprecated in favor of new investigation locations resource type
                # wizard.ArchZoneInvestigationForm.get_info(),
                # location.InvestigationLocationForm.get_info(),
            ]
            
        if self.entitytypeid == 'FIELD_INVESTIGATION.E7':
            description_group['forms'] = [
                summary.InvestSummaryForm.get_info(),
                location.InvestLocationForm.get_info(),
                other.InvestAssessmentForm.get_info(),
                wizard.FileUploadForm.get_info(),
                other.InvestRecForm.get_info(),
                
            ]
            
        if self.entitytypeid == 'HISTORIC_AREA.E53':
            description_group['forms'][:0] = [
                summary.DesSummaryForm.get_info(),
                description.DesDescriptionForm.get_info(),
                location.SimpleLocationForm.get_info(),
                other.DesEvaluationForm.get_info(),
            ]

        if self.entitytypeid == 'ACTOR.E39':
            description_group['forms'] = [
                summary.ActorSummaryForm.get_info(), 
                description.InventoryDescriptionForm.get_info(),
                location.LocationForm.get_info(),
                wizard.RelatedFilesForm.get_info(),
                other.ExternalReferenceForm.get_info()
            ]

        if self.entitytypeid == 'INFORMATION_RESOURCE.E73':
            description_group['forms'] = [
                summary.InformationResourceSummaryForm.get_info(), 
                other.PublicationForm.get_info(),
                location.LocationForm.get_info(),
                description.InventoryDescriptionForm.get_info(),
                wizard.FileUploadForm.get_info()
            ]
 
        if self.entitytypeid == 'ACTIVITY_A.E7':
            description_group['forms'] = [
                summary.ActivityForm.get_info(),
                location.ActALocationForm.get_info(),
                description.ActADescriptionForm.get_info(),
                wizard.ActivityConsultationForm.get_info(),
                other.EntitiesForm.get_info(),
            ]
            review_group['forms'] = [
                review.Section106ReviewForm.get_info(),
                review.ARPAReviewForm.get_info(),
                review.NEPAReviewForm.get_info(),
                review.NCPCReviewForm.get_info(),
                review.CFAReviewForm.get_info(),
            ]

        if self.entitytypeid == 'ACTIVITY_B.E7':
            description_group['forms'] = [
                summary.ActivityForm.get_info(),
                location.SimpleLocationForm.get_info(),
                description.ActBDescriptionForm.get_info(),
                wizard.ActivityConsultationForm.get_info(),
                other.EntitiesForm.get_info(),
            ]
            review_group['forms'] = [
                review.ARPAReviewForm.get_info(),
                review.NEPAReviewForm.get_info(),
                review.HPOHPRBReviewForm.get_info(),
                review.NCPCReviewForm.get_info(),
                review.CFAReviewForm.get_info(),
            ]
        
        ## all resource types get the related resource form
        description_group['forms'].append(other.RelatedResourcesForm.get_info())
            
        self.form_groups.append(description_group)
        
        if len(review_group['forms']) != 0:
            self.form_groups.append(review_group)

        if self.entityid != '':
            self.form_groups.append(manage_group)
        
        if self.entityid != '':
            se = SearchEngineFactory().create()
            resource = se.search(index='resource', id=self.entityid)
            resource_graph = resource['_source']['graph']
            log_path = os.path.join(settings.PACKAGE_ROOT,'logs','current_graph.json')
            with open(log_path,"w") as log:
                print >> log, json.dumps(resource_graph, sort_keys=True,indent=2, separators=(',', ': '))
Exemplo n.º 48
0
def get_concept_label_from_valueid(valueid):
    se = SearchEngineFactory().create()
    concept_label = se.search(index="concepts", id=valueid)
    if concept_label["found"]:
        return concept_label["_source"]
Exemplo n.º 49
0
def report(request, resourceid):
    lang = request.GET.get('lang', settings.LANGUAGE_CODE)
    se = SearchEngineFactory().create()
    report_info = se.search(index='resource', id=resourceid)
    report_info['source'] = report_info['_source']
    report_info['type'] = report_info['_type']
    report_info['source']['graph'] = report_info['source']['graph']
    del report_info['_source']
    del report_info['_type']

    def get_evaluation_path(valueid):
        value = models.Values.objects.get(pk=valueid)
        concept_graph = Concept().get(id=value.conceptid_id, include_subconcepts=False, 
            include_parentconcepts=True, include_relatedconcepts=False, up_depth_limit=None, lang=lang)
        
        paths = []
        for path in concept_graph.get_paths(lang=lang)[0]:
            if path['label'] != 'Arches' and path['label'] != 'Evaluation Criteria Type':
                paths.append(path['label'])
        return '; '.join(paths)


    concept_label_ids = set()
    uuid_regex = re.compile('[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}')
    # gather together all uuid's referenced in the resource graph
    def crawl(items):
        for item in items:
            for key in item:
                if isinstance(item[key], list):
                    crawl(item[key])
                else:
                    if isinstance(item[key], basestring) and uuid_regex.match(item[key]):
                        if key == 'EVALUATION_CRITERIA_TYPE_E55__value':
                            item[key] = get_evaluation_path(item[key])
                        concept_label_ids.add(item[key])

    crawl([report_info['source']['graph']])

    # get all the concept labels from the uuid's
    concept_labels = se.search(index='concept_labels', id=list(concept_label_ids))

    # convert all labels to their localized prefLabel
    temp = {}
    if concept_labels != None:
        for concept_label in concept_labels['docs']:
            #temp[concept_label['_id']] = concept_label
            if concept_label['found']:
                # the resource graph already referenced the preferred label in the desired language
                if concept_label['_source']['type'] == 'prefLabel' and concept_label['_source']['language'] == lang:
                    temp[concept_label['_id']] = concept_label['_source']
                else: 
                    # the resource graph referenced a non-preferred label or a label not in our target language, so we need to get the right label
                    temp[concept_label['_id']] = get_preflabel_from_conceptid(concept_label['_source']['conceptid'], lang)

    # replace the uuid's in the resource graph with their preferred and localized label                    
    def crawl_again(items):
        for item in items:
            for key in item:
                if isinstance(item[key], list):
                    crawl_again(item[key])
                else:
                    if isinstance(item[key], basestring) and uuid_regex.match(item[key]):
                        try:
                            item[key] = temp[item[key]]['value']
                        except:
                            pass

    crawl_again([report_info['source']['graph']])

    #return JSONResponse(report_info, indent=4)

    related_resource_dict = {
        'HERITAGE_RESOURCE': [],
        'HERITAGE_RESOURCE_GROUP': [],
        'ACTIVITY': [],
        'ACTOR': [],
        'HISTORICAL_EVENT': [],
        'INFORMATION_RESOURCE_IMAGE': [],
        'INFORMATION_RESOURCE_DOCUMENT': []
    }

    related_resource_info = get_related_resources(resourceid, lang)

    # parse the related entities into a dictionary by resource type
    for related_resource in related_resource_info['related_resources']:
        information_resource_type = 'DOCUMENT'
        related_resource['relationship'] = []
        if related_resource['entitytypeid'] == 'HERITAGE_RESOURCE.E18':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'RESOURCE_TYPE_CLASSIFICATION.E55':
                    related_resource['relationship'].append(get_preflabel_from_valueid(entity['value'], lang)['value'])
        elif related_resource['entitytypeid'] == 'HERITAGE_RESOURCE_GROUP.E27':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'RESOURCE_TYPE_CLASSIFICATION.E55':
                    related_resource['relationship'].append(get_preflabel_from_valueid(entity['value'], lang)['value'])
        elif related_resource['entitytypeid'] == 'ACTIVITY.E7':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'ACTIVITY_TYPE.E55':
                    related_resource['relationship'].append(get_preflabel_from_valueid(entity['value'], lang)['value'])
        elif related_resource['entitytypeid'] == 'ACTOR.E39':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'ACTOR_TYPE.E55':
                    related_resource['relationship'].append(get_preflabel_from_conceptid(entity['conceptid'], lang)['value'])
                    related_resource['actor_relationshiptype'] = ''
        elif related_resource['entitytypeid'] == 'HISTORICAL_EVENT.E5':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'HISTORICAL_EVENT_TYPE.E55':
                    related_resource['relationship'].append(get_preflabel_from_conceptid(entity['conceptid'], lang)['value'])
        elif related_resource['entitytypeid'] == 'INFORMATION_RESOURCE.E73':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'INFORMATION_RESOURCE_TYPE.E55':
                    related_resource['relationship'].append(get_preflabel_from_valueid(entity['value'], lang)['value'])
            for entity in related_resource['child_entities']:
                if entity['entitytypeid'] == 'FILE_PATH.E62':
                    related_resource['file_path'] = settings.MEDIA_URL + entity['label']
                if entity['entitytypeid'] == 'THUMBNAIL.E62':
                    related_resource['thumbnail'] = settings.MEDIA_URL + entity['label']
                    information_resource_type = 'IMAGE'
            
        # get the relationship between the two entities
        for relationship in related_resource_info['resource_relationships']:
            if relationship['entityid1'] == related_resource['entityid'] or relationship['entityid2'] == related_resource['entityid']: 
                related_resource['relationship'].append(get_preflabel_from_valueid(relationship['relationshiptype'], lang)['value'])

        entitytypeidkey = related_resource['entitytypeid'].split('.')[0]
        if entitytypeidkey == 'INFORMATION_RESOURCE':
            entitytypeidkey = '%s_%s' % (entitytypeidkey, information_resource_type)
        related_resource_dict[entitytypeidkey].append(related_resource)

    # To hide Related Resource block in report page if no related resources present
    no_related_resources = False
    if all(len(value) == 0 for value in related_resource_dict.values()):
        no_related_resources = True

    return render_to_response('resource-report.htm', {
            'geometry': JSONSerializer().serialize(report_info['source']['geometry']),
            'resourceid': resourceid,
            'report_template': 'views/reports/' + report_info['type'] + '.htm',
            'report_info': report_info,
            'related_resource_dict': related_resource_dict,
            'no_related_resources': no_related_resources,
            'main_script': 'resource-report',
            'active_page': 'ResourceReport'
        },
        context_instance=RequestContext(request))        
Exemplo n.º 50
0
def report(request, resourceid):
    lang = request.GET.get('lang', settings.LANGUAGE_CODE)
    se = SearchEngineFactory().create()
    report_info = se.search(index='resource', id=resourceid)
    report_info['source'] = report_info['_source']
    report_info['type'] = report_info['_type']
    report_info['source']['graph'] = report_info['source']['graph']
    del report_info['_source']
    del report_info['_type']

    def get_evaluation_path(valueid):
        value = models.Values.objects.get(pk=valueid)
        concept_graph = Concept().get(id=value.conceptid_id, include_subconcepts=False, 
            include_parentconcepts=True, include_relatedconcepts=False, up_depth_limit=None, lang=lang)
        
        paths = []
        for path in concept_graph.get_paths(lang=lang)[0]:
            if path['label'] != 'Arches' and path['label'] != 'Evaluation Criteria Type':
                paths.append(path['label'])
        return '; '.join(paths)


    concept_label_ids = set()
    uuid_regex = re.compile('[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}')
    # gather together all uuid's referenced in the resource graph
    def crawl(items):
        for item in items:
            for key in item:
                if isinstance(item[key], list):
                    crawl(item[key])
                else:
                    if isinstance(item[key], basestring) and uuid_regex.match(item[key]):
                        if key == 'EVALUATION_CRITERIA_TYPE_E55__value':
                            item[key] = get_evaluation_path(item[key])
                        concept_label_ids.add(item[key])

    crawl([report_info['source']['graph']])

    # get all the concept labels from the uuid's
    concept_labels = se.search(index='concept_labels', id=list(concept_label_ids))

    # convert all labels to their localized prefLabel
    temp = {}
    if concept_labels != None:
        for concept_label in concept_labels['docs']:
            #temp[concept_label['_id']] = concept_label
            if concept_label['found']:
                # the resource graph already referenced the preferred label in the desired language
                if concept_label['_source']['type'] == 'prefLabel' and concept_label['_source']['language'] == lang:
                    temp[concept_label['_id']] = concept_label['_source']
                else: 
                    # the resource graph referenced a non-preferred label or a label not in our target language, so we need to get the right label
                    temp[concept_label['_id']] = get_preflabel_from_conceptid(concept_label['_source']['conceptid'], lang)

    # replace the uuid's in the resource graph with their preferred and localized label                    
    def crawl_again(items):
        for item in items:
            for key in item:
                if isinstance(item[key], list):
                    crawl_again(item[key])
                else:
                    if isinstance(item[key], basestring) and uuid_regex.match(item[key]):
                        try:
                            item[key] = temp[item[key]]['value']
                        except:
                            pass

    crawl_again([report_info['source']['graph']])

    #return JSONResponse(report_info, indent=4)

    related_resource_dict = {
        'HERITAGE_RESOURCE': [],
        'HERITAGE_RESOURCE_GROUP': [],
        'ACTIVITY': [],
        'ACTOR': [],
        'HISTORICAL_EVENT': [],
        'INFORMATION_RESOURCE_IMAGE': [],
        'INFORMATION_RESOURCE_DOCUMENT': []
    }

    related_resource_info = get_related_resources(resourceid, lang)

    # parse the related entities into a dictionary by resource type
    for related_resource in related_resource_info['related_resources']:
        information_resource_type = 'DOCUMENT'
        related_resource['relationship'] = []
        if related_resource['entitytypeid'] == 'HERITAGE_RESOURCE.E18':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'RESOURCE_TYPE_CLASSIFICATION.E55':
                    related_resource['relationship'].append(get_preflabel_from_valueid(entity['value'], lang)['value'])
        elif related_resource['entitytypeid'] == 'HERITAGE_RESOURCE_GROUP.E27':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'RESOURCE_TYPE_CLASSIFICATION.E55':
                    related_resource['relationship'].append(get_preflabel_from_valueid(entity['value'], lang)['value'])
        elif related_resource['entitytypeid'] == 'ACTIVITY.E7':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'ACTIVITY_TYPE.E55':
                    related_resource['relationship'].append(get_preflabel_from_valueid(entity['value'], lang)['value'])
        elif related_resource['entitytypeid'] == 'ACTOR.E39':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'ACTOR_TYPE.E55':
                    related_resource['relationship'].append(get_preflabel_from_conceptid(entity['conceptid'], lang)['value'])
                    related_resource['actor_relationshiptype'] = ''
        elif related_resource['entitytypeid'] == 'HISTORICAL_EVENT.E5':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'HISTORICAL_EVENT_TYPE.E55':
                    related_resource['relationship'].append(get_preflabel_from_conceptid(entity['conceptid'], lang)['value'])
        elif related_resource['entitytypeid'] == 'INFORMATION_RESOURCE.E73':
            for entity in related_resource['domains']:
                if entity['entitytypeid'] == 'INFORMATION_RESOURCE_TYPE.E55':
                    related_resource['relationship'].append(get_preflabel_from_valueid(entity['value'], lang)['value'])
            for entity in related_resource['child_entities']:
                if entity['entitytypeid'] == 'FILE_PATH.E62':
                    related_resource['file_path'] = settings.MEDIA_URL + entity['label']
                if entity['entitytypeid'] == 'THUMBNAIL.E62':
                    related_resource['thumbnail'] = settings.MEDIA_URL + entity['label']
                    information_resource_type = 'IMAGE'
            
        # get the relationship between the two entities
        for relationship in related_resource_info['resource_relationships']:
            if relationship['entityid1'] == related_resource['entityid'] or relationship['entityid2'] == related_resource['entityid']: 
                related_resource['relationship'].append(get_preflabel_from_valueid(relationship['relationshiptype'], lang)['value'])

        entitytypeidkey = related_resource['entitytypeid'].split('.')[0]
        if entitytypeidkey == 'INFORMATION_RESOURCE':
            entitytypeidkey = '%s_%s' % (entitytypeidkey, information_resource_type)
        related_resource_dict[entitytypeidkey].append(related_resource)

    return render_to_response('resource-report.htm', {
            'geometry': JSONSerializer().serialize(report_info['source']['geometry']),
            'resourceid': resourceid,
            'report_template': 'views/reports/' + report_info['type'] + '.htm',
            'report_info': report_info,
            'related_resource_dict': related_resource_dict,
            'main_script': 'resource-report',
            'active_page': 'ResourceReport'
        },
        context_instance=RequestContext(request))