Beispiel #1
0
    def get_node_display_values(self):
        for nodeid, nodevalue in self.data.iteritems():
            if models.Node.objects.get(pk=nodeid).datatype == 'concept':
                self.data[nodeid] = get_preflabel_from_valueid(
                    nodevalue, 'en-US')['value']

        return self.data
Beispiel #2
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
Beispiel #3
0
    def load(self, lang):
        data = []
        for relatedentity in self.resource.get_related_resources():
            nodes = relatedentity['related_entity'].flatten()

            data.append({
                'nodes': nodes, 
                'relationship': relatedentity['relationship'], 
                'relationshiptypelabel': get_preflabel_from_valueid(relatedentity['relationship'].relationshiptype, lang)['value'],
                'relatedresourcename':relatedentity['related_entity'].get_primary_name(),
                'relatedresourcetype':relatedentity['related_entity'].entitytypeid,
                'relatedresourceid':relatedentity['related_entity'].entityid,
                'related': True,
            })

        relationship_types = Concept().get_e55_domain('ARCHES_RESOURCE_CROSS-REFERENCE_RELATIONSHIP_TYPES.E55')

        try:
            default_relationship_type = relationship_types[0]['id']

            self.data['related-resources'] = {
                'branch_lists': data,
                'domains': {
                    'RELATIONSHIP_TYPES.E32': relationship_types
                },
                'default_relationship_type':  default_relationship_type
            }
            self.data['resource-id'] = self.resource.entityid
        except IndexError:
            pass
Beispiel #4
0
    def get_nodes(self, entitytypeid):

        #return self.resource.get_nodes(entitytypeid, keys=['label', 'value', 'entityid', 'entitytypeid'])
        ret = []
        prefLabel = {}
        entities = self.resource.find_entities_by_type_id(entitytypeid)
        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}'
        )
        for entity in entities:

            flattened = []
            # Iterates through every branch (with its child nodes) to substitute the default label with the desired prefLabel
            for flattenedvalue in entity.flatten():
                # Makes sure that only the visualisation of concepts is altered: free text and geometric data are not
                if isinstance(flattenedvalue.value,
                              basestring) and uuid_regex.match(
                                  flattenedvalue.value):
                    # Retrieves the concept label in the correct language
                    prefLabel = get_preflabel_from_valueid(
                        flattenedvalue.value, lang=translation.get_language())
                    flattenedvalue.label = prefLabel['value']
                    flattenedvalue.value = prefLabel['id']

                flattened.append(flattenedvalue)

            ret.append({'nodes': flattened})
        return ret
Beispiel #5
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
Beispiel #6
0
    def get_nodes(self, entitytypeid):

        #return self.resource.get_nodes(entitytypeid, keys=['label', 'value', 'entityid', 'entitytypeid'])
        ret = []
        prefLabel  = {}
        entities = self.resource.find_entities_by_type_id(entitytypeid)
        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}')
        for entity in entities:
            
            flattened = []
            # Iterates through every branch (with its child nodes) to substitute the default label with the desired prefLabel
            for flattenedvalue in entity.flatten():
                # Makes sure that only the visualisation of concepts is altered: free text and geometric data are not
                if isinstance(flattenedvalue.value, basestring) and uuid_regex.match(flattenedvalue.value):
                    # Retrieves the concept label in the correct language
                    prefLabel = get_preflabel_from_valueid(flattenedvalue.value, lang=translation.get_language())
                    flattenedvalue.label = prefLabel['value']
                    flattenedvalue.value = prefLabel['id']

                flattened.append(flattenedvalue)
            
            ret.append({'nodes': flattened})

        return ret
Beispiel #7
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))     
Beispiel #8
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))
Beispiel #9
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))        
Beispiel #10
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))        
Beispiel #11
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))        
Beispiel #12
0
    def get_node_display_values(self):
        for nodeid, nodevalue in self.data.iteritems():
            if models.Node.objects.get(pk=nodeid).datatype == 'concept':
                self.data[nodeid] = get_preflabel_from_valueid(nodevalue, 'en-US')['value']

        return self.data
Beispiel #13
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))   
Beispiel #14
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))
Beispiel #15
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))