def search_terms(request): lang = request.GET.get('lang', settings.LANGUAGE_CODE) se = SearchEngineFactory().create() searchString = request.GET.get('q', '') user_is_reviewer = request.user.groups.filter(name='Resource Reviewer').exists() i = 0 ret = {} for index in ['terms', 'concepts']: query = Query(se, start=0, limit=0) boolquery = Bool() boolquery.should(Match(field='value', query=searchString.lower(), type='phrase_prefix')) boolquery.should(Match(field='value.folded', query=searchString.lower(), type='phrase_prefix')) boolquery.should(Match(field='value.folded', query=searchString.lower(), fuzziness='AUTO', prefix_length=settings.SEARCH_TERM_SENSITIVITY)) if user_is_reviewer is False and index == 'terms': boolquery.filter(Terms(field='provisional', terms=['false'])) query.add_query(boolquery) base_agg = Aggregation(name='value_agg', type='terms', field='value.raw', size=settings.SEARCH_DROPDOWN_LENGTH, order={"max_score": "desc"}) nodegroupid_agg = Aggregation(name='nodegroupid', type='terms', field='nodegroupid') top_concept_agg = Aggregation(name='top_concept', type='terms', field='top_concept') conceptid_agg = Aggregation(name='conceptid', type='terms', field='conceptid') max_score_agg = MaxAgg(name='max_score', script='_score') top_concept_agg.add_aggregation(conceptid_agg) base_agg.add_aggregation(max_score_agg) base_agg.add_aggregation(top_concept_agg) base_agg.add_aggregation(nodegroupid_agg) query.add_aggregation(base_agg) ret[index] = [] results = query.search(index=index) for result in results['aggregations']['value_agg']['buckets']: if len(result['top_concept']['buckets']) > 0: for top_concept in result['top_concept']['buckets']: top_concept_id = top_concept['key'] top_concept_label = get_preflabel_from_conceptid(top_concept['key'], lang)['value'] for concept in top_concept['conceptid']['buckets']: ret[index].append({ 'type': 'concept', 'context': top_concept_id, 'context_label': top_concept_label, 'id': i, 'text': result['key'], 'value': concept['key'] }) i = i + 1 else: ret[index].append({ 'type': 'term', 'context': '', 'context_label': get_resource_model_label(result), 'id': i, 'text': result['key'], 'value': result['key'] }) i = i + 1 return JSONResponse(ret)
def get_auto_filter(request): lang = request.GET.get('lang', settings.LANGUAGE_CODE) se1 = SearchEngineFactory().create() searchString1 = settings.PUBLISHED_LABEL query1 = Query(se1, start=0, limit=settings.SEARCH_DROPDOWN_LENGTH) boolquery1 = Bool() boolquery1.should(Match(field='term', query=searchString1.lower(), type='phrase_prefix', fuzziness='AUTO')) boolquery1.should(Match(field='term.folded', query=searchString1.lower(), type='phrase_prefix', fuzziness='AUTO')) boolquery1.should(Match(field='term.folded', query=searchString1.lower(), fuzziness='AUTO')) query1.add_query(boolquery1) results1 = query1.search(index='term', doc_type='value') conceptid1 = '' context1 = '' for result1 in results1['hits']['hits']: prefLabel = get_preflabel_from_conceptid(result1['_source']['context'], lang) result1['_source']['options']['context_label'] = prefLabel['value'] if (prefLabel['value'] == settings.EW_STATUS_TERM and result1['_source']['term'] == settings.PUBLISHED_LABEL) : conceptid1 = result1['_source']['options']['conceptid'] context1 = result1['_source']['context'] AUTO_TERM_FILTER = {"inverted": False, "type": "concept"} AUTO_TERM_FILTER["text"] = settings.PUBLISHED_LABEL AUTO_TERM_FILTER["value"] = conceptid1 AUTO_TERM_FILTER["context"] = context1 AUTO_TERM_FILTER["context_label"] = settings.EW_STATUS_TERM AUTO_TERM_FILTER["id"] = AUTO_TERM_FILTER['text'] + conceptid1 return AUTO_TERM_FILTER
def search_terms(request): lang = request.GET.get('lang', settings.LANGUAGE_CODE) se = SearchEngineFactory().create() searchString = request.GET.get('q', '') query = Query(se, start=0, limit=0) user_is_reviewer = request.user.groups.filter(name='Resource Reviewer').exists() boolquery = Bool() boolquery.should(Match(field='value', query=searchString.lower(), type='phrase_prefix', fuzziness='AUTO')) boolquery.should(Match(field='value.folded', query=searchString.lower(), type='phrase_prefix', fuzziness='AUTO')) boolquery.should(Match(field='value.folded', query=searchString.lower(), fuzziness='AUTO')) if user_is_reviewer is False: boolquery.filter(Terms(field='provisional', terms=['false'])) query.add_query(boolquery) base_agg = Aggregation(name='value_agg', type='terms', field='value.raw', size=settings.SEARCH_DROPDOWN_LENGTH, order={"max_score": "desc"}) nodegroupid_agg = Aggregation(name='nodegroupid', type='terms', field='nodegroupid') top_concept_agg = Aggregation(name='top_concept', type='terms', field='top_concept') conceptid_agg = Aggregation(name='conceptid', type='terms', field='conceptid') max_score_agg = MaxAgg(name='max_score', script='_score') top_concept_agg.add_aggregation(conceptid_agg) base_agg.add_aggregation(max_score_agg) base_agg.add_aggregation(top_concept_agg) base_agg.add_aggregation(nodegroupid_agg) query.add_aggregation(base_agg) results = query.search(index='strings') or {'hits': {'hits':[]}} i = 0; ret = [] for result in results['aggregations']['value_agg']['buckets']: if len(result['top_concept']['buckets']) > 0: for top_concept in result['top_concept']['buckets']: top_concept_id = top_concept['key'] top_concept_label = get_preflabel_from_conceptid(top_concept['key'], lang)['value'] for concept in top_concept['conceptid']['buckets']: ret.append({ 'type': 'concept', 'context': top_concept_id, 'context_label': top_concept_label, 'id': i, 'text': result['key'], 'value': concept['key'] }) i = i + 1 else: ret.append({ 'type': 'term', 'context': '', 'context_label': get_resource_model_label(result), 'id': i, 'text': result['key'], 'value': result['key'] }) i = i + 1 return JSONResponse(ret)
def search_terms(request): lang = request.GET.get('lang', settings.LANGUAGE_CODE) se = SearchEngineFactory().create() searchString = request.GET.get('q', '') query = Query(se, start=0, limit=0) boolquery = Bool() boolquery.should(Match(field='value', query=searchString.lower(), type='phrase_prefix', fuzziness='AUTO')) boolquery.should(Match(field='value.folded', query=searchString.lower(), type='phrase_prefix', fuzziness='AUTO')) boolquery.should(Match(field='value.folded', query=searchString.lower(), fuzziness='AUTO')) query.add_query(boolquery) base_agg = Aggregation(name='value_agg', type='terms', field='value.raw', size=settings.SEARCH_DROPDOWN_LENGTH, order={"max_score": "desc"}) nodegroupid_agg = Aggregation(name='nodegroupid', type='terms', field='nodegroupid') top_concept_agg = Aggregation(name='top_concept', type='terms', field='top_concept') conceptid_agg = Aggregation(name='conceptid', type='terms', field='conceptid') max_score_agg = MaxAgg(name='max_score', script='_score') top_concept_agg.add_aggregation(conceptid_agg) base_agg.add_aggregation(max_score_agg) base_agg.add_aggregation(top_concept_agg) base_agg.add_aggregation(nodegroupid_agg) query.add_aggregation(base_agg) results = query.search(index='strings') or {'hits': {'hits':[]}} i = 0; ret = [] for result in results['aggregations']['value_agg']['buckets']: if len(result['top_concept']['buckets']) > 0: for top_concept in result['top_concept']['buckets']: top_concept_id = top_concept['key'] top_concept_label = get_preflabel_from_conceptid(top_concept['key'], lang)['value'] for concept in top_concept['conceptid']['buckets']: ret.append({ 'type': 'concept', 'context': top_concept_id, 'context_label': top_concept_label, 'id': i, 'text': result['key'], 'value': concept['key'] }) i = i + 1 else: ret.append({ 'type': 'term', 'context': '', 'context_label': '', 'id': i, 'text': result['key'], 'value': result['key'] }) i = i + 1 return JSONResponse(ret)
def search_terms(request): lang = request.GET.get('lang', settings.LANGUAGE_CODE) query = build_search_terms_dsl(request) results = query.search(index='term', doc_type='value') for result in results['hits']['hits']: prefLabel = get_preflabel_from_conceptid(result['_source']['context'], lang) result['_source']['options']['context_label'] = prefLabel['value'] return JSONResponse(results)
def get_search_range_contexts(request): search_range_context = {} search_range_context = cache.get('search_range_contexts') if search_range_context is not None: #print 'Search_range_context iz cacha!' return search_range_context lang = request.GET.get('lang', request.LANGUAGE_CODE) se1 = SearchEngineFactory().create() context_label1 = '-' search_range_context = {} for search_term in settings.RANGE_TERMS: searchString1 = search_term['text'] query1 = Query(se1, start=0, limit=settings.SEARCH_DROPDOWN_LENGTH) boolquery1 = Bool() boolquery1.should(Match(field='term', query=searchString1.lower(), type='phrase_prefix', fuzziness='AUTO')) boolquery1.should(Match(field='term.folded', query=searchString1.lower(), type='phrase_prefix', fuzziness='AUTO')) boolquery1.should(Match(field='term.folded', query=searchString1.lower(), fuzziness='AUTO')) query1.add_query(boolquery1) results1 = query1.search(index='term', doc_type='value') conceptid1 = '' context1 = '' for result1 in results1['hits']['hits']: prefLabel = get_preflabel_from_conceptid(result1['_source']['context'], lang) result1['_source']['options']['context_label'] = prefLabel['value'] if (prefLabel['value'] == search_term['context_label'] and result1['_source']['term'] == search_term['text']): #print result1['_source']['ids'][0] conceptid1 = result1['_source']['options']['conceptid'] context1 = result1['_source']['context'] valueid1 = result1['_source']['ids'][0] #print search_term['context_label'] + ': ' + conceptid1 #print searchString1 #print result1 result = {'conceptid': conceptid1, 'context': context1, 'valueid': valueid1} if context_label1 <> search_term['context_label']: value = {} #print result value[search_term['text_key']] = result #print value search_range_context[search_term['context_key']] = value #print search_range_context #print 'Iscem [' + search_term['context_label'] + '][' + search_term['text'] + ']' #print value context_label1 = search_term['context_label'] #print search_range_context #print search_range_context['Historical_Period']['BRONZE_AGE'] #print 'Shranjujem search_range_context v cache' cache.set('search_range_contexts', search_range_context, 86400) return search_range_context
def get_search_contexts(request): search_context = {} search_context = cache.get('search_contexts') if search_context is not None: #print 'Search_context iz cacha!' return search_context lang = request.GET.get('lang', settings.LANGUAGE_CODE) se1 = SearchEngineFactory().create() context_label1 = '-' search_context = {} for search_term in settings.SEARCH_TERMS: searchString1 = search_term['text'] print searchString1 query1 = Query(se1, start=0, limit=settings.SEARCH_DROPDOWN_LENGTH) boolquery1 = Bool() boolquery1.should(Match(field='term', query=searchString1.lower(), type='phrase_prefix', fuzziness='AUTO')) boolquery1.should(Match(field='term.folded', query=searchString1.lower(), type='phrase_prefix', fuzziness='AUTO')) boolquery1.should(Match(field='term.folded', query=searchString1.lower(), fuzziness='AUTO')) query1.add_query(boolquery1) results1 = query1.search(index='term', doc_type='value') conceptid1 = '' context1 = '' for result1 in results1['hits']['hits']: prefLabel = get_preflabel_from_conceptid(result1['_source']['context'], lang) result1['_source']['options']['context_label'] = prefLabel['value'] if (prefLabel['value'] == search_term['context_label'] and result1['_source']['term'] == search_term['text']): conceptid1 = result1['_source']['options']['conceptid'] context1 = result1['_source']['context'] #print search_term['context_label'] + ': ' + conceptid1 #print searchString1 #print result1 result = {'conceptid': conceptid1, 'context': context1} if context_label1 <> search_term['context_label']: value = {} print result value[search_term['text_key']] = result #print value search_context[search_term['context_key']] = value #print search_context #print 'Iscem [' + search_term['context_label'] + '][' + search_term['text'] + ']' #print value context_label1 = search_term['context_label'] #print search_context #print search_context['Historical_Period']['BRONZE_AGE'] #print 'Shranjujem search_context v cache' cache.set('search_contexts', search_context, 86400) return search_context
def search_terms(request): lang = request.GET.get('lang', settings.LANGUAGE_CODE) query = build_search_terms_dsl(request) results = query.search(index='term', doc_type='value') return_results = [] for result in results['hits']['hits']: # Ce uporabnik ni avtenticiran, prikazemo le veljavne (to je verjetno potrebno se dodelati (mogoce da vidijo le svoje???)!!!) if (request.user.username == 'anonymous'): if ('ewstatus' in result['_source']): if (result['_source']['ewstatus'] != settings.PUBLISHED_LABEL): continue prefLabel = get_preflabel_from_conceptid(result['_source']['context'], lang) result['_source']['options']['context_label'] = prefLabel['value'] return_results.append(result) results['hits']['hits'] = return_results return JSONResponse(results)
def map_layers(request, entitytypeid='all', get_centroids=False): lang = request.GET.get('lang', request.LANGUAGE_CODE) if lang == 'en': lang = 'en-US' data = [] geom_param = request.GET.get('geom', None) print 'map_layers: ' + entitytypeid #print request.method bbox = request.GET.get('bbox', '') limit = request.GET.get('limit', settings.MAP_LAYER_FEATURE_LIMIT) if request.method == 'GET': entityids = request.GET.get('entityid', '') elif request.method == 'POST': entityids = request.POST.get('entityid', '') #print entityids geojson_collection = { "type": "FeatureCollection", "features": [] } #print request.META url = request.META.get('HTTP_REFERER') searchType = 'Search' if not url: return JSONResponse(geojson_collection) if url.find('searchType')>0: parsed = urlparse.urlparse(url) searchType = urlparse.parse_qs(parsed.query)['searchType'][0] else: if url.find('search_sites')>0: searchType = 'Site' entitytypeid = 'SITE.E18' elif url.find('search_graves')>0: searchType = 'Grave' entitytypeid = 'GRAVE.E18' elif url.find('search_objects')>0: searchType = 'Object' entitytypeid = 'OBJECT.E18' #print searchType se = SearchEngineFactory().create() query = Query(se, limit=limit) args = { 'index': 'maplayers' } if entitytypeid != 'all': args['doc_type'] = entitytypeid if entityids != '': for entityid in entityids.split(','): item = se.search(index='maplayers', id=entityid) #print item # Prevodi #print 'Result_item' #print item['_source']['properties'] concept_label_ids = set() uuid_regex = re.compile('[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}') # gather together all uuid's referenced in the resource graph def crawl(items): for item in items: if isinstance(item, dict): for key in item: if isinstance(item[key], list): crawl(item[key]) else: if isinstance(item[key], basestring) and uuid_regex.match(item[key]): concept_label_ids.add(item[key]) crawl([item['_source']['properties']]) # get all the concept labels from the uuid's concept_labels = se.search(index='concept_labels', id=list(concept_label_ids)) # convert all labels to their localized prefLabel temp = {} if concept_labels != None: for concept_label in concept_labels['docs']: #temp[concept_label['_id']] = concept_label if concept_label['found']: # the resource graph already referenced the preferred label in the desired language if concept_label['_source']['type'] == 'prefLabel' and concept_label['_source']['language'] == lang: temp[concept_label['_id']] = concept_label['_source'] else: # the resource graph referenced a non-preferred label or a label not in our target language, so we need to get the right label temp[concept_label['_id']] = get_preflabel_from_conceptid(concept_label['_source']['conceptid'], lang) # replace the uuid's in the resource graph with their preferred and localized label def crawl_again(items): for item in items: if isinstance(item, dict): for key in item: if isinstance(item[key], list): crawl_again(item[key]) else: if isinstance(item[key], basestring) and uuid_regex.match(item[key]): try: item[key] = temp[item[key]]['value'] except: pass crawl_again([item['_source']['properties']]) #print 'crawl_again' #print item['_source']['properties'] geojson_collection['features'].append(item['_source']) #geojson_collection['features'].append(se.search(index='maplayers', id=entityid)['_source']) #Poiskus pridobitve slik - pridobiti se jih da, vendar jih je potem problem prikazati, zato jih tu ne bomo prikazovali #related_resources = get_related_resources(entityid, lang, start=0, limit=15) #if related_resources['related_resources']: # thumbnails = {'thumbnail': [] } # for entity in related_resources['related_resources'][0]['child_entities']: #print entity # if entity['entitytypeid']=='THUMBNAIL.E62': # thumbnails['thumbnail'].append(entity['value']) # item['_source']['properties']['thumbnails'] = thumbnails #print item['_source']['properties'] geojson_collection['features'] = sorted(geojson_collection['features'], key=lambda k: (k['properties']['primaryname'].lower())) return JSONResponse(geojson_collection) data = query.search(**args) for item in data['hits']['hits']: # Ce nismo na splosnem searchu, upostevamo samo ustrezne tipe resourcov if (searchType != 'Search'): #print item #print item['_source']['properties']['searchType'] if (item['_source']['properties']['searchType'] != searchType): continue #print 'Je' if get_centroids: item['_source']['geometry'] = item['_source']['properties']['centroid'] item['_source'].pop('properties', None) elif geom_param != None: item['_source']['geometry'] = item['_source']['properties'][geom_param] item['_source']['properties'].pop('extent', None) item['_source']['properties'].pop(geom_param, None) else: item['_source']['properties'].pop('extent', None) item['_source']['properties'].pop('centroid', None) geojson_collection['features'].append(item['_source']) print 'St. zapisov: ' print len(data['hits']['hits']) return JSONResponse(geojson_collection)
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))
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))
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))
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))
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))
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))
def search_terms(request): lang = request.GET.get("lang", settings.LANGUAGE_CODE) se = SearchEngineFactory().create() searchString = request.GET.get("q", "") user_is_reviewer = user_is_resource_reviewer(request.user) i = 0 ret = {} for index in ["terms", "concepts"]: query = Query(se, start=0, limit=0) boolquery = Bool() boolquery.should( Match(field="value", query=searchString.lower(), type="phrase_prefix")) boolquery.should( Match(field="value.folded", query=searchString.lower(), type="phrase_prefix")) boolquery.should( Match(field="value.folded", query=searchString.lower(), fuzziness="AUTO", prefix_length=settings.SEARCH_TERM_SENSITIVITY)) if user_is_reviewer is False and index == "terms": boolquery.filter(Terms(field="provisional", terms=["false"])) query.add_query(boolquery) base_agg = Aggregation(name="value_agg", type="terms", field="value.raw", size=settings.SEARCH_DROPDOWN_LENGTH, order={"max_score": "desc"}) nodegroupid_agg = Aggregation(name="nodegroupid", type="terms", field="nodegroupid") top_concept_agg = Aggregation(name="top_concept", type="terms", field="top_concept") conceptid_agg = Aggregation(name="conceptid", type="terms", field="conceptid") max_score_agg = MaxAgg(name="max_score", script="_score") top_concept_agg.add_aggregation(conceptid_agg) base_agg.add_aggregation(max_score_agg) base_agg.add_aggregation(top_concept_agg) base_agg.add_aggregation(nodegroupid_agg) query.add_aggregation(base_agg) ret[index] = [] results = query.search(index=index) if results is not None: for result in results["aggregations"]["value_agg"]["buckets"]: if len(result["top_concept"]["buckets"]) > 0: for top_concept in result["top_concept"]["buckets"]: top_concept_id = top_concept["key"] top_concept_label = get_preflabel_from_conceptid( top_concept["key"], lang)["value"] for concept in top_concept["conceptid"]["buckets"]: ret[index].append({ "type": "concept", "context": top_concept_id, "context_label": top_concept_label, "id": i, "text": result["key"], "value": concept["key"], }) i = i + 1 else: ret[index].append({ "type": "term", "context": "", "context_label": get_resource_model_label(result), "id": i, "text": result["key"], "value": result["key"], }) i = i + 1 return JSONResponse(ret)
def get_field_map_values(self, resource, template_record, field_map): """ For a given resource, loops over its field map in the export mappings and collects values that correspond each entitytypeid in the field map. Inserts those values into the template record's corresponding list of values. """ domains = resource['_source']['domains'] child_entities = resource['_source']['child_entities'] child_entities += resource['_source']['dates'] child_entities += resource['_source']['numbers'] for mapping in field_map: conceptid = '' if 'value_type' in mapping: conceptid = mapping['value_type'] entitytypeid = mapping['entitytypeid'] alternates = False alternate_entitytypeid = None alternate_values = None if 'alternate_entitytypeid' in mapping: alternates = True alternate_entitytypeid = mapping['alternate_entitytypeid'] alternate_values = [] businesstable = archesmodels.EntityTypes.objects.get( pk=mapping['entitytypeid']) if businesstable.businesstablename != 'domains': if businesstable.businesstablename == 'geometries': geometries = resource['_source']['geometries'] for g in geometries: template_record[mapping['field_name']].append( g['value']) else: for entity in child_entities: if alternate_entitytypeid == entity['entitytypeid']: alternate_values.append(entity['value']) if entitytypeid == entity[ 'entitytypeid'] and conceptid == '': template_record[mapping['field_name']].append( entity['value']) elif entitytypeid == entity[ 'entitytypeid'] and conceptid != '': for domain in domains: if conceptid == domain['conceptid']: if entity['entityid'] == domain[ 'parentid']: template_record[ mapping['field_name']].append( entity['value']) else: for domain in domains: if entitytypeid == domain[ 'entitytypeid'] and conceptid == '': template_record[mapping['field_name']].append( get_preflabel_from_conceptid( domain['conceptid'], lang=translation.get_language())['value']) elif entitytypeid == domain[ 'entitytypeid'] and conceptid != '': for domain_type in domains: if conceptid == domain_type['conceptid']: if domain['entityid'] == domain_type[ 'parentid']: template_record[ mapping['field_name']].append( get_preflabel_from_conceptid( domain['conceptid'], lang=translation.get_language( ))['value']) if alternate_entitytypeid == domain['entitytypeid']: alternate_values.append(entity['value']) if alternates == True and len( template_record[mapping['field_name']]) == 0: if len(alternate_values) > 0: template_record[mapping['field_name']] = alternate_values return dict((k, v) for k, v in template_record.items() if v)
def search_terms(request): lang = request.GET.get('lang', settings.LANGUAGE_CODE) query = build_search_terms_dsl(request) results = query.search(index='term', doc_type='value') group_root_node = request.GET.get('group_root_node', '') delete_results = [] for result in results['hits']['hits']: prefLabel = get_preflabel_from_conceptid(result['_source']['context'], lang) result['_source']['options']['context_label'] = prefLabel['value'] entity_type = None # if a group is selected we have to filter out the results that don't belong to the selected group if group_root_node != 'No group': if 'conceptid' in result['_source']['options']: # concept: find the entity_type to check if it is connected to the selected group valueid = result['_source']['options']["conceptid"] value_relations_to = models.ConceptRelations.objects.filter(conceptidto=valueid, relationtype='member') if len(value_relations_to): value_parent_concept = models.Concepts.objects.filter(conceptid=value_relations_to[0].conceptidfrom) parent_relations_to = models.ConceptRelations.objects.filter(conceptidto=value_parent_concept[0].conceptid, relationtype='member') if value_parent_concept[0].nodetype.nodetype == 'Concept': # need to get at the parent until we reach the root collection. concepts are arranged hierarchically grandparent = models.Concepts.objects.filter(conceptid=parent_relations_to[0].conceptidfrom) entity_type = grandparent[0].legacyoid elif value_parent_concept[0].nodetype.nodetype == 'Collection': entity_type = value_parent_concept[0].legacyoid else: logging.warning("Not a concept or collection") else: # not a concept - possibly a name field or similar. Use the context entity_type = models.EntityTypes.objects.filter(conceptid=result['_source']['context']) delete_result = True # check the if the entity_type is under the selected root group node # so that it can be deleted later if entity_type: res = Entity().get_mapping_schema_to(entity_type) # search parents for group_root_node if 'HERITAGE_RESOURCE_GROUP.E27' in res: for parent in res['HERITAGE_RESOURCE_GROUP.E27']['steps']: if parent['entitytyperange'] == group_root_node: delete_result = False break if delete_result: delete_results.append(result) # deleted the flagged results for result in delete_results: results['hits']['hits'].remove(result); results['hits']['total'] = len(results['hits']['hits']) return JSONResponse(results)
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))
def search_terms(request): lang = request.GET.get('lang', settings.LANGUAGE_CODE) query = build_search_terms_dsl(request) results = query.search(index='term', doc_type='value') group_root_node = request.GET.get('group_root_node', '') delete_results = [] for result in results['hits']['hits']: prefLabel = get_preflabel_from_conceptid(result['_source']['context'], lang) result['_source']['options']['context_label'] = prefLabel['value'] entity_type = None # if a group is selected we have to filter out the results that don't belong to the selected group if group_root_node != 'No group': entities = [] if 'conceptid' in result['_source']['options']: # concept: find the entity_type to check if it is connected to the selected group valueid = result['_source']['options']["conceptid"] value_relations_to = models.ConceptRelations.objects.filter(conceptidto=valueid, relationtype='member') if value_relations_to: for value_relations_to_concept in value_relations_to: value_parent_concept = models.Concepts.objects.filter(conceptid=value_relations_to_concept.conceptidfrom) parent_relations_to = models.ConceptRelations.objects.filter(conceptidto=value_parent_concept[0].conceptid, relationtype='member') if value_parent_concept[0].nodetype.nodetype == 'Concept': # need to get at the parent until we reach the root collection. concepts are arranged hierarchically grandparent = models.Concepts.objects.filter(conceptid=parent_relations_to[0].conceptidfrom) entity_type = grandparent[0].legacyoid entities.append(entity_type) elif value_parent_concept[0].nodetype.nodetype == 'Collection': entity_type = value_parent_concept[0].legacyoid entities.append(entity_type) else: logging.warning("Not a concept or collection") else: # not a concept - possibly a name field or similar. Use the context entity_type = models.EntityTypes.objects.filter(conceptid=result['_source']['context']) entities.append(entity_type) delete_result = True # check the if the entity_type is under the selected root group node # so that it can be deleted later if entities: for entity_type in entities: res = Entity().get_mapping_schema_to(entity_type) # search parents for group_root_node for resourcetype in settings.RESOURCE_TYPE_CONFIGS().keys(): if resourcetype in res: for parent in res[resourcetype]['steps']: if parent['entitytyperange'] == group_root_node: delete_result = False break if delete_result: delete_results.append(result) # deleted the flagged results for result in delete_results: results['hits']['hits'].remove(result); results['hits']['total'] = len(results['hits']['hits']) return JSONResponse(results)
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))