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 not found: return output # Get the parents of the child by looking up the child # uuid. self.get_parents_by_child_uuid(ent.uuid, False) if not self.contexts_list: print('Find a parent for: ' + ent.uuid) return output parent_uuid = self.contexts_list[0] # Clear class so we can use this again. # NOTE: I haven't looked at this in years. Does this have # list and dict mutation issues that lead to weird behaviors? self.contexts_list = [] self.contexts = {} self.recurse_count = 0 ent_p = Entity() found_p = ent_p.dereference(parent_uuid) if not found_p: print('Cannot dereference parent_uuid: ' + parent_uuid) return output output = ent_p.slug 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 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' 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_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 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 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 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 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_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 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 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 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 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_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 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 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_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_vocabulary_entity_from_proj_context(self, object_id, item_type): """ get a predicate or type from the project_context """ # id_keys are used to look for identifiers in json_ld items that can be # used to reference object_ids ent = False id_keys = [ '@id', 'id', 'uuid', 'slug', 'owl:sameAs' ] # print('try to look for ' + object_id + ' ' + item_type) # print('proj_context: ' + str(self.proj_context_json_ld)) if isinstance(self.proj_context_json_ld, dict) \ and item_type in self.ITEM_TYPE_PROJ_VOCAB_LIST: if '@graph' in self.proj_context_json_ld: vocab = self.proj_context_json_ld['@graph'] else: vocab = [] for key, item in self.proj_context_json_ld: if key != '@context': vocab.append(item) for item in vocab: id_match = False for id_key in id_keys: id_match = False if id_key in item: if object_id == item[id_key]: # the object id matches an identifer id_match = True break if id_match: # print('found this: ' + str(item)) ent = Entity() ent.item_json_ld = item ent.slug_uri = None if 'id' in item: ent.uri = item['id'] elif '@id' in item: ent.uri = item['@id'] elif 'owl:sameAs' in item: ent.uri = item['owl:sameAs'] if isinstance(ent.uri, str): # identify the item_type by looking at the URI for act_type in self.ITEM_TYPE_PROJ_VOCAB_LIST: if ('/' + act_type + '/') in ent.uri: ent.item_type = act_type if 'uuid' in item: ent.uuid = item['uuid'] if 'slug' in item: ent.slug = item['slug'] if ent.item_type == 'predicates': # make a predicate slug URI ent.slug_uri = 'oc-pred:' + ent.slug if 'label' in item: ent.label = item['label'] break return ent
def prepare(self): """ checks to make sure the referrer actually exists in the database """ if self.uri is not False: ent = Entity() found = ent.dereference(self.uri) if found: self.label = ent.label self.alt_label = ent.alt_label self.vocab_label = ent.vocabulary self.vocab_uri = ent.vocab_uri self.valid = True else: # the referring source is not known in the database if self.vocab_uri is not False\ and self.vocab_label is not False\ and self.label is not False: # we have enough data to save a referrer in the database referrer_ent_type = 'vocabulary' if self.vocab_uri != self.uri: referrer_ent_type = 'class' ent_v = Entity() found_v = ent_v.dereference(self.vocab_uri) if found_v is False: # the referring vocabulary is not known in the database # so we need to create it lev = LinkEntity() lev.uri = self.vocab_uri lev.label = self.vocab_label lev.alt_label = self.vocab_alt_label lev.vocab_uri = self.vocab_uri lev.ent_type = 'vocabulary' lev.save() # now are ready to make a linked entity for the referrer le = LinkEntity() le.uri = self.uri le.label = self.label le.alt_label = self.alt_label le.vocab_uri = self.vocab_uri le.ent_type = referrer_ent_type le.save() self.valid = True
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 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 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 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 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 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