def make_trinomial_from_site_labels(self, project_uuid, state_prefix=''): """ makes trinomial identifiers from a site label """ ent = Entity() found = ent.dereference(project_uuid) if found: proj_label = ent.label sites = Manifest.objects\ .filter(project_uuid=project_uuid, class_uri='oc-gen:cat-site') for site in sites: trinomial = str(state_prefix) + site.label parts = self.parse_trinomial(trinomial) dt = Trinomial() dt.uri = URImanagement.make_oc_uri(site.uuid, site.item_type) dt.uuid = site.uuid dt.label = site.label dt.project_label = proj_label dt.trinomial = trinomial dt.state = parts['state'] dt.county = parts['county'] dt.site = parts['site'] dt.save() print('Trinomial: ' + trinomial + ', from: ' + site.label)
def id_summary(request, identifier): """ Returns JSON data for entities limited by certain criteria """ lequiv = LinkEquivalence() id_list = lequiv.get_identifier_list_variants(identifier) entity_obj = False for test_id in id_list: ent = Entity() ent.get_stable_ids = True found = ent.dereference(test_id) if found: entity_obj = LastUpdatedOrderedDict() entity_obj['id'] = ent.uri entity_obj['label'] = ent.label entity_obj['uuid'] = ent.uuid entity_obj['slug'] = ent.slug entity_obj['item_type'] = ent.item_type entity_obj['class_uri'] = ent.class_uri entity_obj['data_type'] = ent.data_type entity_obj['vocab_uri'] = ent.vocab_uri entity_obj['project_uuid'] = ent.project_uuid entity_obj['stable_id_uris'] = ent.stable_id_uris break if entity_obj is not False: json_output = json.dumps(entity_obj, indent=4, ensure_ascii=False) return HttpResponse(json_output, content_type='application/json; charset=utf8') else: raise Http404
def create_template_for_entity(self, entity): """ creates a template for diplaying a concept or vocabulary entity, either in HTML or in JSON """ self.entity = entity self.uri = entity.uri self.vocab_uri = entity.vocab_uri self.project_uuid = entity.project_uuid # add the version control uri for vocab_key, version_uri in self.VERSION_CONTROL_LINKS.items(): if vocab_key == self.vocab_uri: self.version_control_uri = version_uri break self.get_comments() # always get comments for the entity if self.uri == self.vocab_uri: # we have a vocabulary so do vocab methods self.vocab_entity = entity self.get_root_entities() else: # we have a concept in a vocabulary, so do # concept methods ent_voc = Entity() vocab_found = ent_voc.dereference(self.vocab_uri) if vocab_found: # found the parent vocab entity self.vocab_entity = ent_voc self.get_entity_parents() self.get_entity_children()
def project_annotations_ttl(request, identifier): """ Returns RDF open annotation assertions conforming to Pelagios specifications that link Open Context resources with place entities in gazetteers, in the turtle format """ ent = Entity() found = ent.dereference(identifier) if found: if ent.item_type == 'projects': pp = ProjectPermissions(ent.uuid) permitted = pp.view_allowed(request) if permitted: pelagios = PelagiosGraph() pelagios.project_uuids = [ent.uuid] pelagios.test_limit = None pelagios.make_graph() # default to outputting in turtle output = pelagios.g.serialize(format='turtle') return HttpResponse(output, content_type='text/turtle; charset=utf8') else: return HttpResponse('Not authorized to get this resource', content_type='text/html; charset=utf8', status=401) else: found = False if found is False: raise Http404
def get_subject_type_fields(self, field_num_list=False): """ Gets a list of subject-type field objects, limited by a list of field_num if not false """ self.get_examples = True if field_num_list is not False: imp_fields = ImportField.objects\ .filter(source_id=self.source_id, field_type__in=self.DEFAULT_SUBJECT_TYPE_FIELDS, field_num__in=field_num_list) else: imp_fields = ImportField.objects\ .filter(source_id=self.source_id, field_type__in=self.DEFAULT_SUBJECT_TYPE_FIELDS) for field_obj in imp_fields: if field_obj.field_type == 'media': self.has_media_field = True if field_obj.field_type == 'documents': self.has_doc_field = True if field_obj.field_type == 'complex-description': self.has_complex_des_field = True field_obj.examples = self.get_example_entities(field_obj.field_num, field_obj.value_prefix) field_obj.ex_csv = ', '.join(field_obj.examples) if len(field_obj.field_value_cat) > 0: ent = Entity() ent.get_icon = True found = ent.dereference(field_obj.field_value_cat) if found: field_obj.field_value_cat_label = ent.label field_obj.field_value_cat_icon = ent.icon else: field_obj.field_value_cat_icon = False field_obj.field_value_cat_label = '' self.fields.append(field_obj)
def make_dict_from_field_obj(self, field_obj): """ returns an ordered dict for a field_obj """ field_dict = LastUpdatedOrderedDict() field_dict['field_num'] = field_obj.field_num field_dict['label'] = field_obj.label field_dict['ref_name'] = field_obj.ref_name field_dict['field_type'] = field_obj.field_type field_dict['field_data_type'] = field_obj.field_data_type field_dict['field_value_cat'] = field_obj.field_value_cat field_dict['obs_num'] = field_obj.obs_num field_dict['value_prefix'] = field_obj.value_prefix field_dict['unique_count'] = field_obj.unique_count field_dict['f_uuid'] = field_obj.f_uuid if self.get_examples: field_dict['examples'] = self.get_example_entities(field_obj.field_num, field_obj.value_prefix) field_dict['ex_csv'] = ', '.join(field_dict['examples']) if len(field_obj.field_value_cat) > 0: ent = Entity() ent.get_icon = True found = ent.dereference(field_obj.field_value_cat) if found: field_dict['field_value_cat_label'] = ent.label field_dict['field_value_cat_icon'] = ent.icon return field_dict
def get_subject_type_fields(self, field_num_list=False): """ Gets a list of subject-type field objects, limited by a list of field_num if not false """ self.get_examples = True if field_num_list is not False: imp_fields = ImportField.objects\ .filter(source_id=self.source_id, field_type__in=self.DEFAULT_SUBJECT_TYPE_FIELDS, field_num__in=field_num_list) else: imp_fields = ImportField.objects\ .filter(source_id=self.source_id, field_type__in=self.DEFAULT_SUBJECT_TYPE_FIELDS) for field_obj in imp_fields: if field_obj.field_type == 'media': self.has_media_field = True if field_obj.field_type == 'documents': self.has_doc_field = True if field_obj.field_type == 'complex-description': self.has_complex_des_field = True field_obj.examples = self.get_example_entities( field_obj.field_num, field_obj.value_prefix) field_obj.ex_csv = ', '.join(field_obj.examples) if len(field_obj.field_value_cat) > 0: ent = Entity() ent.get_icon = True found = ent.dereference(field_obj.field_value_cat) if found: field_obj.field_value_cat_label = ent.label field_obj.field_value_cat_icon = ent.icon else: field_obj.field_value_cat_icon = False field_obj.field_value_cat_label = '' self.fields.append(field_obj)
def make_dict_from_field_obj(self, field_obj): """ returns an ordered dict for a field_obj """ field_dict = LastUpdatedOrderedDict() field_dict['field_num'] = field_obj.field_num field_dict['label'] = field_obj.label field_dict['ref_name'] = field_obj.ref_name field_dict['field_type'] = field_obj.field_type field_dict['field_data_type'] = field_obj.field_data_type field_dict['field_value_cat'] = field_obj.field_value_cat field_dict['obs_num'] = field_obj.obs_num field_dict['value_prefix'] = field_obj.value_prefix field_dict['unique_count'] = field_obj.unique_count field_dict['f_uuid'] = field_obj.f_uuid if self.get_examples: field_dict['examples'] = self.get_example_entities( field_obj.field_num, field_obj.value_prefix) field_dict['ex_csv'] = ', '.join(field_dict['examples']) if len(field_obj.field_value_cat) > 0: ent = Entity() ent.get_icon = True found = ent.dereference(field_obj.field_value_cat) if found: field_dict['field_value_cat_label'] = ent.label field_dict['field_value_cat_icon'] = ent.icon return field_dict
def make_trinomial_from_site_labels(self, project_uuid, state_prefix=''): """ makes trinomial identifiers from a site label """ ent = Entity() found = ent.dereference(project_uuid) if found: proj_label = ent.label sites = Manifest.objects\ .filter(project_uuid=project_uuid, class_uri='oc-gen:cat-site') for site in sites: trinomial = str(state_prefix) + site.label if '*' in trinomial: # for North Carolina, only the part before the '*' is a trinomial tr_ex = trinomial.split('*') trinomial = tr_ex[0] print('working on (' + site.uuid + '): ' + trinomial) parts = self.parse_trinomial(trinomial) if 'Tennessee' in proj_label: trinomial = parts['state'] + parts['county'] + str( parts['site']) dt = Trinomial() dt.uri = URImanagement.make_oc_uri(site.uuid, site.item_type) dt.uuid = site.uuid dt.label = site.label dt.project_label = proj_label dt.trinomial = trinomial dt.state = parts['state'] dt.county = parts['county'] dt.site = parts['site'] try: dt.save() print('Trinomial: ' + trinomial + ', from: ' + site.label) except: print('Trinomial: ' + trinomial + ' not valid as a trinomial')
def make_trinomial_from_site_labels(self, project_uuid, state_prefix=''): """ makes trinomial identifiers from a site label """ ent = Entity() found = ent.dereference(project_uuid) if found: proj_label = ent.label sites = Manifest.objects\ .filter(project_uuid=project_uuid, class_uri='oc-gen:cat-site') for site in sites: trinomial = str(state_prefix) + site.label if '*' in trinomial: # for North Carolina, only the part before the '*' is a trinomial tr_ex = trinomial.split('*') trinomial = tr_ex[0] print('working on (' + site.uuid + '): ' + trinomial) parts = self.parse_trinomial(trinomial) if 'Tennessee' in proj_label: trinomial = parts['state'] + parts['county'] + str(parts['site']) dt = Trinomial() dt.uri = URImanagement.make_oc_uri(site.uuid, site.item_type) dt.uuid = site.uuid dt.label = site.label dt.project_label = proj_label dt.trinomial = trinomial dt.state = parts['state'] dt.county = parts['county'] dt.site = parts['site'] try: dt.save() print('Trinomial: ' + trinomial + ', from: ' + site.label) except: print('Trinomial: ' + trinomial + ' not valid as a trinomial')
def get_identifier_list_variants(self, id_list): """ makes different variants of identifiers for a list of identifiers """ output_list = [] if not isinstance(id_list, list): id_list = [str(id_list)] for identifier in id_list: output_list.append(identifier) if (identifier[:7] == 'http://' or identifier[:8] == 'https://'): oc_uuid = URImanagement.get_uuid_from_oc_uri(identifier) if oc_uuid is not False: output_list.append(oc_uuid) else: prefix_id = URImanagement.prefix_common_uri(identifier) output_list.append(prefix_id) elif ':' in identifier: full_uri = URImanagement.convert_prefix_to_full_uri(identifier) output_list.append(full_uri) else: # probably an open context uuid or a slug ent = Entity() found = ent.dereference(identifier) if found: full_uri = ent.uri output_list.append(full_uri) prefix_uri = URImanagement.prefix_common_uri(full_uri) if prefix_uri != full_uri: output_list.append(prefix_uri) return output_list
def get_jsonldish_parents(self, uuid, add_original=True): """ Gets parent projects for a project. Returns a list of dictionary objects similar to JSON-LD expectations This is useful for faceted search If add_original is true, add the original UUID for the entity that's the childmost item, at the bottom of the hierarchy """ output = False raw_parents = self.get_parents(uuid) if(add_original): # add the original identifer to the list of parents, at lowest rank raw_parents.insert(0, uuid) if len(raw_parents) > 0: # reverse the order of the list, to make top most concept # first output = [] parents = raw_parents[::-1] for par_id in parents: ent = Entity() found = ent.dereference(par_id) if(found): p_item = LastUpdatedOrderedDict() p_item['id'] = ent.uri p_item['slug'] = ent.slug p_item['label'] = ent.label if(ent.data_type is not False): p_item['type'] = ent.data_type else: p_item['type'] = '@id' output.append(p_item) return output
def get_parent_slug_by_slug(self, child_slug): """ gets the slug for a parent item from a child item's slug """ self.recurse_count = 0 self.contexts_list = [] self.contexts = {} output = False ent = Entity() found = ent.dereference(child_slug) if found: self.get_parents_by_child_uuid(ent.uuid, False) if len(self.contexts_list) > 0: parent_uuid = self.contexts_list[0] # clear class so we can use this again self.contexts_list = [] self.contexts = {} self.recurse_count = 0 ent_p = Entity() found_p = ent_p.dereference(parent_uuid) if found_p: output = ent_p.slug else: print('Cannot dereference parent_uuid: ' + parent_uuid) else: # print('No parent item found. (Root Context)') pass else: print('Cannot find the item for slug: ' + child_slug) return output
def add_child_entity(self, child_id, children_dict): """ creates a child entity dictionary object if found in the dictionary object of children and if the child entity can be dereferenced """ active_child = False if child_id in children_dict: active_child = LastUpdatedOrderedDict() ent = Entity() ent.get_icon = True found = ent.dereference(child_id) if found: active_child['label'] = ent.label active_child['id'] = child_id active_child['icon'] = ent.icon if children_dict[child_id] is not False: active_child['children'] = [] for child_child_id in children_dict[child_id]: child_child = self.add_child_entity( child_child_id, children_dict) if child_child is not False: active_child['children'].append(child_child) active_child['children'] = self.sort_children_by_label( active_child['children']) return active_child
def contain_children(request, identifier): """ Returns JSON data with spatial containment for a given uuid identiffied entity """ ent = Entity() found = ent.dereference(identifier) if found: depth = 1 recursive = False if 'depth' in request.GET: try: depth = int(float(request.GET['depth'])) except: depth = 1 et = EntityTemplate() children = et.get_containment_children(ent, depth) json_output = json.dumps(children, indent=4, ensure_ascii=False) return HttpResponse(json_output, content_type='application/json; charset=utf8') else: raise Http404
def look_up(request, item_type): """ Returns JSON data for entities limited by certain criteria """ ent = Entity() qstring = '' class_uri = False project_uuid = False vocab_uri = False ent_type = False context_uuid = False data_type = False if len(item_type) < 2: item_type = False if 'q' in request.GET: qstring = request.GET['q'] if 'class_uri' in request.GET: class_uri = request.GET['class_uri'] if 'project_uuid' in request.GET: project_uuid = request.GET['project_uuid'] if 'vocab_uri' in request.GET: vocab_uri = request.GET['vocab_uri'] if 'ent_type' in request.GET: ent_type = request.GET['ent_type'] if 'context_uuid' in request.GET: context_uuid = request.GET['context_uuid'] if 'data_type' in request.GET: data_type = request.GET['data_type'] entity_list = ent.search(qstring, item_type, class_uri, project_uuid, vocab_uri, ent_type, context_uuid, data_type) json_output = json.dumps(entity_list, indent=4, ensure_ascii=False) return HttpResponse(json_output, content_type='application/json; charset=utf8')
def get_identifier_list_variants(self, id_list): """ makes different variants of identifiers for a list of identifiers """ output_list = [] if not isinstance(id_list, list): id_list = [str(id_list)] for identifier in id_list: output_list.append(identifier) if(identifier[:7] == 'http://' or identifier[:8] == 'https://'): oc_uuid = URImanagement.get_uuid_from_oc_uri(identifier) if oc_uuid is not False: output_list.append(oc_uuid) else: prefix_id = URImanagement.prefix_common_uri(identifier) output_list.append(prefix_id) elif ':' in identifier: full_uri = URImanagement.convert_prefix_to_full_uri(identifier) output_list.append(full_uri) else: # probably an open context uuid or a slug ent = Entity() found = ent.dereference(identifier) if found: full_uri = ent.uri output_list.append(full_uri) prefix_uri = URImanagement.prefix_common_uri(full_uri) if prefix_uri != full_uri: output_list.append(prefix_uri) return output_list
def description_hierarchy(request, identifier): """ Returns JSON data with descriptive property and type hierarchies for a given uuid identiffied entity """ item_type = False class_uri = False if '/' in identifier: id_ex = identifier.split('/') identifier = id_ex[0] if len(id_ex) >= 2: item_type = id_ex[1] if len(id_ex) >= 3: class_uri = id_ex[2] ent = Entity() found = ent.dereference(identifier) if found: depth = 1 recursive = False if 'depth' in request.GET: try: depth = int(float(request.GET['depth'])) except: depth = 1 et = EntityTemplate() children = et.get_description_tree(ent, depth, True, item_type, class_uri) json_output = json.dumps(children, indent=4, ensure_ascii=False) return HttpResponse(json_output, content_type='application/json; charset=utf8') else: raise Http404
def get_entity_db(self, identifier, context_id, is_path=False): """ returns an entity object via database calls """ output = False found = False entity = Entity() if is_path: found = entity.context_dereference(identifier) else: found = entity.dereference(identifier) if found is False: # case of linked data slugs found = entity.dereference(identifier, identifier) if found: if is_path: self.entities[context_id] = entity else: self.entities[identifier] = entity if entity.uuid is not False: self.entities[entity.uuid] = entity if entity.slug is not False: self.entities[entity.slug] = entity output = entity return output
def id_summary(request, identifier): """ Returns JSON data for entities limited by certain criteria """ lequiv = LinkEquivalence() id_list = lequiv.get_identifier_list_variants(identifier) entity_obj = False for test_id in id_list: ent = Entity() found = ent.dereference(test_id) if found: entity_obj = LastUpdatedOrderedDict() entity_obj['id'] = ent.uri entity_obj['label'] = ent.label entity_obj['uuid'] = ent.uuid entity_obj['slug'] = ent.slug entity_obj['item_type'] = ent.item_type entity_obj['class_uri'] = ent.class_uri entity_obj['data_type'] = ent.data_type entity_obj['vocab_uri'] = ent.vocab_uri entity_obj['project_uuid'] = ent.project_uuid break if entity_obj is not False: json_output = json.dumps(entity_obj, indent=4, ensure_ascii=False) return HttpResponse(json_output, content_type='application/json; charset=utf8') else: raise Http404
def get_jsonldish_parents(self, uuid, add_original=True): """ Gets parent projects for a project. Returns a list of dictionary objects similar to JSON-LD expectations This is useful for faceted search If add_original is true, add the original UUID for the entity that's the childmost item, at the bottom of the hierarchy """ output = False raw_parents = self.get_parents(uuid) if (add_original): # add the original identifer to the list of parents, at lowest rank raw_parents.insert(0, uuid) if len(raw_parents) > 0: # reverse the order of the list, to make top most concept # first output = [] parents = raw_parents[::-1] for par_id in parents: ent = Entity() found = ent.dereference(par_id) if (found): p_item = LastUpdatedOrderedDict() p_item['id'] = ent.uri p_item['slug'] = ent.slug p_item['label'] = ent.label if (ent.data_type is not False): p_item['type'] = ent.data_type else: p_item['type'] = '@id' output.append(p_item) return output
def set_project_label(self): """ sets the project label """ if self.project_uuid is not False and self.project_label is False: ent = Entity() found = ent.dereference(self.project_uuid) if found: self.project_label = ent.label
def get_project_content(self): """ gets content for a project (and its subprojects) """ uuid = self.project_uuid self.subjects = False # will be a list if there is content self.media = False self.projects = False self.documents = False self.subjects_list = [] project_uuids = self.get_sub_project_uuids(uuid, []) if uuid not in project_uuids: project_uuids.append(uuid) # don't get rid of this! Keeps memory issues from # screwing things up man_content = Manifest.objects\ .filter(project_uuid__in=project_uuids)\ .order_by('class_uri')\ .values_list('item_type', 'class_uri')\ .distinct() if len(man_content) > 0: for content in man_content: item_type = content[0] class_uri = content[1] if item_type == 'subjects': self.subjects = '/subjects-search/' \ + '?proj=' + self.make_proj_query_term() if len(class_uri) > 0: ent = Entity() found = ent.dereference(class_uri) if found: obj = {} obj['label'] = ent.label obj['slug'] = ent.slug obj['link'] = self.subjects + '&prop=' + ent.slug self.subjects_list.append(obj) elif item_type == 'media': self.media = '/media-search/' \ + '?proj=' + self.make_proj_query_term() elif item_type == 'documents': self.documents = '/search/' \ + '?proj=' + self.make_proj_query_term() \ + '&type=documents' elif item_type == 'projects': self.projects = '/projects-search/' \ + '?proj=' + self.make_proj_query_term() else: pass if len(project_uuids) < 2: # don't show sub projects if there's only 1 project self.projects = False output = { 'subjects': self.subjects, 'media': self.media, 'documents': self.documents, 'projects': self.projects, 'subjects_list': self.subjects_list } return output
def augment_projects(self): """ adds some additional informaiton about projects to make them easier to display """ uuids = [] for proj_r in self.raw_records: uuids.append(proj_r.uuid) # now query the database for all the records with these uuids proj_objs = Project.objects\ .filter(uuid__in=uuids) # now make a dict object to easily get project info by a UUID key proj_obj_dict = {} for proj_obj in proj_objs: proj_obj_dict[proj_obj.uuid] = proj_obj # now query the database for all of the dc related predicates le = LinkEquivalence() subjects = le.get_identifier_list_variants(uuids) predicates = le.get_identifier_list_variants(self.DC_META_PREDS) dc_annos = LinkAnnotation.objects\ .filter(subject__in=subjects, predicate_uri__in=predicates) # now make a dict object to easily get annotations by UUID key dc_anno_dict = {} for dc_anno in dc_annos: dc_pred = URImanagement.prefix_common_uri(dc_anno.predicate_uri) dc_pred = dc_pred.replace('dc-terms:', '') # remove namespace prefix if dc_anno.subject not in dc_anno_dict: dc_anno_dict[dc_anno.subject] = {} if dc_pred not in dc_anno_dict[dc_anno.subject]: dc_anno_dict[dc_anno.subject][dc_pred] = [] if dc_anno.object_uri not in dc_anno_dict[dc_anno.subject][dc_pred]: dc_anno_dict[dc_anno.subject][dc_pred].append(dc_anno.object_uri) # now add information we got from queries and organized into dicts # to the project response objects for proj_r in self.raw_records: if proj_r.uuid in proj_obj_dict: # add projects objects from the database proj_r.extra = proj_obj_dict[proj_r.uuid] if proj_r.uuid in dc_anno_dict: # add annotations from the database proj_r.dc = {'meta': []} for pred, object_uris in dc_anno_dict[proj_r.uuid].items(): proj_r.dc[pred] = [] for object_uri in object_uris: ent = Entity() found = ent.dereference(object_uri) if found: obj_obj = {'id': object_uri, 'label': ent.label} if ent.item_type == 'uri': obj_obj['href'] = ent.uri else: obj_obj['href'] = self.base_url \ + '/' + ent.item_type \ + '/' + ent.uuid proj_r.dc[pred].append(obj_obj) if pred != 'creator' and pred != 'temporal': proj_r.dc['meta'].append(obj_obj) self.records.append(proj_r) # now append the augmented record
def make_dict_from_anno_obj(self, anno_obj): """ returns an ordered dict for an import field annotation object """ anno_dict = LastUpdatedOrderedDict() anno_dict['id'] = anno_obj.id sub_field_obj = self.get_field_object(anno_obj.field_num) if sub_field_obj is not False: anno_dict['subject'] = self.make_dict_from_field_obj(sub_field_obj) anno_dict['subject']['id'] = anno_obj.field_num else: anno_dict['subject'] = False if anno_obj.predicate_field_num > 0: pred_field_obj = self.get_field_object(anno_obj.predicate_field_num) anno_dict['predicate'] = self.make_dict_from_field_obj(pred_field_obj) anno_dict['predicate']['id'] = anno_obj.predicate_field_num anno_dict['predicate']['type'] = 'import-field' else: anno_dict['predicate'] = LastUpdatedOrderedDict() anno_dict['predicate']['id'] = anno_obj.predicate ent = Entity() found = ent.dereference(anno_obj.predicate) if found: anno_dict['predicate']['label'] = ent.label anno_dict['predicate']['type'] = ent.item_type elif anno_obj.predicate == ImportFieldAnnotation.PRED_CONTAINED_IN: anno_dict['predicate']['label'] = 'Contained in' elif anno_obj.predicate == ImportFieldAnnotation.PRED_DESCRIBES: anno_dict['predicate']['label'] = 'Describes' elif anno_obj.predicate == ImportFieldAnnotation.PRED_VALUE_OF: anno_dict['predicate']['label'] = 'Value of' elif anno_obj.predicate == ImportFieldAnnotation.PRED_MEDIA_PART_OF: anno_dict['predicate']['label'] = 'Media part of' elif anno_obj.predicate == ImportFieldAnnotation.PRED_DOC_Text: anno_dict['predicate']['label'] = 'Has document text' elif anno_obj.predicate == ImportFieldAnnotation.PRED_DRAFT_CONTAINS: anno_dict['predicate']['label'] = 'Contains (part. hierarchy)' elif anno_obj.predicate == ImportFieldAnnotation.PRED_COMPLEX_DES: anno_dict['predicate']['label'] = 'Has Complex Description' elif anno_obj.predicate == ImportFieldAnnotation.PRED_OBS_NUM: anno_dict['predicate']['label'] = 'Is Observation Number Of' else: anno_dict['predicate']['label'] = False anno_dict['predicate']['type'] = False if anno_obj.object_field_num > 0: obj_field_obj = self.get_field_object(anno_obj.object_field_num) anno_dict['object'] = self.make_dict_from_field_obj(obj_field_obj) anno_dict['object']['id'] = anno_obj.object_field_num anno_dict['object']['type'] = 'import-field' else: anno_dict['object'] = LastUpdatedOrderedDict() anno_dict['object']['id'] = anno_obj.object_uuid ent = Entity() found = ent.dereference(anno_obj.object_uuid) if found: anno_dict['object']['label'] = ent.label anno_dict['object']['type'] = ent.item_type else: anno_dict['object']['label'] = False anno_dict['object']['type'] = False return anno_dict
def get_project_content(self): """ gets content for a project (and its subprojects) """ uuid = self.project_uuid self.subjects = False # will be a list if there is content self.media = False self.projects = False self.documents = False self.subjects_list = [] project_uuids = self.get_sub_project_uuids(uuid, []) if uuid not in project_uuids: project_uuids.append(uuid) # don't get rid of this! Keeps memory issues from # screwing things up man_content = Manifest.objects\ .filter(project_uuid__in=project_uuids)\ .order_by('class_uri')\ .values_list('item_type', 'class_uri')\ .distinct() if len(man_content) > 0: for content in man_content: item_type = content[0] class_uri = content[1] if item_type == 'subjects': self.subjects = '/subjects-search/' \ + '?proj=' + self.make_proj_query_term() if len(class_uri) > 0: ent = Entity() found = ent.dereference(class_uri) if found: obj = {} obj['label'] = ent.label obj['slug'] = ent.slug obj['link'] = self.subjects + '&prop=' + ent.slug self.subjects_list.append(obj) elif item_type == 'media': self.media = '/media-search/' \ + '?proj=' + self.make_proj_query_term() elif item_type == 'documents': self.documents = '/search/' \ + '?proj=' + self.make_proj_query_term() \ + '&type=documents' elif item_type == 'projects': self.projects = '/projects-search/' \ + '?proj=' + self.make_proj_query_term() else: pass if len(project_uuids) < 2: # don't show sub projects if there's only 1 project self.projects = False output = {'subjects': self.subjects, 'media': self.media, 'documents': self.documents, 'projects': self.projects, 'subjects_list': self.subjects_list} return output
def get_containment_children(self, entity_obj, depth=1, first_time=True): """ gets a spatial containment hierarchy. If the entity_obj.item_type = 'projects' then the get the project's root parents. It the entity_obj.item_type is subjects, then get items containtained by the uuid """ cont = Containment() if entity_obj.item_type == 'projects': tree = self.make_containment_item(entity_obj) tree['children'] = [] child_list = cont.get_project_top_level_contexts(entity_obj.uuid) for child_uuid in child_list: child_ent = Entity() found = child_ent.dereference(child_uuid) if found: if depth > 1: child = self.get_containment_children(child_ent, depth - 1, False) else: child = self.make_containment_item(child_ent) tree['children'].append(child) if first_time: output = [] output.append(tree) else: output = tree elif entity_obj.item_type == 'subjects': tree = self.make_containment_item(entity_obj) tree['children'] = [] path_dict = cont.get_children_by_parent_uuid(entity_obj.uuid, False) for path_key, child_list in path_dict.items(): for child_uuid in child_list: child_ent = Entity() found = child_ent.dereference(child_uuid) if found: if depth > 1: child = self.get_containment_children(child_ent, depth - 1, False) else: child = self.make_containment_item(child_ent) tree['children'].append(child) if first_time: output = [] output.append(tree) else: output = tree else: output = [] return output
def html_view(request, identifier): rp = RootPath() base_url = rp.get_baseurl() uri = 'http://opencontext.org/vocabularies/' + str(identifier) lequiv = LinkEquivalence() id_list = lequiv.get_identifier_list_variants(uri) lequiv = LinkEquivalence() id_s_list = lequiv.get_identifier_list_variants(uri + '/') for id_s in id_s_list: if id_s not in id_list: # add the slashed version to the list id_list.append(id_s) entity = False for test_id in id_list: ent = Entity() found = ent.dereference(test_id) if found is False: found = ent.dereference(test_id, test_id) if found: entity = ent break if entity is not False: t_vocab = TemplateVocab() t_vocab.create_template_for_entity(entity) t_vocab.make_json_for_html() req_neg = RequestNegotiation('text/html') req_neg.supported_types = ['application/ld+json', 'application/json'] if 'HTTP_ACCEPT' in request.META: req_neg.check_request_support(request.META['HTTP_ACCEPT']) if req_neg.supported: if 'json' in req_neg.use_response_type: # content negotiation requested JSON or JSON-LD json_obj = t_vocab.make_json_obj() return HttpResponse(json.dumps(json_obj, ensure_ascii=False, indent=4), content_type=req_neg.use_response_type + "; charset=utf8") else: template = loader.get_template('vocabularies/view.html') context = { 'item': t_vocab, 'base_url': base_url, 'page_title': 'Open Context: Vocabularies + Ontologies', 'act_nav': 'vocabularies', 'nav_items': settings.NAV_ITEMS } return HttpResponse(template.render(context, request)) else: # client wanted a mimetype we don't support return HttpResponse(req_neg.error_message, content_type="text/plain; charset=utf8", status=415) else: raise Http404
def project_annotations(request, identifier): """ Returns RDF open annotation assertions conforming to Pelagios specifications that link Open Context resources with place entities in gazetteers """ ent = Entity() found = ent.dereference(identifier) if found or identifier == 'web': if ent.item_type == 'projects' or identifier == 'web': pelagios = PelagiosGraph() pelagios.test_limit = None if 'refresh' in request.GET: # we're going to refresh the cache pelagios.refresh_cache = True if identifier != 'web': pp = ProjectPermissions(ent.uuid) permitted = pp.view_allowed(request) pelagios.project_uuids = [ent.uuid] else: # we're doing web annotations pelagios.do_web_annotations = True permitted = True if permitted: pelagios.get_graph() req_neg = RequestNegotiation('text/turtle') req_neg.supported_types = [ 'application/rdf+xml', 'text/n3', 'application/n-triples' ] if 'HTTP_ACCEPT' in request.META: req_neg.check_request_support(request.META['HTTP_ACCEPT']) if req_neg.supported: content_type = req_neg.use_response_type + '; charset=utf8' output = pelagios.g.serialize( format=req_neg.use_response_type) return HttpResponse(output, content_type=content_type) else: # client wanted a mimetype we don't support return HttpResponse( req_neg.error_message, content_type="text/plain; charset=utf8", status=415) else: # default to outputting in turtle output = pelagios.g.serialize(format='turtle') return HttpResponse( output, content_type='text/turtle; charset=utf8') else: return HttpResponse('Not authorized to get this resource', content_type='text/html; charset=utf8', status=401) else: found = False if found is False: raise Http404
def project_annotations(request, identifier): """ Returns RDF open annotation assertions conforming to Pelagios specifications that link Open Context resources with place entities in gazetteers """ ent = Entity() found = ent.dereference(identifier) if found or identifier == 'web': if ent.item_type == 'projects' or identifier == 'web': pelagios = PelagiosGraph() pelagios.test_limit = None if 'refresh' in request.GET: # we're going to refresh the cache pelagios.refresh_cache = True if identifier != 'web': pp = ProjectPermissions(ent.uuid) permitted = pp.view_allowed(request) pelagios.project_uuids = [ent.uuid] else: # we're doing web annotations pelagios.do_web_annotations = True permitted = True if permitted: pelagios.get_graph() req_neg = RequestNegotiation('text/turtle') req_neg.supported_types = ['application/rdf+xml', 'text/n3', 'application/n-triples'] if 'HTTP_ACCEPT' in request.META: req_neg.check_request_support(request.META['HTTP_ACCEPT']) if req_neg.supported: content_type = req_neg.use_response_type + '; charset=utf8' output = pelagios.g.serialize(format=req_neg.use_response_type) return HttpResponse(output, content_type=content_type) else: # client wanted a mimetype we don't support return HttpResponse(req_neg.error_message, content_type="text/plain; charset=utf8", status=415) else: # default to outputting in turtle output = pelagios.g.serialize(format='turtle') return HttpResponse(output, content_type='text/turtle; charset=utf8') else: return HttpResponse('Not authorized to get this resource', content_type='text/html; charset=utf8', status=401) else: found = False if found is False: raise Http404
def get_children(self, identifier): """ Gets SKOS or OWL children for an entity """ ent = Entity() found = ent.dereference(identifier) if found: self.children = [] lr = LinkRecursion() lr.get_entity_children(identifier) self.children = lr.child_entities return self.children
def make_zendo_keywords_for_media_files(self, dir_dict): """ makes a list of keywords based on categories from the dir_dict """ zenodo_list = [] if 'category' in dir_dict: if isinstance(dir_dict['category'], list): for obj_dict in dir_dict['category']: ent = Entity() found = ent.dereference(obj_dict['id']) if found: if ent.label not in zenodo_list: zenodo_list.append(ent.label) return zenodo_list
def get_entity(self, identifier): """ gets entities, but checkes first if they are in memory """ output = False if identifier in self.mem_cache_entities: output = self.mem_cache_entities[identifier] else: ent = Entity() found = ent.dereference(identifier) if found: output = ent self.mem_cache_entities[identifier] = ent return output
def context_dereference(self, context): """ looks up a context, described as a '/' seperated list of labels """ ent = Entity() output = False try: subject = Subject.objects.filter(context=context)[:1] except Subject.DoesNotExist: subject = False if subject is not False: if len(subject) == 1: output = ent.dereference(subject[0].uuid) self.entity = ent return output
def make_object_dict_item(self, identifier): """ makes a dict item for the object of a predicate """ item = LastUpdatedOrderedDict() item['id'] = identifier item['label'] = False ent = Entity() found = ent.dereference(identifier) if found is False: found = ent.dereference(identifier, identifier) if found: item['label'] = ent.label item['slug'] = ent.slug return item
def make_zendo_keywords_for_media_files(self, dir_dict): """ makes zenodo keywords from a directory contents object """ zenodo_keywords = [] if 'category' in dir_dict: if instance(dir_dict['category'], list): for cat_obj in dir_dict['category']: ent = Entity() found = ent.dereference(cat_obj['id']) if found: zenodo_keywords.append(ent.label) return zenodo_keywords
def get_orphan_items(self, item_type, consider=['contain', 'link']): """ gets a list of items that do not have descriptions """ assertion_limit = '' if 'contain' in consider: assertion_limit += ' AND oc_assertions.predicate_uuid !=' assertion_limit += '\''+ Assertion.PREDICATES_CONTAINS + '\'' if 'link' in consider: assertion_limit += ' AND oc_assertions.predicate_uuid !=' assertion_limit += '\''+ Assertion.PREDICATES_LINK + '\'' sql = 'SELECT oc_manifest.uuid AS uuid, \ oc_manifest.label AS label, \ oc_manifest.item_type AS item_type, \ oc_manifest.class_uri AS class_uri \ FROM oc_manifest \ LEFT JOIN oc_assertions ON \ (oc_manifest.uuid = oc_assertions.uuid \ ' + assertion_limit + ') \ WHERE oc_manifest.project_uuid = \ \'' + self.project_uuid + '\' \ AND oc_manifest.item_type = \ \'' + item_type + '\' \ AND oc_assertions.uuid IS NULL \ ORDER BY oc_manifest.sort; ' non_descript = Manifest.objects.raw(sql) for dull_man in non_descript: item_type = dull_man.item_type class_uri = dull_man.class_uri if len(class_uri) < 1: class_uri = item_type if item_type not in self.blank_items: self.blank_items[item_type] = LastUpdatedOrderedDict() if class_uri not in self.blank_items[item_type]: class_dict = LastUpdatedOrderedDict() ent = Entity() found = ent.dereference(class_uri) if found: class_dict['label'] = ent.label else: class_dict['label'] = item_type class_dict['items'] = [] self.blank_items[item_type][class_uri] = class_dict item = LastUpdatedOrderedDict() item['uuid'] = dull_man.uuid item['label'] = dull_man.label self.blank_items[item_type][class_uri]['items'].append(item) return self.blank_items
def add_person_names_to_obj(self, obj_dict, default_to_label=True): """ adds person names to a JSON-LD object dict """ obj_dict['family_given_name'] = None if 'id' in obj_dict: act_id = obj_dict['id'] elif '@id' in obj_dict: act_id = obj_dict['@id'] else: act_id = None if default_to_label: if 'label' in obj_dict: if isinstance(obj_dict['label'], str): obj_dict['family_given_name'] = obj_dict['label'] if isinstance(act_id, str): ent = Entity() found = ent.dereference(act_id) if found: obj_dict['family_given_name'] = ent.label if ent.item_type == 'persons': surname = '' given_name = '' try: pers = Person.objects.get(uuid=ent.uuid) except Person.DoesNotExist: pers = None if pers is not None: if isinstance(pers.surname, str): if len(pers.surname.strip()) > 0: surname = pers.surname.strip() if isinstance(pers.given_name, str): if len(pers.given_name.strip()) > 0: given_name = pers.given_name.strip() obj_dict['family_given_name'] = surname + ', ' + given_name if isinstance(pers.mid_init, str): if len(pers.mid_init.strip()) > 0: obj_dict['family_given_name'] += ' ' + pers.mid_init.strip() return obj_dict def make_zendo_keywords_for_media_files(self, dir_dict): """ makes zenodo keywords from a directory contents object """ zenodo_keywords = [] if 'category' in dir_dict: if instance(dir_dict['category'], list): for cat_obj in dir_dict['category']: ent = Entity() found = ent.dereference(cat_obj['id']) if found: zenodo_keywords.append(ent.label) return zenodo_keywords
def get_context_from_id(self, identifier): """ Returns the context path from an identifier, including a slug """ output = False ent = Entity() found = ent.dereference(identifier) if found: print(str(ent.uuid)) try: sub_obj = Subject.objects.get(str(ent.uuid)) except Subject.DoesNotExist: sub_obj = False if sub_obj is not False: output = sub_obj.context return output
def get_orphan_items(self, item_type, consider=['contain', 'link']): """ gets a list of items that do not have descriptions """ assertion_limit = '' if 'contain' in consider: assertion_limit += ' AND oc_assertions.predicate_uuid !=' assertion_limit += '\'' + Assertion.PREDICATES_CONTAINS + '\'' if 'link' in consider: assertion_limit += ' AND oc_assertions.predicate_uuid !=' assertion_limit += '\'' + Assertion.PREDICATES_LINK + '\'' sql = 'SELECT oc_manifest.uuid AS uuid, \ oc_manifest.label AS label, \ oc_manifest.item_type AS item_type, \ oc_manifest.class_uri AS class_uri \ FROM oc_manifest \ LEFT JOIN oc_assertions ON \ (oc_manifest.uuid = oc_assertions.uuid \ ' + assertion_limit + ') \ WHERE oc_manifest.project_uuid = \ \'' + self.project_uuid + '\' \ AND oc_manifest.item_type = \ \'' + item_type + '\' \ AND oc_assertions.uuid IS NULL \ ORDER BY oc_manifest.sort; ' non_descript = Manifest.objects.raw(sql) for dull_man in non_descript: item_type = dull_man.item_type class_uri = dull_man.class_uri if len(class_uri) < 1: class_uri = item_type if item_type not in self.blank_items: self.blank_items[item_type] = LastUpdatedOrderedDict() if class_uri not in self.blank_items[item_type]: class_dict = LastUpdatedOrderedDict() ent = Entity() found = ent.dereference(class_uri) if found: class_dict['label'] = ent.label else: class_dict['label'] = item_type class_dict['items'] = [] self.blank_items[item_type][class_uri] = class_dict item = LastUpdatedOrderedDict() item['uuid'] = dull_man.uuid item['label'] = dull_man.label self.blank_items[item_type][class_uri]['items'].append(item) return self.blank_items
def add_period_coverage(self, uuid, period_uri): """ Adds an periodo uri annotation to an item """ ok = False po_api = PeriodoAPI() if not isinstance(self.periodo_data, dict): self.check_add_period_pred() po_api.get_periodo_data() self.periodo_data = po_api.periodo_data else: po_api.periodo_data = self.periodo_data if isinstance(po_api.periodo_data, dict): period = po_api.get_period_by_uri(period_uri) if isinstance(period, dict): # we found the period, now check the UUID # is found entity = Entity() found = entity.dereference(uuid) if found: # save the period collection entity to database, if needed self.check_add_period_collection(period) # save the period entity to the database, if needed self.check_add_period(period) # check to make sure the annotation does not yet exist # do so by checking all possible varients in expressing # this annotation lequiv = LinkEquivalence() subjects = lequiv.get_identifier_list_variants(uuid) predicates = lequiv.get_identifier_list_variants( self.DC_PERIOD_PRED) objects = lequiv.get_identifier_list_variants( period['period-meta']['uri']) la_exists = LinkAnnotation.objects\ .filter(subject__in=subjects, predicate_uri__in=predicates, object_uri__in=objects)[:1] if len(la_exists) < 1: # OK save to make the annotation new_la = LinkAnnotation() new_la.subject = entity.uuid new_la.subject_type = entity.item_type new_la.project_uuid = entity.project_uuid new_la.source_id = self.source_id new_la.predicate_uri = self.DC_PERIOD_PRED new_la.object_uri = period['period-meta']['uri'] new_la.creator_uuid = '' new_la.save() ok = True return ok
def deref_entity_label(self, entity_id): """ Dereferences an entity """ output = False if entity_id in self.entities: ent = self.entities[entity_id] output = ent.label else: ent = Entity() found = ent.dereference(entity_id) if found: output = ent.label self.entities[entity_id] = ent else: print('Missing id: ' + entity_id) return output
def _get_cache_entity_db(self, identifier): """Get an entity object from the database, if successful, cache it.""" found = False entity = Entity() entity.get_context = True entity.get_thumbnail = True found = entity.dereference(identifier) if not found: # case of linked data slugs found = entity.dereference(identifier, identifier) if not found: return False else: # cache the entity now return self.cache_entity(entity)
def add_period_annoation(self, p_ref): """ adds a period annotation """ entity = Entity() found = entity.dereference(p_ref['oc-uri']) if found: new_la = LinkAnnotation() new_la.subject = entity.uuid new_la.subject_type = entity.item_type new_la.project_uuid = entity.project_uuid new_la.source_id = self.source_id new_la.predicate_uri = 'dc-terms:isReferencedBy' new_la.object_uri = p_ref['period-meta']['uri'] new_la.creator_uuid = '' new_la.save() return found
def replace_hierarchy(self, old_parent, new_parent): """ replaces hirearchy annotations, so that children of the old_parent become children of the new_parent """ ok = False lequiv = LinkEquivalence() old_parent_ids = lequiv.get_identifier_list_variants(old_parent) p_for_superobjs = LinkAnnotation.PREDS_SBJ_IS_SUB_OF_OBJ preds_for_superobjs = lequiv.get_identifier_list_variants( p_for_superobjs) p_for_subobjs = LinkAnnotation.PREDS_SBJ_IS_SUPER_OF_OBJ preds_for_subobjs = lequiv.get_identifier_list_variants(p_for_subobjs) new_parent_entity_obj = False new_parent_entity_obj = Entity() found = new_parent_entity_obj.dereference(new_parent) if found: ok = True # get children (the subjects) where the parent is a superclass object child_subs_by_superobjs = LinkAnnotation.objects\ .filter(object_uri__in=old_parent_ids, predicate_uri__in=preds_for_superobjs) for child_subj in child_subs_by_superobjs: new_parent_superobj = child_subj del_hash_id = child_subj.hash_id # change the object (the super class) to the new parent new_parent_superobj.object_uri = new_parent_entity_obj.uri new_parent_superobj.source_id = self.source_id LinkAnnotation.objects\ .filter(hash_id=del_hash_id).delete() new_parent_superobj.save() # get children (the objects) where the parent is a superclass subject child_objs_by_subobjs = LinkAnnotation.objects\ .filter(subject__in=old_parent_ids, predicate_uri__in=preds_for_subobjs) for child_obj in child_objs_by_subobjs: new_parent_supersubj = child_obj del_hash_id = child_obj.hash_id # change the subject (the super class) to the new parent if isinstance(new_parent_superobj.uuid, str): new_parent_supersubj.subject = new_parent_superobj.uuid else: new_parent_supersubj.subject = new_parent_superobj.uri new_parent_supersubj.subject_type = new_parent_superobj.item_type new_parent_supersubj.source_id = self.source_id LinkAnnotation.objects\ .filter(hash_id=del_hash_id).delete() new_parent_supersubj.save() return ok
def get_field_parent_entity(self, field_num): """ Get's a parent entity named for a given field """ parent_entity_found = False self.field_parent_entities[field_num] = False parent_anno = ImportFieldAnnotation.objects\ .filter(source_id=self.source_id, field_num=field_num, predicate=ImportFieldAnnotation.PRED_CONTAINED_IN)[:1] if len(parent_anno) > 0: ent = Entity() ent.get_context = True found = ent.dereference(parent_anno[0].object_uuid) if found: self.field_parent_entities[field_num] = ent parent_entity_found = True return parent_entity_found
def replace_hierarchy(self, old_parent, new_parent): """ replaces hirearchy annotations, so that children of the old_parent become children of the new_parent """ ok = False lequiv = LinkEquivalence() old_parent_ids = lequiv.get_identifier_list_variants(old_parent) p_for_superobjs = LinkAnnotation.PREDS_SBJ_IS_SUB_OF_OBJ preds_for_superobjs = lequiv.get_identifier_list_variants(p_for_superobjs) p_for_subobjs = LinkAnnotation.PREDS_SBJ_IS_SUPER_OF_OBJ preds_for_subobjs = lequiv.get_identifier_list_variants(p_for_subobjs) new_parent_entity_obj = False new_parent_entity_obj = Entity() found = new_parent_entity_obj.dereference(new_parent) if found: ok = True # get children (the subjects) where the parent is a superclass object child_subs_by_superobjs = LinkAnnotation.objects\ .filter(object_uri__in=old_parent_ids, predicate_uri__in=preds_for_superobjs) for child_subj in child_subs_by_superobjs: new_parent_superobj = child_subj del_hash_id = child_subj.hash_id # change the object (the super class) to the new parent new_parent_superobj.object_uri = new_parent_entity_obj.uri new_parent_superobj.source_id = self.source_id LinkAnnotation.objects\ .filter(hash_id=del_hash_id).delete() new_parent_superobj.save() # get children (the objects) where the parent is a superclass subject child_objs_by_subobjs = LinkAnnotation.objects\ .filter(subject__in=old_parent_ids, predicate_uri__in=preds_for_subobjs) for child_obj in child_objs_by_subobjs: new_parent_supersubj = child_obj del_hash_id = child_obj.hash_id # change the subject (the super class) to the new parent if isinstance(new_parent_superobj.uuid, str): new_parent_supersubj.subject = new_parent_superobj.uuid else: new_parent_supersubj.subject = new_parent_superobj.uri new_parent_supersubj.subject_type = new_parent_superobj.item_type new_parent_supersubj.source_id = self.source_id LinkAnnotation.objects\ .filter(hash_id=del_hash_id).delete() new_parent_supersubj.save() return ok
def get_cache_entity(self, identifier, get_icon=False): """ gets an entity either from memory or from the database, if from the database, cache the entity in memory to make lookups faster """ output = False if identifier in self.mem_entities: # found in memory output = self.mem_entities[identifier] else: ent = Entity() ent.get_icon = get_icon found = ent.dereference(identifier) if found: self.mem_entities[identifier] = ent output = ent return output
def create_proj_object_perm_groups(self, project_obj): """ Creates permissions groups for a project object """ output = False ent = Entity() found = ent.dereference(project_obj.uuid) if found: view_group = Group() view_group.name = str(ent.label[:60]) + ' [Can View]' view_group.save() project_obj.view_group_id = view_group.id edit_group = Group() edit_group.name = str(ent.label[:60]) + ' [Can Edit]' edit_group.save() project_obj.edit_group_id = edit_group.id project_obj.save() output = project_obj return output