def get_uri_children(self, uri): """ gets the children for a given uri """ output = [] if isinstance(uri, str): lr = LinkRecursion() lr.get_entity_children(uri, False) children = lr.child_entities if isinstance(children, dict): child_uris = [] for ch_uri, ch_list in children.items(): if uri == ch_uri: child_uris = ch_list break # now we have child entity uris, put them in the proper order lequiv = LinkEquivalence() child_uris = lequiv.get_identifier_list_variants(child_uris) child_ents = LinkEntity.objects\ .filter(uri__in=child_uris)\ .exclude(uri=self.uri)\ .order_by('sort', 'label', 'uri') for act_ent in child_ents: ent_dict = LastUpdatedOrderedDict() ent_dict['id'] = act_ent.uri ent_dict['label'] = act_ent.label ent_dict['slug'] = act_ent.slug ent_dict['href'] = self.make_local_url(act_ent.uri) output.append(ent_dict) return output
def _get_entity_children_db(self, identifier, recursive=True): """ Gets child concepts for a given URI or UUID identified entity """ if not self.child_entities: self.child_entities = LastUpdatedOrderedDict() if identifier in self.child_entities and recursive: output = self.child_entities[identifier] else: act_children = [] p_for_superobjs = LinkAnnotation.PREDS_SBJ_IS_SUB_OF_OBJ p_for_subobjs = LinkAnnotation.PREDS_SBJ_IS_SUPER_OF_OBJ lequiv = LinkEquivalence() identifiers = lequiv.get_identifier_list_variants(identifier) try: # look for child items in the objects of the assertion subobjs_anno = LinkAnnotation.objects.filter(subject__in=identifiers, predicate_uri__in=p_for_subobjs) if(len(subobjs_anno) < 1): subobjs_anno = False except LinkAnnotation.DoesNotExist: subobjs_anno = False if subobjs_anno is not False: for sub_obj in subobjs_anno: child_id = sub_obj.object_uri act_children.append(child_id) try: """ Now look for subordinate entities in the subject, not the object """ subsubj_anno = LinkAnnotation.objects.filter(object_uri__in=identifiers, predicate_uri__in=p_for_superobjs) if len(subsubj_anno) < 1: subsubj_anno = False except LinkAnnotation.DoesNotExist: subsubj_anno = False if subsubj_anno is not False: for sub_sub in subsubj_anno: child_id = sub_sub.subject act_children.append(child_id) if len(act_children) > 0: identifier_children = [] for child_id in act_children: if child_id.count('/') > 1: oc_uuid = URImanagement.get_uuid_from_oc_uri(child_id) if oc_uuid: child_id = oc_uuid identifier_children.append(child_id) # recursively get the children of the child if recursive: self.get_entity_children(child_id, recursive) # same the list of children of the current identified item if identifier not in self.child_entities: self.child_entities[identifier] = identifier_children else: # save a False for the current identified item. it has no children if identifier not in self.child_entities: self.child_entities[identifier] = [] output = self.child_entities[identifier] 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() 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 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 get_dtypes_db(self, entity_uri): """ returns an entity data type """ # haven't found it yet, so look in database lequiv = LinkEquivalence() lequiv.predicates = self.predicates dtypes = lequiv.get_data_types_from_object(entity_uri) self.predicates = lequiv.predicates self.entity_dtypes[entity_uri] = dtypes return dtypes
def get_entity_parents(self, identifier): """ Gets parent concepts for a given URI or UUID identified entity """ self.loop_count += 1 lequiv = LinkEquivalence() identifiers = lequiv.get_identifier_list_variants(identifier) 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) try: # look for superior items in the objects of the assertion # sorting by sort so we can privelage a certain hierarchy path superobjs_anno = LinkAnnotation.objects.filter(subject__in=identifiers, predicate_uri__in=preds_for_superobjs)\ .exclude(object_uri__in=identifiers)\ .order_by('sort', 'object_uri')[:1] if(len(superobjs_anno) < 1): superobjs_anno = False except LinkAnnotation.DoesNotExist: superobjs_anno = False if(superobjs_anno is not False): parent_id = superobjs_anno[0].object_uri if(parent_id.count('/') > 1): oc_uuid = URImanagement.get_uuid_from_oc_uri(parent_id) if(oc_uuid is not False): parent_id = oc_uuid if(parent_id not in self.parent_entities): self.parent_entities.append(parent_id) if self.loop_count <= 50: self.parent_entities = self.get_entity_parents(parent_id) try: """ Now look for superior entities in the subject, not the object sorting by sort so we can privelage a certain hierarchy path """ supersubj_anno = LinkAnnotation.objects.filter(object_uri__in=identifiers, predicate_uri__in=preds_for_subobjs)\ .exclude(subject__in=identifiers)\ .order_by('sort', 'subject')[:1] if(len(supersubj_anno) < 1): supersubj_anno = False except LinkAnnotation.DoesNotExist: supersubj_anno = False if supersubj_anno is not False: parent_id = supersubj_anno[0].subject if(parent_id.count('/') > 1): oc_uuid = URImanagement.get_uuid_from_oc_uri(parent_id) if(oc_uuid is not False): parent_id = oc_uuid if(parent_id not in self.parent_entities): self.parent_entities.append(parent_id) if self.loop_count <= 50: self.parent_entities = self.get_entity_parents(parent_id) return self.parent_entities
def get_geochron_from_subject_list(self, subject_list, metadata_type, do_parents=True): """ gets the most specific geospatial or chronology metadata related to a list of subject items if not found, looks up parent items """ metadata_items = False if(len(subject_list) < 1): # can't find a related subject uuid # print(" Sad, an empty list! \n") return metadata_items else: if do_parents: self.contexts = {} self.contexts_list = [] for search_uuid in subject_list: # print(" trying: " + search_uuid + "\n") if metadata_type == 'geo': metadata_items = Geospace.objects.filter(uuid=search_uuid) if len(metadata_items) >= 1: break else: metadata_items = False elif metadata_type == 'temporal': # get temporal metadata lequiv = LinkEquivalence() subjects = lequiv.get_identifier_list_variants(search_uuid) predicates = lequiv.get_identifier_list_variants('dc-terms:temporal') metadata_items = LinkAnnotation.objects\ .filter(subject__in=subjects, predicate_uri__in=predicates) if len(metadata_items) >= 1: break else: metadata_items = False else: metadata_items = Event.objects.filter(uuid=search_uuid) if len(metadata_items) >= 1: break else: # can't find any vent, build a list of parent uuids to search metadata_items = False if isinstance(metadata_items, list): if len(metadata_items) < 1: metadata_items = False if do_parents and metadata_items is False: self.recurse_count = 0 self.get_parents_by_child_uuid(search_uuid) if(metadata_items is False and do_parents): # print(" going for parents: " + str(self.contexts_list) + "\n") # use the list of parent uuid's from the context_list. It's in order of more # specific to more general metadata_items = self.get_geochron_from_subject_list(self.contexts_list, metadata_type, False) return metadata_items
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 get_temporal_from_project(self, project_uuid): """ gets temporal metadata by association with a project """ lequiv = LinkEquivalence() subjects = lequiv.get_identifier_list_variants(project_uuid) predicates = lequiv.get_identifier_list_variants('dc-terms:temporal') metadata_items = LinkAnnotation.objects\ .filter(subject__in=subjects, predicate_uri__in=predicates) if len(metadata_items) < 1: metadata_items = False return metadata_items
def get_attributes(self, solr_rec): """ gets attributes for a record, based on the predicates requested in the search and optional predicates passed by a client with a GET request with parameter 'attributes' """ qm = QueryMaker() solr_field_entities = {} for attribute in self.rec_attributes: entity = self.get_entity(attribute) if entity is not False: prop_slug = entity.slug # check to make sure we have the entity data type for linked fields if entity.data_type is False and entity.item_type == 'uri': lequiv = LinkEquivalence() dtypes = lequiv.get_data_types_from_object(entity.uri) if isinstance(dtypes, list): # set te data type and the act-field # print('Found for ' + prop_slug + ' ' + dtypes[0]) entity.data_type = dtypes[0] if prop_slug in self.entities: self.entities[prop_slug] = entity # store entitty for later use field_parts = qm.make_prop_solr_field_parts(entity) solr_field = field_parts['prefix'] + '___pred_' + field_parts['suffix'] # extract children of the solr_field so we know if # we have the most specific attributes, then we can get # values for the most specific attributes self.extract_attribute_children(solr_rec, solr_field) self.clean_attribute_hiearchies() if isinstance(self.attribute_hierarchies, dict): self.other_attributes = [] for field_slug_key, values in self.attribute_hierarchies.items(): entity = self.get_entity(field_slug_key) if entity is not False: attribute_dict = LastUpdatedOrderedDict() attribute_dict['property'] = entity.label attribute_dict['values_list'] = [] attribute_dict['value'] = '' string_val = False delim = '' for val in values: if isinstance(val, str): string_val = True parsed_val = self.parse_solr_value_parts(val) attribute_dict["values_list"].append(parsed_val['label']) attribute_dict['value'] += delim + str(parsed_val['label']) else: attribute_dict["values_list"].append(val) attribute_dict['value'] += delim + str(val) delim = self.ATTRIBUTE_DELIM if len(values) == 1 \ and string_val is False: attribute_dict['value'] = values[0] self.other_attributes.append(attribute_dict)
def get_comments(self): """ gets comments for the entity (or vocabulary) """ if isinstance(self.uri, str): lequiv = LinkEquivalence() subjs = lequiv.get_identifier_list_variants(self.uri) lequiv = LinkEquivalence() # get all the varients of RDFS:comments comment_uris = lequiv.get_identifier_list_variants('rdfs:comment') commment_annos = LinkAnnotation.objects\ .filter(subject__in=subjs, predicate_uri__in=comment_uris)[:1] if len(commment_annos) > 0: self.comment = commment_annos[0].obj_extra lang = Languages() self.default_comment = lang.get_default_value_str(self.comment)
def check_new_annotation(self, subject, object_uri): """ checks to make sure we have a new annotation """ is_new = False # very comprehensive validation for all variants of subject, predicates and object identifiers link_equiv = LinkEquivalence() subject_list = link_equiv.get_identifier_list_variants(subject) predicate_list = link_equiv.get_identifier_list_variants(self.DC_TERMS_REFERENCED_BY) object_list = link_equiv.get_identifier_list_variants(object_uri) anno_list = LinkAnnotation.objects\ .filter(subject__in=subject_list, predicate_uri__in=predicate_list, object_uri__in=object_list)[:1] if len(anno_list) < 1: is_new = True return is_new
def _get_parent_id_db(self, identifier): """Get the parent id for the current identifier """ parent_id = None lequiv = LinkEquivalence() identifiers = lequiv.get_identifier_list_variants(identifier) # print('identifiers: {}'.format(identifiers)) 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) try: # look for superior items in the objects of the assertion # sorting by sort so we can privelage a certain hierarchy path superobjs_anno = LinkAnnotation.objects.filter(subject__in=identifiers, predicate_uri__in=preds_for_superobjs)\ .exclude(object_uri__in=identifiers)\ .order_by('sort', 'object_uri')[:1] if len(superobjs_anno) < 1: superobjs_anno = False except LinkAnnotation.DoesNotExist: superobjs_anno = False if superobjs_anno: parent_id = superobjs_anno[0].object_uri # print('Subject {} is child of {}'.format(identifiers, parent_id)) oc_uuid = URImanagement.get_uuid_from_oc_uri(parent_id) if oc_uuid: parent_id = oc_uuid try: """ Now look for superior entities in the subject, not the object sorting by sort so we can privelage a certain hierarchy path """ supersubj_anno = LinkAnnotation.objects.filter(object_uri__in=identifiers, predicate_uri__in=preds_for_subobjs)\ .exclude(subject__in=identifiers)\ .order_by('sort', 'subject')[:1] if len(supersubj_anno) < 1: supersubj_anno = False except LinkAnnotation.DoesNotExist: supersubj_anno = False if supersubj_anno: parent_id = supersubj_anno[0].subject # print('Subject {} is parent of {}'.format(parent_id, identifiers)) oc_uuid = URImanagement.get_uuid_from_oc_uri(parent_id) if oc_uuid: parent_id = oc_uuid return parent_id
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_db_geochron_from_search_uuid(self, search_uuid, metadata_type): """ gets geospatial, event, or temporal metadata objects for a search_uuid """ metadata_items = False if metadata_type == 'geo': metadata_items = Geospace.objects.filter(uuid=search_uuid) elif metadata_type == 'temporal': # get temporal metadata lequiv = LinkEquivalence() subjects = lequiv.get_identifier_list_variants(search_uuid) predicates = lequiv.get_identifier_list_variants('dc-terms:temporal') metadata_items = LinkAnnotation.objects\ .filter(subject__in=subjects, predicate_uri__in=predicates) else: metadata_items = Event.objects.filter(uuid=search_uuid) if isinstance(metadata_items, list): if len(metadata_items) < 1: metadata_items = False return metadata_items
def get_related_labels(self, uri, unique=False): """ gets labels for related URIs """ if uri not in self.raw_related_labels: le = LinkEquivalence() equiv_uuids = le.get_from_object(uri) self.raw_related_labels[uri] = [] for uuid in equiv_uuids: try: man_obj = Manifest.objects.get(uuid=uuid) except Manifest.DoesNotExist: man_obj = False if man_obj is not False: self.raw_related_labels[uri].append(man_obj.label) output = self.raw_related_labels[uri] if unique: output = [] for label in self.raw_related_labels[uri]: if label not in output: output.append(label) return output
def get_temporal_from_project(self, project_uuid): """ gets temporal metadata by association with a project """ metadata_items = None cache_id = self.cache_use.make_cache_key('meta-proj-temp-', project_uuid) if self.use_cache: # use the cache to look for metadata metadata_items = self.cache_use.get_cache_object(cache_id) if metadata_items is None: lequiv = LinkEquivalence() subjects = lequiv.get_identifier_list_variants(project_uuid) predicates = lequiv.get_identifier_list_variants('dc-terms:temporal') metadata_items = LinkAnnotation.objects\ .filter(subject__in=subjects, predicate_uri__in=predicates) if len(metadata_items) < 1: metadata_items = False if self.use_cache: self.cache_use.save_cache_object(cache_id, metadata_items) return metadata_items
def validate_class_uri(self, class_uri): """ validates a class_uri as actually identifiying a known class. Useful for making sure data entry is OK for the manifest table """ output = False if len(class_uri) > 0: le = LinkEquivalence() class_list = le.get_identifier_list_variants(class_uri) link_entities = LinkEntity.objects\ .filter(uri__in=class_list, vocab_uri__in=self.VALID_OC_CLASS_VOCABS)[:1] if len(link_entities) > 0: # OK! We found it. now make it prefixed for use in the manifest table full_class_uri = link_entities[0].uri output = URImanagement.prefix_common_uri(full_class_uri) else: if self.allow_blank: # we are allowing blank values for no class_uri to be OK output = '' return output
def json_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) json_obj = t_vocab.make_json_obj() req_neg = RequestNegotiation('application/ld+json') req_neg.supported_types = ['application/json'] if 'HTTP_ACCEPT' in request.META: req_neg.check_request_support(request.META['HTTP_ACCEPT']) if req_neg.supported: return HttpResponse(json.dumps(json_obj, ensure_ascii=False, indent=4), content_type=req_neg.use_response_type + "; charset=utf8") 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 check_pred_dc_author(self, pred_obj): """ some predicates have equivalence relations with DC-terms:contributor or DC-terms:creator properties. This method checks to see if a predicate has some sort of equivalence to a dublin core contributor or creator URI. If so """ if isinstance(pred_obj, dict): pred_obj['dc_contrib'] = False pred_obj['dc_creator'] = False if 'ent' in pred_obj: pred_ent = pred_obj['ent'] if hasattr(pred_ent, 'item_json_ld'): if isinstance(pred_ent.item_json_ld, dict): pred_json_ld = pred_ent.item_json_ld le = LinkEquivalence() equiv_keys = le.get_identifier_list_variants( LinkAnnotation.PREDS_SBJ_EQUIV_OBJ) dc_contrib_uris = le.get_identifier_list_variants( ItemKeys.PREDICATES_DCTERMS_CONTRIBUTOR) dc_creator_uris = le.get_identifier_list_variants( ItemKeys.PREDICATES_DCTERMS_CREATOR) for equiv_key in equiv_keys: if equiv_key in pred_json_ld: if isinstance(pred_json_ld[equiv_key], list): for pred_equiv in pred_json_ld[equiv_key]: uri = None if 'id' in pred_equiv: uri = pred_equiv['id'] elif '@id' in pred_equiv: uri = pred_equiv['@id'] if uri in dc_contrib_uris: pred_obj['dc_contrib'] = True break if uri in dc_creator_uris: pred_obj['dc_creator'] = True break if pred_obj['dc_contrib'] or pred_obj['dc_creator']: # no need to keep searching, we found equivalence break return pred_obj
def replace_object_uri(self, old_object_uri, new_object_uri): """ replaces annotations using a given old_object_uri with a new one """ lequiv = LinkEquivalence() old_obj_list = lequiv.get_identifier_list_variants(old_object_uri) la_objs = LinkAnnotation.objects\ .filter(object_uri__in=old_obj_list) print('Change object_uri for annotations: ' + str(len(la_objs))) for la_obj in la_objs: old_hash = la_obj.hash_id new_la = la_obj new_la.object_uri = new_object_uri try: new_la.save() ok = True except Exception as error: ok = False print("Error: " + str(error)) if ok: LinkAnnotation.objects\ .filter(hash_id=old_hash).delete()
def save_entity_comments(self): """ saves comments about an entity """ if self.graph is not False and self.vocabulary_uri is not False: lequiv = LinkEquivalence() # get all the varients of RDFS:comments comment_uris = lequiv.get_identifier_list_variants('rdfs:comment') # now get all the entities from this vocabulary (that may be the subject of a comment) raw_subject_uris = LinkEntity.objects.filter(vocab_uri=self.vocabulary_uri) lequiv = LinkEquivalence() subject_uris = lequiv.get_identifier_list_variants(raw_subject_uris) if self.replace_old: # delete the old comments LinkAnnotation.objects\ .filter(subject__in=subject_uris, predicate_uri__in=comment_uris)\ .delete() for s, p, o in self.graph.triples((None, RDFS.comment, None)): subject_uri = s.__str__() # get the URI of the subject as a string comment = o.__str__() # get the comment from the object as a string # update the entity's comment link_ent = False try: link_ent = LinkEntity.objects.get(uri=subject_uri) except LinkEntity.DoesNotExist: link_ent = False if link_ent is not False: lang = Languages() newr = LinkAnnotation() # make the subject a prefixed URI if common newr.subject = URImanagement.prefix_common_uri(subject_uri) newr.subject_type = 'uri' newr.project_uuid = '0' newr.source_id = self.vocabulary_uri newr.predicate_uri = 'rdfs:comment' newr.obj_extra = {} newr.obj_extra[lang.DEFAULT_LANGUAGE] = comment newr.save()
def replace_predicate_uri(self, old_pred_uri, new_pred_uri): """ replaces annotations using a given old_predicate with a new one """ lequiv = LinkEquivalence() old_pred_list = lequiv.get_identifier_list_variants(old_pred_uri) la_preds = LinkAnnotation.objects\ .filter(predicate_uri__in=old_pred_list) print('Change predicates for annotations: ' + str(len(la_preds))) for la_pred in la_preds: old_hash = la_pred.hash_id new_la = la_pred new_la.predicate_uri = new_pred_uri try: new_la.save() ok = True except Exception as error: ok = False if ok: LinkAnnotation.objects\ .filter(hash_id=old_hash).delete()
def check_pred_dc_author(self, pred_obj): """ some predicates have equivalence relations with DC-terms:contributor or DC-terms:creator properties. This method checks to see if a predicate has some sort of equivalence to a dublin core contributor or creator URI. If so """ if isinstance(pred_obj, dict): pred_obj['dc_contrib'] = False pred_obj['dc_creator'] = False if 'ent' in pred_obj: pred_ent = pred_obj['ent'] if hasattr(pred_ent, 'item_json_ld'): if isinstance(pred_ent.item_json_ld, dict): pred_json_ld = pred_ent.item_json_ld le = LinkEquivalence() equiv_keys = le.get_identifier_list_variants(LinkAnnotation.PREDS_SBJ_EQUIV_OBJ) dc_contrib_uris = le.get_identifier_list_variants(ItemKeys.PREDICATES_DCTERMS_CONTRIBUTOR) dc_creator_uris = le.get_identifier_list_variants(ItemKeys.PREDICATES_DCTERMS_CREATOR) for equiv_key in equiv_keys: if equiv_key in pred_json_ld : if isinstance(pred_json_ld[equiv_key], list): for pred_equiv in pred_json_ld[equiv_key]: uri = None if 'id' in pred_equiv: uri = pred_equiv['id'] elif '@id' in pred_equiv: uri = pred_equiv['@id'] if uri in dc_contrib_uris: pred_obj['dc_contrib'] = True break if uri in dc_creator_uris: pred_obj['dc_creator'] = True break if pred_obj['dc_contrib'] or pred_obj['dc_creator']: # no need to keep searching, we found equivalence break return pred_obj
def save_entity_comments(self): """ saves comments about an entity """ if self.graph is not False and self.vocabulary_uri is not False: lequiv = LinkEquivalence() # get all the varients of RDFS:comments comment_uris = lequiv.get_identifier_list_variants('rdfs:comment') # now get all the entities from this vocabulary (that may be the subject of a comment) raw_subject_uris = LinkEntity.objects.filter( vocab_uri=self.vocabulary_uri) lequiv = LinkEquivalence() subject_uris = lequiv.get_identifier_list_variants( raw_subject_uris) if self.replace_old: # delete the old comments LinkAnnotation.objects\ .filter(subject__in=subject_uris, predicate_uri__in=comment_uris)\ .delete() for s, p, o in self.graph.triples((None, RDFS.comment, None)): subject_uri = s.__str__( ) # get the URI of the subject as a string comment = o.__str__( ) # get the comment from the object as a string # update the entity's comment link_ent = False try: link_ent = LinkEntity.objects.get(uri=subject_uri) except LinkEntity.DoesNotExist: link_ent = False if link_ent is not False: lang = Languages() newr = LinkAnnotation() # make the subject a prefixed URI if common newr.subject = URImanagement.prefix_common_uri(subject_uri) newr.subject_type = 'uri' newr.project_uuid = '0' newr.source_id = self.vocabulary_uri newr.predicate_uri = 'rdfs:comment' newr.obj_extra = {} newr.obj_extra[lang.DEFAULT_LANGUAGE] = comment newr.save()
def add_item_annotation(self, post_data): """ Adds a linked data annotation to an item """ note = '' ok_predicates = ['dc-terms:creator', 'dc-terms:contributor', 'dc-terms:subject', 'dc-terms:coverage', 'dc-terms:temporal', 'dc-terms:references', 'dc-terms:isReferencedBy', 'dc-terms:license', 'skos:closeMatch', 'skos:exactMatch', 'owl:sameAs', 'skos:broader', 'skos:related', 'skos:example', 'rdfs:isDefinedBy', 'http://www.w3.org/2000/01/rdf-schema#range'] ok = True predicate_uri = self.request_param_val(post_data, 'predicate_uri') object_uri = self.request_param_val(post_data, 'object_uri') if predicate_uri is not False \ and object_uri is not False: p_entity = Entity() found_p = p_entity.dereference(predicate_uri) if found_p is False \ and predicate_uri in ok_predicates: found_p = True o_entity = Entity() found_o = o_entity.dereference(object_uri) if found_p and found_o: lequiv = LinkEquivalence() pred_list = lequiv.get_identifier_list_variants(predicate_uri) obj_list = lequiv.get_identifier_list_variants(object_uri) la_exist = LinkAnnotation.objects\ .filter(subject=self.uuid, predicate_uri__in=pred_list, object_uri__in=obj_list)[:1] if len(la_exist) < 1: # we don't have an annotation like this yet object_uri = o_entity.uri new_la = LinkAnnotation() new_la.subject = self.manifest.uuid new_la.subject_type = self.manifest.item_type new_la.project_uuid = self.manifest.project_uuid new_la.source_id = self.request_param_val(post_data, 'source_id', 'manual-web-form', False) new_la.sort = self.request_param_val(post_data, 'sort', 0, False) new_la.predicate_uri = predicate_uri new_la.object_uri = object_uri new_la.creator_uuid = self.creator_uuid new_la.save() # now clear the cache a change was made self.clear_caches() else: ok = False note = 'This annotation already exists.' else: ok = False note = 'Missing a predicate or object entity' else: note = self.errors['params'] ok = False self.response = {'action': 'add-item-annotation', 'ok': ok, 'change': {'note': note}} return self.response
def sort_change_annotation(self, post_data): """ Adds a linked data annotation to an item """ note = '' ok = True if 'hash_id' in post_data \ and 'sort_change' in post_data: hash_id = post_data['hash_id'] sort_change = 0 try: sort_change = int(float(post_data['sort_change'])) except: sort_change = 0 ok = False note += 'Error, sort_change needs to be an integer value. ' if sort_change != 0: try: la_act = LinkAnnotation.objects\ .get(hash_id=hash_id) except LinkAnnotation.DoesNotExist: la_act = False ok = False note += 'Cannot find annotation to re-sort. ' if la_act is not False: # now get the related annotations to re-sort le = LinkEquivalence() subject_list = le.get_identifier_list_variants(la_act.subject) if self.manifest.uuid not in subject_list: subject_list.append(self.manifest.uuid) pred_list = le.get_identifier_list_variants(la_act.predicate_uri) rel_annos = LinkAnnotation.objects\ .filter(subject__in=subject_list, predicate_uri__in=pred_list) pseudo_sort = 0 # used to make a sort value if none was given i = -1 current_hash_index = False for rel_anno in rel_annos: pseudo_sort += 1 i += 1 if rel_anno.sort is None \ or rel_anno.sort == 0: rel_anno.sort = pseudo_sort rel_anno.save() if rel_anno.hash_id == hash_id: current_hash_index = i if current_hash_index is not False: item_b_index = current_hash_index + sort_change if item_b_index >=0 and item_b_index < len(rel_annos): anno_a = rel_annos[current_hash_index] anno_b = rel_annos[item_b_index] new_sort_anno_b = anno_a.sort new_sort_anno_a = anno_b.sort if new_sort_anno_a == new_sort_anno_b: # so we don't have exactly the same values new_sort_anno_a += sort_change anno_a.sort = new_sort_anno_a anno_a.save() anno_b.sort = new_sort_anno_b anno_b.save() ok = True note += 'Annotation successfully resorted. ' # now clear the cache a change was made self.clear_caches() # now fix it so we will always have unique sorts. used_sorts = [] rel_annos = LinkAnnotation.objects\ .filter(subject__in=subject_list, predicate_uri__in=pred_list) for act_anno in rel_annos: if act_anno.sort not in used_sorts: used_sorts.append(act_anno.sort) else: act_anno.sort += 1 used_sorts.append(act_anno.sort) act_anno.save() else: ok = False note += 'Cannot change sorting, as at limit of the list of objects.' note += ' Current_hash_index: ' + str(current_hash_index) note += ' Exchange with index: ' + str(item_b_index) else: ok = False note += 'A truly bizzare something happened. ' else: ok = False note = 'Missing a annotation hash-id or sorting parameter "sort_change". ' self.errors['params'] = note self.response = {'action': 'edit-annotation', 'ok': ok, 'change': {'note': note}} return self.response
def get_date_range_via_ldata(self, object_uri): """ gets predicates linked to an object_uri """ lequiv = LinkEquivalence() predicate_uuids = lequiv.get_from_object(object_uri) return self.get_date_range(predicate_uuids)
def _get_dtypes_db(self, entity_uri): """ returns an entity data type """ # haven't found it yet, so look in database lequiv = LinkEquivalence() return lequiv.get_data_types_from_object(entity_uri)
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 get_db_project_dc_metadata(self, project_uuid): """ Gets dc-metadata information for a project from the database these metadata are inherited by all items in the project (author, and temporal) """ pr = ProjectRels() le = LinkEquivalence() dc_contrib_uris = le.get_identifier_list_variants( ItemKeys.PREDICATES_DCTERMS_CONTRIBUTOR) dc_creator_uris = le.get_identifier_list_variants( ItemKeys.PREDICATES_DCTERMS_CREATOR) dc_temporal_uris = le.get_identifier_list_variants( ItemKeys.PREDICATES_DCTERMS_TEMPORAL) dc_license_uris = le.get_identifier_list_variants( ItemKeys.PREDICATES_DCTERMS_LICENSE) dc_meta_uris = dc_contrib_uris + dc_creator_uris + dc_temporal_uris + dc_license_uris proj_dc_meta = False project_entity = self.get_entity(project_uuid) try: project = Project.objects.get(uuid=project_uuid) except Project.DoesNotExist: project = None if project_entity is not False: proj_dc_meta = { 'entity': project_entity, 'project_obj': project, 'sub_projects': pr.get_sub_projects(project_uuid), ItemKeys.PREDICATES_DCTERMS_CONTRIBUTOR: [], ItemKeys.PREDICATES_DCTERMS_CREATOR: [], ItemKeys.PREDICATES_DCTERMS_TEMPORAL: [], ItemKeys.PREDICATES_DCTERMS_LICENSE: [] } # get the project dc-metadata annotations (interitable only) proj_meta_annos = LinkAnnotation.objects\ .filter(subject=project_uuid, predicate_uri__in=dc_meta_uris)\ .order_by('predicate_uri', 'sort') for anno in proj_meta_annos: if anno.predicate_uri in dc_contrib_uris: # we've got a contributor annotation if anno.object_uri not in proj_dc_meta[ ItemKeys.PREDICATES_DCTERMS_CONTRIBUTOR]: proj_dc_meta[ItemKeys.PREDICATES_DCTERMS_CONTRIBUTOR]\ .append(anno) elif anno.predicate_uri in dc_creator_uris: # we've got creator annotation if anno.object_uri not in proj_dc_meta[ ItemKeys.PREDICATES_DCTERMS_CREATOR]: proj_dc_meta[ItemKeys.PREDICATES_DCTERMS_CREATOR]\ .append(anno) elif anno.predicate_uri in dc_temporal_uris: # we've got temporal annotation if anno.object_uri not in proj_dc_meta[ ItemKeys.PREDICATES_DCTERMS_TEMPORAL]: proj_dc_meta[ItemKeys.PREDICATES_DCTERMS_TEMPORAL]\ .append(anno) elif anno.predicate_uri in dc_license_uris: # we've got a license annotation if anno.object_uri not in proj_dc_meta[ ItemKeys.PREDICATES_DCTERMS_LICENSE]: proj_dc_meta[ItemKeys.PREDICATES_DCTERMS_LICENSE]\ .append(anno) else: # there's no project entity proj_dc_meta = False return proj_dc_meta