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_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_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.startswith('http://') or identifier.startswith('https://')): oc_uuid = URImanagement.get_uuid_from_oc_uri(identifier) if oc_uuid: output_list.append(oc_uuid) prefix_id = URImanagement.prefix_common_uri(identifier) if prefix_id: 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 m_cache = MemoryCache() ent = m_cache.get_entity(identifier) if ent: 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 add_project_predicates_and_annotations_to_graph(self, graph): """ gets the project predicates and their annotations with database calls """ pred_sql_dict_list = self.get_working_project_predicates() la_preds = self.get_link_annotations_for_preds(pred_sql_dict_list) if not isinstance(pred_sql_dict_list, list): # No predicates in the project. Weird, but possible return graph annotated_pred_uuids = {la.subject: [] for la in la_preds} for la in la_preds: annotated_pred_uuids[la.subject].append(la) for sql_dict in pred_sql_dict_list: act_pred = LastUpdatedOrderedDict() act_pred['@id'] = 'oc-pred:' + str(sql_dict['slug']) act_pred['owl:sameAs'] = URImanagement.make_oc_uri( sql_dict['predicate_uuid'], 'predicates') act_pred['label'] = sql_dict['label'] act_pred['uuid'] = sql_dict['predicate_uuid'] act_pred['slug'] = sql_dict['slug'] if isinstance(sql_dict['class_uri'], str) and len(sql_dict['class_uri']) > 0: act_pred['oc-gen:predType'] = sql_dict['class_uri'] uuid_la_preds = annotated_pred_uuids.get( sql_dict['predicate_uuid'], []) for la_pred in uuid_la_preds: la_pred_uri = URImanagement.prefix_common_uri( la_pred.predicate_uri) act_pred = self.add_unique_object_dict_to_pred( act_pred, la_pred_uri, la_pred.object_uri) graph.append(act_pred) return graph
def save_icons(self, predicate_uri='oc-gen:hasIcon'): """ Saves icons in the general Open Context namespace """ data = False if(self.graph is not False and self.vocabulary_uri is not False): data = [] if(self.replace_old): # delete old relations from this vocabulary using this predicate LinkAnnotation.objects.filter(source_id=self.vocabulary_uri, predicate_uri=predicate_uri).delete() if(predicate_uri == 'oc-gen:hasIcon'): # for subClassOf predicates full_pred_uri = URImanagement.convert_prefix_to_full_uri(predicate_uri) icon_pred = URIRef(full_pred_uri) for s, p, o in self.graph.triples((None, icon_pred, None)): subject_uri = s.__str__() # get the URI of the subject as a string object_uri = o.__str__() # get the URI of the object as a string act_t = {'s': subject_uri, 'o': object_uri} if(subject_uri != object_uri): data.append(act_t) if(len(data) > 0): for act_t in data: newr = LinkAnnotation() # make the subject a prefixed URI if common newr.subject = URImanagement.prefix_common_uri(act_t['s']) newr.subject_type = 'uri' newr.project_uuid = '0' newr.source_id = self.vocabulary_uri newr.predicate_uri = predicate_uri newr.object_uri = act_t['o'] newr.save() return data
def add_project_types_with_annotations_to_graph(self, graph): """ adds project types that have annotations """ type_sql_dict_list = self.get_working_project_types() if isinstance(type_sql_dict_list, list): # consolidate things so a given type is given once in the list # of a graph. To do so, we first put everything in a all_types # dict all_types = LastUpdatedOrderedDict() for sql_dict in type_sql_dict_list: type_uri = URImanagement.make_oc_uri(sql_dict['type_uuid'], 'types') if type_uri not in all_types: act_type = LastUpdatedOrderedDict() act_type['@id'] = type_uri act_type['label'] = sql_dict['type_label'] act_type['owl:sameAs'] = URImanagement.make_oc_uri( sql_dict['type_slug'], 'types') act_type['uuid'] = sql_dict['type_uuid'] act_type['slug'] = sql_dict['type_slug'] else: act_type = all_types[type_uri] la_pred_uri = URImanagement.prefix_common_uri( sql_dict['predicate_uri']) act_type = self.add_unique_object_dict_to_pred( act_type, la_pred_uri, sql_dict['object_uri']) all_types[type_uri] = act_type for type_uri, act_type in all_types.items(): graph.append(act_type) return graph
def add_project_predicates_and_annotations_to_graph(self, graph): """ gets the project predicates and their annotations with database calls """ pred_sql_dict_list = self.get_working_project_predicates() la_preds = self.get_link_annotations_for_preds(pred_sql_dict_list) if isinstance(pred_sql_dict_list, list): for sql_dict in pred_sql_dict_list: act_pred = LastUpdatedOrderedDict() act_pred['@id'] = 'oc-pred:' + sql_dict['slug'] act_pred['owl:sameAs'] = URImanagement.make_oc_uri(sql_dict['predicate_uuid'], 'predicates') act_pred['label'] = sql_dict['label'] act_pred['uuid'] = sql_dict['predicate_uuid'] act_pred['slug'] = sql_dict['slug'] if isinstance(sql_dict['class_uri'], str): if len(sql_dict['class_uri']) > 0: act_pred['oc-gen:predType'] = sql_dict['class_uri'] pred_found = False for la_pred in la_preds: if la_pred.subject == sql_dict['predicate_uuid']: pred_found = True # prefix common URIs for the predicate of the link annotation la_pred_uri = URImanagement.prefix_common_uri(la_pred.predicate_uri) if la_pred_uri not in act_pred: act_pred[la_pred_uri] = [] la_object_item = self.make_object_dict_item(la_pred.object_uri) act_pred[la_pred_uri].append(la_object_item) else: if pred_found: # because this list is sorted by la_pred.subject, we're done # finding any more annotations on act_pred item break graph.append(act_pred) return graph
def add_project_types_with_annotations_to_graph(self, graph): """ adds project types that have annotations """ type_sql_dict_list = self.get_working_project_types() if isinstance(type_sql_dict_list, list): # consolidate things so a given type is given once in the list # of a graph. To do so, we first put everything in a all_types # dict all_types = LastUpdatedOrderedDict() for sql_dict in type_sql_dict_list: type_uri = URImanagement.make_oc_uri(sql_dict['type_uuid'], 'types') if type_uri not in all_types: act_type = LastUpdatedOrderedDict() act_type['@id'] = type_uri act_type['label'] = sql_dict['type_label'] act_type['owl:sameAs'] = URImanagement.make_oc_uri(sql_dict['type_slug'], 'types') act_type['uuid'] = sql_dict['type_uuid'] act_type['slug'] = sql_dict['type_slug'] else: act_type = all_types[type_uri] la_pred_uri = URImanagement.prefix_common_uri(sql_dict['predicate_uri']) if la_pred_uri not in act_type: act_type[la_pred_uri] = [] la_object_item = self.make_object_dict_item(sql_dict['object_uri']) act_type[la_pred_uri].append(la_object_item) all_types[type_uri] = act_type for type_uri, act_type in all_types.items(): graph.append(act_type) return graph
def save_icons(self, predicate_uri='oc-gen:hasIcon'): """ Saves icons in the general Open Context namespace """ data = False if (self.graph is not False and self.vocabulary_uri is not False): data = [] if (self.replace_old): # delete old relations from this vocabulary using this predicate LinkAnnotation.objects.filter( source_id=self.vocabulary_uri, predicate_uri=predicate_uri).delete() if (predicate_uri == 'oc-gen:hasIcon'): # for subClassOf predicates full_pred_uri = URImanagement.convert_prefix_to_full_uri( predicate_uri) icon_pred = URIRef(full_pred_uri) for s, p, o in self.graph.triples((None, icon_pred, None)): subject_uri = s.__str__( ) # get the URI of the subject as a string object_uri = o.__str__( ) # get the URI of the object as a string act_t = {'s': subject_uri, 'o': object_uri} if (subject_uri != object_uri): data.append(act_t) if (len(data) > 0): for act_t in data: newr = LinkAnnotation() # make the subject a prefixed URI if common newr.subject = URImanagement.prefix_common_uri(act_t['s']) newr.subject_type = 'uri' newr.project_uuid = '0' newr.source_id = self.vocabulary_uri newr.predicate_uri = predicate_uri newr.object_uri = act_t['o'] newr.save() return data
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_alt_uri(self, uri): """ makes an alternative URI, changing a prefixed to a full uri or a full uri to a prefix """ output = uri if (uri[:7] == 'http://' or uri[:8] == 'https://'): output = URImanagement.prefix_common_uri(uri) else: output = URImanagement.convert_prefix_to_full_uri(uri) return output
def make_alt_uri(self, uri): """ makes an alternative URI, changing a prefixed to a full uri or a full uri to a prefix """ output = uri if(uri[:7] == 'http://' or uri[:8] == 'https://'): output = URImanagement.prefix_common_uri(uri) else: output = URImanagement.convert_prefix_to_full_uri(uri) return output
def prep_delete_uuid(self, delete_uuid): """ Prepares some information needed to delete a uuid """ ok_delete = False delete_obj = self.get_manifest(delete_uuid) if delete_obj is not False: ok_delete = True self.delete_manifest_obj = delete_obj self.delete_uri = URImanagement.make_oc_uri(delete_uuid, delete_obj.item_type) self.delete_prefix_uri = URImanagement.prefix_common_uri(self.delete_uri) return ok_delete
def prep_merge_uuid(self, merge_into_uuid): """ Prepares some information needed to delete a uuid """ ok_merge = False merge_obj = self.get_manifest(merge_into_uuid) if merge_obj is not False: ok_merge = True self.merge_manifest_obj = merge_obj self.merge_uri = URImanagement.make_oc_uri(merge_into_uuid, merge_obj.item_type) self.merge_prefix_uri = URImanagement.prefix_common_uri(self.merge_uri) return ok_merge
def dereference_linked_data(self, identifier, link_entity_slug=None): """Dereferences a linked data entity (not part of an OC project)""" uris = [] if ((len(identifier) > 8) and (identifier[:7] == 'http://' or identifier[:8] == 'https://')): ent_equivs = EntityEquivalents() uris = ent_equivs.make_uri_variants(identifier) if not uris and not link_entity_slug: return None ld_entity = LinkEntity.objects.filter( Q(uri__in=uris) | Q(slug=identifier)).first() if not ld_entity: return None self.uri = ld_entity.uri self.slug = ld_entity.slug self.label = ld_entity.label self.item_type = 'uri' self.alt_label = ld_entity.alt_label self.entity_type = ld_entity.ent_type self.vocab_uri = ld_entity.vocab_uri self.ld_object_ok = True if isinstance(ld_entity.localized_json, dict): self.data_type = ld_entity.localized_json.get('data_type', False) # Now get the vocabulary item. vocab_uris = [ld_entity.vocab_uri] vocab_uris.append(ld_entity.vocab_uri.replace('https://', 'http://')) vocab_uris.append(ld_entity.vocab_uri.replace('http://', 'https://')) vocab_entity = LinkEntity.objects.filter(uri__in=vocab_uris).first() if vocab_entity: self.vocabulary = vocab_entity.label if (not self.data_type and self.get_ld_data_type and ld_entity.ent_type == 'property'): # Get the data_type for the link entity via looking at the # most common data_type for associated predicate items. self.data_type = self.get_linked_entity_data_type(ld_entity) if not self.get_icon: # Do not bother adding an icon. return True # Add an icon link if true prefix_uri = URImanagement.prefix_common_uri(ld_entity.uri) icon_ids = uris + [ld_entity.uri, identifier, prefix_uri] icon_anno = LinkAnnotation.objects.filter( subject__in=icon_ids, predicate_uri='oc-gen:hasIcon').first() if icon_anno: self.icon = icon_anno.object_uri return True
def add_json_ld_link_annotations(self, json_ld): """ adds linked data annotations (typically referencing URIs from outside Open Context) """ if not self.link_annotations: return json_ld if not len(self.link_annotations): return json_ld parts_json_ld = PartsJsonLD() parts_json_ld.proj_context_json_ld = self.proj_context_json_ld parts_json_ld.manifest_obj_dict = self.manifest_obj_dict for la in self.link_annotations: tcheck = URImanagement.get_uuid_from_oc_uri(la.object_uri, True) if not tcheck: # this item is NOT from open context item_type = False else: # an Open Context item item_type = tcheck['item_type'] if item_type == 'persons': # add a stable ID to person items, but only if they are ORCID IDs parts_json_ld.stable_id_predicate = ItemKeys.PREDICATES_FOAF_PRIMARYTOPICOF parts_json_ld.stable_id_prefix_limit = StableIdentifer.ID_TYPE_PREFIXES[ 'orcid'] # this shortens URIs in item-context declared namespaces # to make a compact URI (prefixed), as the act_pred act_pred = URImanagement.prefix_common_uri(la.predicate_uri) if act_pred not in self.dc_author_preds \ and act_pred not in self.dc_inherit_preds \ and act_pred not in self.dc_metadata_preds: # the act_pred is not a dublin core predicate, so we're OK to add it # now, not later. if not biological_taxonomy_validation(act_pred, la.object_uri): # We have a act_pred and object_uri combination # that is not valid. So skip. continue json_ld = parts_json_ld.addto_predicate_list( json_ld, act_pred, la.object_uri, item_type) else: # we've got dublin core assertions, cache these in the dict_object # dc_assertions so they get added LAST, after other asserttions self.dc_assertions = parts_json_ld.addto_predicate_list( self.dc_assertions, act_pred, la.object_uri, item_type) return json_ld
def add_json_ld_link_annotations(self, json_ld): """ adds linked data annotations (typically referencing URIs from outside Open Context) """ if not self.link_annotations or not len(self.link_annotations): # No link annotations, so skip out. return json_ld # We have link annotations. parts_json_ld = PartsJsonLD() parts_json_ld.proj_context_json_ld = self.proj_context_json_ld parts_json_ld.manifest_obj_dict = self.manifest_obj_dict for la in self.link_annotations: tcheck = URImanagement.get_uuid_from_oc_uri(la.object_uri, True) if not tcheck: # this item is NOT from open context item_type = False else: # an Open Context item item_type = tcheck['item_type'] if item_type == 'persons': # add a stable ID to person items, but only if they are ORCID IDs parts_json_ld.stable_id_predicate = ItemKeys.PREDICATES_FOAF_PRIMARYTOPICOF parts_json_ld.stable_id_prefix_limit = StableIdentifer.ID_TYPE_PREFIXES['orcid'] # this shortens URIs in item-context declared namespaces # to make a compact URI (prefixed), as the act_pred act_pred = URImanagement.prefix_common_uri(la.predicate_uri) if act_pred not in self.dc_author_preds \ and act_pred not in self.dc_inherit_preds \ and act_pred not in self.dc_metadata_preds: # the act_prec is not a dublin core predicate, so we're OK to add it # now, not later. json_ld = parts_json_ld.addto_predicate_list( json_ld, act_pred, la.object_uri, item_type ) else: # we've got dublin core assertions, cache these in the dict_object # dc_assertions so they get added LAST, after other asserttions self.dc_assertions = parts_json_ld.addto_predicate_list( self.dc_assertions, act_pred, la.object_uri, item_type ) return json_ld
def save_hierarchy(self, predicate_uri='rdfs:subClassOf'): """ Saves hierarchic relations from a vocabulary, defaulting to subClassOf predicates """ data = False if (self.graph is not False and self.vocabulary_uri is not False): data = [] if (self.replace_old): # delete old relations from this vocabulary using this predicate LinkAnnotation.objects.filter( source_id=self.vocabulary_uri, predicate_uri=predicate_uri).delete() if (predicate_uri == 'rdfs:subClassOf'): # for subClassOf predicates for s, p, o in self.graph.triples( (None, RDFS.subClassOf, None)): subject_uri = s.__str__( ) # get the URI of the subject as a string object_uri = o.__str__( ) # get the URI of the object as a string act_t = {'s': subject_uri, 'o': object_uri} if (subject_uri != object_uri): data.append(act_t) elif (predicate_uri == 'rdfs:subPropertyOf'): # for subPropertyOf predicates for s, p, o in self.graph.triples( (None, RDFS.subPropertyOf, None)): subject_uri = s.__str__( ) # get the URI of the subject as a string object_uri = o.__str__( ) # get the URI of the object as a string act_t = {'s': subject_uri, 'o': object_uri} if (subject_uri != object_uri): data.append(act_t) if (len(data) > 0): for act_t in data: newr = LinkAnnotation() # make the subject a prefixed URI if common newr.subject = URImanagement.prefix_common_uri(act_t['s']) newr.subject_type = 'uri' newr.project_uuid = '0' newr.source_id = self.vocabulary_uri newr.predicate_uri = predicate_uri newr.object_uri = act_t['o'] newr.save() return data
def make_uri_equivalence_list(raw_term, alt_suffix="/"): """ Makes Prefixed, HTTP, HTTPS and '/' ending options list for URLs """ # NOTE: Open Context often references Web URL/URIs to "linked data" # entities. Open Context considers http:// and https:// URLs to be # equivalent. This function takes a raw term and makes http:// # https:// variants. It also makes a prefixed URL if a namespace # is recognized in URImanagement. Finally, it will by default, # make variants that have and do not have a trailing "/". output_list = [] if not isinstance(raw_term, str): return None output_list.append(raw_term) url_terms = [] if raw_term.startswith('http://') or raw_term.startswith('https://'): # NOTE: The raw_term looks like a Web URL. We need to make # variants that start with http, https, and end in a slash, and # do not end in a slash. url_terms = make_suffix_no_suffix_list(raw_term, suffix=alt_suffix) elif raw_term.count(':') == 1: full_uri = URImanagement.convert_prefix_to_full_uri(raw_term) if full_uri: url_terms = make_suffix_no_suffix_list(full_uri, suffix=alt_suffix) url_terms.append(raw_term) for term in url_terms: http_alts = make_alternative_prefix_list(term, alt_prefixes=( 'http://', 'https://', )) if not http_alts: continue for http_alt in http_alts: if http_alt not in output_list: output_list.append(http_alt) prefix_id = URImanagement.prefix_common_uri(http_alt) if alt_suffix and prefix_id.endswith(alt_suffix): # Remove any trailing slash with prefixed IDs. prefix_id = prefix_id[:-len(alt_suffix)] if prefix_id and prefix_id not in output_list: output_list.append(prefix_id) return output_list
def save_hierarchy(self, predicate_uri='rdfs:subClassOf'): """ Saves hierarchic relations from a vocabulary, defaulting to subClassOf predicates """ data = False if(self.graph is not False and self.vocabulary_uri is not False): data = [] if(self.replace_old): # delete old relations from this vocabulary using this predicate LinkAnnotation.objects.filter(source_id=self.vocabulary_uri, predicate_uri=predicate_uri).delete() if(predicate_uri == 'rdfs:subClassOf'): # for subClassOf predicates for s, p, o in self.graph.triples((None, RDFS.subClassOf, None)): subject_uri = s.__str__() # get the URI of the subject as a string object_uri = o.__str__() # get the URI of the object as a string act_t = {'s': subject_uri, 'o': object_uri} if(subject_uri != object_uri): data.append(act_t) elif(predicate_uri == 'rdfs:subPropertyOf'): # for subPropertyOf predicates for s, p, o in self.graph.triples((None, RDFS.subPropertyOf, None)): subject_uri = s.__str__() # get the URI of the subject as a string object_uri = o.__str__() # get the URI of the object as a string act_t = {'s': subject_uri, 'o': object_uri} if(subject_uri != object_uri): data.append(act_t) if(len(data) > 0): for act_t in data: newr = LinkAnnotation() # make the subject a prefixed URI if common newr.subject = URImanagement.prefix_common_uri(act_t['s']) newr.subject_type = 'uri' newr.project_uuid = '0' newr.source_id = self.vocabulary_uri newr.predicate_uri = predicate_uri newr.object_uri = act_t['o'] newr.save() return data
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 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 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 dereference(self, identifier, link_entity_slug=False): """ Dereferences an entity identified by an identifier, checks if a URI, if, not a URI, then looks in the OC manifest for the item """ output = False try_manifest = True identifier = URImanagement.convert_prefix_to_full_uri(identifier) if (link_entity_slug or (len(identifier) > 8)): if (link_entity_slug or (identifier[:7] == 'http://' or identifier[:8] == 'https://')): try: try_manifest = False ld_entity = LinkEntity.objects.get( Q(uri=identifier) | Q(slug=identifier)) except LinkEntity.DoesNotExist: ld_entity = False if (ld_entity is not False): output = True self.uri = ld_entity.uri self.slug = ld_entity.slug self.label = ld_entity.label self.item_type = 'uri' self.alt_label = ld_entity.alt_label self.entity_type = ld_entity.ent_type self.vocab_uri = ld_entity.vocab_uri self.ld_object_ok = True try: vocab_entity = LinkEntity.objects.get( uri=self.vocab_uri) except LinkEntity.DoesNotExist: vocab_entity = False if (vocab_entity is not False): self.vocabulary = vocab_entity.label if self.get_icon: prefix_uri = URImanagement.prefix_common_uri( ld_entity.uri) icon_anno = LinkAnnotation.objects\ .filter(Q(subject=ld_entity.uri) | Q(subject=identifier) | Q(subject=prefix_uri), predicate_uri='oc-gen:hasIcon')[:1] if len(icon_anno) > 0: self.icon = icon_anno[0].object_uri else: try_manifest = True # couldn't find the item in the linked entities table identifier = URImanagement.get_uuid_from_oc_uri(identifier) if (try_manifest): try: manifest_item = Manifest.objects.get( Q(uuid=identifier) | Q(slug=identifier)) except Manifest.DoesNotExist: manifest_item = False if (manifest_item is not False): output = True self.uri = URImanagement.make_oc_uri(manifest_item.uuid, manifest_item.item_type) self.uuid = manifest_item.uuid self.slug = manifest_item.slug self.label = manifest_item.label self.item_type = manifest_item.item_type self.class_uri = manifest_item.class_uri self.project_uuid = manifest_item.project_uuid if (manifest_item.item_type == 'media' and self.get_thumbnail): # a media item. get information about its thumbnail. try: thumb_obj = Mediafile.objects.get( uuid=manifest_item.uuid, file_type='oc-gen:thumbnail') except Mediafile.DoesNotExist: thumb_obj = False if thumb_obj is not False: self.thumbnail_media = thumb_obj self.thumbnail_uri = thumb_obj.file_uri elif (manifest_item.item_type == 'types'): tl = TypeLookup() tl.get_octype_without_manifest(identifier) self.content = tl.content elif (manifest_item.item_type == 'predicates'): try: oc_pred = Predicate.objects.get( uuid=manifest_item.uuid) except Predicate.DoesNotExist: oc_pred = False if (oc_pred is not False): self.data_type = oc_pred.data_type elif (manifest_item.item_type == 'subjects' and self.get_context): try: subj = Subject.objects.get(uuid=manifest_item.uuid) except Subject.DoesNotExist: subj = False if subj is not False: self.context = subj.context return output
def dereference(self, identifier, link_entity_slug=False): """ Dereferences an entity identified by an identifier, checks if a URI, if, not a URI, then looks in the OC manifest for the item """ output = False if isinstance(identifier, str): # only try to dereference if the identifier is a string. try_manifest = True identifier = URImanagement.convert_prefix_to_full_uri(identifier) if (settings.CANONICAL_HOST + '/tables/') in identifier: identifier = identifier.replace((settings.CANONICAL_HOST + '/tables/'), '') if link_entity_slug or (len(identifier) > 8): if link_entity_slug or (identifier[:7] == 'http://' or identifier[:8] == 'https://'): ent_equivs = EntityEquivalents() uris = ent_equivs.make_uri_variants(identifier) ld_entities = LinkEntity.objects.filter(Q(uri__in=uris) | Q(slug=identifier))[:1] if len(ld_entities) > 0: ld_entity = ld_entities[0] else: ld_entity = False if ld_entity is not False: output = True self.uri = ld_entity.uri self.slug = ld_entity.slug self.label = ld_entity.label self.item_type = 'uri' self.alt_label = ld_entity.alt_label self.entity_type = ld_entity.ent_type self.vocab_uri = ld_entity.vocab_uri self.ld_object_ok = True try: if 'https://' in self.vocab_uri: alt_vocab_uri = self.vocab_uri.replace('https://', 'http://') else: alt_vocab_uri = self.vocab_uri.replace('http://', 'https://') vocab_entity = LinkEntity.objects.get(Q(uri=self.vocab_uri) | Q(uri=alt_vocab_uri)) except LinkEntity.DoesNotExist: vocab_entity = False if vocab_entity is not False: self.vocabulary = vocab_entity.label if self.get_icon: prefix_uri = URImanagement.prefix_common_uri(ld_entity.uri) icon_anno = LinkAnnotation.objects\ .filter(Q(subject=ld_entity.uri) | Q(subject=identifier) | Q(subject=prefix_uri), predicate_uri='oc-gen:hasIcon')[:1] if len(icon_anno) > 0: self.icon = icon_anno[0].object_uri else: try_manifest = True # couldn't find the item in the linked entities table identifier = URImanagement.get_uuid_from_oc_uri(identifier) if try_manifest: try: manifest_item = Manifest.objects.get(Q(uuid=identifier) | Q(slug=identifier)) except Manifest.DoesNotExist: manifest_item = False if manifest_item is not False: output = True self.uri = URImanagement.make_oc_uri(manifest_item.uuid, manifest_item.item_type) self.uuid = manifest_item.uuid self.slug = manifest_item.slug self.label = manifest_item.label self.item_type = manifest_item.item_type self.class_uri = manifest_item.class_uri self.project_uuid = manifest_item.project_uuid if manifest_item.item_type == 'media' and self.get_thumbnail: # a media item. get information about its thumbnail. try: thumb_obj = Mediafile.objects.get(uuid=manifest_item.uuid, file_type='oc-gen:thumbnail') except Mediafile.DoesNotExist: thumb_obj = False if thumb_obj is not False: self.thumbnail_media = thumb_obj self.thumbnail_uri = thumb_obj.file_uri elif manifest_item.item_type in ['persons', 'projects', 'tables'] \ or self.get_stable_ids: # get stable identifiers for persons or projects by default stable_ids = StableIdentifer.objects.filter(uuid=manifest_item.uuid) if len(stable_ids) > 0: self.stable_id_uris = [] doi_uris = [] orcid_uris = [] other_uris = [] for stable_id in stable_ids: if stable_id.stable_type in StableIdentifer.ID_TYPE_PREFIXES: prefix = StableIdentifer.ID_TYPE_PREFIXES[stable_id.stable_type] else: prefix = '' stable_uri = prefix + stable_id.stable_id if stable_id.stable_type == 'orcid': orcid_uris.append(stable_uri) elif stable_id.stable_type == 'doi': doi_uris.append(stable_uri) else: other_uris.append(stable_uri) # now list URIs in order of importance, with ORCIDs and DOIs # first, followed by other stable URI types (Arks or something else) self.stable_id_uris = orcid_uris + doi_uris + other_uris elif manifest_item.item_type == 'types': tl = TypeLookup() tl.get_octype_without_manifest(identifier) self.content = tl.content elif manifest_item.item_type == 'predicates': try: oc_pred = Predicate.objects.get(uuid=manifest_item.uuid) except Predicate.DoesNotExist: oc_pred = False if oc_pred is not False: self.data_type = oc_pred.data_type self.sort = oc_pred.sort self.slug_uri = 'oc-pred:' + str(self.slug) elif manifest_item.item_type == 'projects': # get a manifest object for the parent of a project, if it exists ch_tab = '"oc_projects" AS "child"' filters = 'child.project_uuid=oc_manifest.uuid '\ ' AND child.uuid=\'' + self.uuid + '\' ' \ ' AND child.project_uuid != \'' + self.uuid + '\' ' par_rows = Manifest.objects\ .filter(item_type='projects')\ .exclude(uuid=self.uuid)\ .extra(tables=[ch_tab], where=[filters])[:1] if len(par_rows) > 0: self.par_proj_man_obj = par_rows[0] elif manifest_item.item_type == 'subjects' and self.get_context: try: subj = Subject.objects.get(uuid=manifest_item.uuid) except Subject.DoesNotExist: subj = False if subj is not False: self.context = subj.context 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 dereference(self, identifier, link_entity_slug=False): """ Dereferences an entity identified by an identifier, checks if a URI, if, not a URI, then looks in the OC manifest for the item """ output = False try_manifest = True identifier = URImanagement.convert_prefix_to_full_uri(identifier) if(link_entity_slug or (len(identifier) > 8)): if(link_entity_slug or (identifier[:7] == 'http://' or identifier[:8] == 'https://')): try: try_manifest = False ld_entity = LinkEntity.objects.get(Q(uri=identifier) | Q(slug=identifier)) except LinkEntity.DoesNotExist: ld_entity = False if(ld_entity is not False): output = True self.uri = ld_entity.uri self.slug = ld_entity.slug self.label = ld_entity.label self.item_type = 'uri' self.alt_label = ld_entity.alt_label self.entity_type = ld_entity.ent_type self.vocab_uri = ld_entity.vocab_uri self.ld_object_ok = True try: vocab_entity = LinkEntity.objects.get(uri=self.vocab_uri) except LinkEntity.DoesNotExist: vocab_entity = False if(vocab_entity is not False): self.vocabulary = vocab_entity.label if self.get_icon: prefix_uri = URImanagement.prefix_common_uri(ld_entity.uri) icon_anno = LinkAnnotation.objects\ .filter(Q(subject=ld_entity.uri) | Q(subject=identifier) | Q(subject=prefix_uri), predicate_uri='oc-gen:hasIcon')[:1] if len(icon_anno) > 0: self.icon = icon_anno[0].object_uri else: try_manifest = True # couldn't find the item in the linked entities table identifier = URImanagement.get_uuid_from_oc_uri(identifier) if(try_manifest): try: manifest_item = Manifest.objects.get(Q(uuid=identifier) | Q(slug=identifier)) except Manifest.DoesNotExist: manifest_item = False if(manifest_item is not False): output = True self.uri = URImanagement.make_oc_uri(manifest_item.uuid, manifest_item.item_type) self.uuid = manifest_item.uuid self.slug = manifest_item.slug self.label = manifest_item.label self.item_type = manifest_item.item_type self.class_uri = manifest_item.class_uri self.project_uuid = manifest_item.project_uuid if(manifest_item.item_type == 'media' and self.get_thumbnail): # a media item. get information about its thumbnail. try: thumb_obj = Mediafile.objects.get(uuid=manifest_item.uuid, file_type='oc-gen:thumbnail') except Mediafile.DoesNotExist: thumb_obj = False if thumb_obj is not False: self.thumbnail_media = thumb_obj self.thumbnail_uri = thumb_obj.file_uri elif(manifest_item.item_type == 'types'): tl = TypeLookup() tl.get_octype_without_manifest(identifier) self.content = tl.content elif(manifest_item.item_type == 'predicates'): try: oc_pred = Predicate.objects.get(uuid=manifest_item.uuid) except Predicate.DoesNotExist: oc_pred = False if(oc_pred is not False): self.data_type = oc_pred.data_type elif(manifest_item.item_type == 'subjects' and self.get_context): try: subj = Subject.objects.get(uuid=manifest_item.uuid) except Subject.DoesNotExist: subj = False if subj is not False: self.context = subj.context return output