def get_parents_context_metadata(self, uuid): """ get all parents from memory or by DB lookups """ if len(self.parents) >= 5000: self.parents = {} par_res = Assertion.objects\ .filter(object_uuid=uuid, predicate_uuid=Assertion.PREDICATES_CONTAINS)[:1] if len(par_res) > 0: # item has a parent parent_uuid = par_res[0].uuid if parent_uuid not in self.parents: # we don't have a context path parent list for this parent in memory yet # so let's go and make it p_list = [] act_contain = Containment() raw_parents = act_contain.get_parents_by_child_uuid(parent_uuid) if raw_parents is not False: if len(raw_parents) > 0: for tree_node, r_parents in raw_parents.items(): p_list = r_parents break p_list.insert(0, parent_uuid) # add the 1st parent to the start of the list context_metadata = {'p_list': p_list} self.parents[parent_uuid] = context_metadata else: context_metadata = self.parents[parent_uuid] else: parent_uuid = False # now get geo and chrono metadata context_metadata = self.get_geo_chrono_metadata(uuid, parent_uuid, context_metadata) return context_metadata
def generate_save_context_path_from_uuid(self, uuid, do_children=True): """ Generates and saves a context path for a subject item by uuid """ cache = caches['redis'] cache.clear() output = False try: man_obj = Manifest.objects.get(uuid=uuid, item_type='subjects') except Manifest.DoesNotExist: man_obj = False if man_obj is not False: if man_obj.item_type == 'subjects': output = self.generate_save_context_path_from_manifest_obj(man_obj) if do_children: act_contain = Containment() act_contain.redis_ok = False # get the contents recusivelhy contents = act_contain.get_children_by_parent_uuid(uuid, True) if isinstance(contents, dict): for tree_node, children in contents.items(): for child_uuid in children: # do the children, but not recursively since we # already have a resurive look up of contents output = self.generate_save_context_path_from_uuid(child_uuid, False) return output
def merge_by_uuid(self, delete_uuid, merge_into_uuid): """ Merges an item. The delete_uuid will be destroyed, but all of it's associated data will be merged into the 'merge_into_uuid' which will be retained. Returns a dictionary about what happened """ self.delete_uuid = delete_uuid self.merge_into_uuid = merge_into_uuid ok_delete = self.prep_delete_uuid(delete_uuid) ok_merge = self.prep_merge_uuid(merge_into_uuid) output = {} output['done'] = False if ok_delete and ok_merge and delete_uuid != merge_into_uuid: output['assertions'] = self.alter_assertions(delete_uuid, merge_into_uuid) output['annotations'] = self.alter_annotations(delete_uuid, merge_into_uuid) self.delete_self_containment(merge_into_uuid) cont = Containment() self.merge_children = cont.get_children_by_parent_uuid(merge_into_uuid, True) output['altered_children'] = self.update_children_subjects(self.merge_children) output['message'] = 'Merged item. Deleted - ' output['message'] += self.delete_manifest_obj.label + '(' + delete_uuid + ')' output['message'] = ', merged into - ' output['message'] += self.merge_manifest_obj.label + '(' + merge_into_uuid + ')' self.delete_manifest_obj.delete() # deletes object from the manifest self.delete_type_records(delete_uuid, self.delete_manifest_obj.item_type) output['done'] = True return output
def generate_context_path(self, uuid, include_self=True, delim='/'): """ generates a context path for a subject with a given uuid """ path = False act_contain = Containment() act_contain.redis_ok = False contexts = [] r_contexts = act_contain.get_parents_by_child_uuid(uuid) for tree_node, r_parents in r_contexts.items(): # now reverse the list of parent contexts, so top most parent context is first, # followed by children contexts contexts = r_parents[::-1] if(include_self): contexts.append(uuid) if(len(contexts) > 0): path_items = [] for p_uuid in contexts: try: act_p = Manifest.objects.get(uuid=p_uuid) path_items.append(act_p.label) except Manifest.DoesNotExist: return False path = delim.join(path_items) return path
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 get_related_uuids(self, uuid, inclusive=True): """ gets a list of uuids related to a given uuid if inclusive, include the UUID passed in the output list """ link_item_types = ['subjects', 'media', 'persons', 'documents', 'projects'] uuids = [] if isinstance(uuid, list): start_uuids = uuid else: start_uuids = [uuid] for uuid in start_uuids: try: m_obj = Manifest.objects.get(uuid=uuid) except Manifest.DoesNotExist: m_obj = False if m_obj is not False: if inclusive: uuids.append(m_obj.uuid) if m_obj.item_type == 'subjects': act_contain = Containment() # get the contents recusivelhy contents = act_contain.get_children_by_parent_uuid(m_obj.uuid, True) if isinstance(contents, dict): for tree_node, children in contents.items(): for child_uuid in children: if child_uuid not in uuids: uuids.append(child_uuid) elif m_obj.item_type == 'predicates': # reindex all of the items described by a given predicate # this can take a while! rel_objs = Assertion.objects\ .filter(predicate_uuid=m_obj.uuid) for rel_item in rel_objs: if rel_item.uuid not in uuids: uuids.append(rel_item.uuid) rel_objs = Assertion.objects\ .filter(uuid=m_obj.uuid, object_type__in=link_item_types) for rel_item in rel_objs: if rel_item.object_uuid not in uuids: uuids.append(rel_item.object_uuid) rel_subs = Assertion.objects\ .filter(subject_type__in=link_item_types, object_uuid=m_obj.uuid) for rel_item in rel_subs: if rel_item.object_uuid not in uuids: uuids.append(rel_item.object_uuid) return uuids
def check_label_exists_in_scope(self, label): """ checks to see if a label already exists within the scope of a project or related context """ if self.uuid is False: uuid_exclude = 'skip' else: uuid_exclude = self.uuid man_list = [] if self.context_uuid is not False: uuids = [] if self.item_type == 'subjects': # get items with the same parent context # that will be the scope for a unique label cont = Containment() child_trees = cont.get_children_by_parent_uuid(self.context_uuid) for tree_key, children in child_trees.items(): for child_uuid in children: if child_uuid not in uuids: uuids.append(child_uuid) else: # get items (of the same item_type) related to a context # that will be the scope for a unique label rel_list = Assertion.objects\ .filter(uuid=self.context_uuid, object_type=self.item_type) for rel in rel_list: if rel.uuid not in uuids: uuids.append(rel.uuid) if len(uuids) > 0: man_list = Manifest.objects\ .filter(project_uuid=self.project_uuid, label=label, uuid__in=uuids)\ .exclude(uuid=uuid_exclude) else: if self.item_type is not False: man_list = Manifest.objects\ .filter(project_uuid=self.project_uuid, label=label, item_type=self.item_type)\ .exclude(uuid=uuid_exclude) else: man_list = Manifest.objects\ .filter(project_uuid=self.project_uuid, label=label)\ .exclude(uuid=uuid_exclude) if len(man_list) > 0: label_exists = man_list[0].uuid else: label_exists = False return label_exists
def check_ok_cluster_for_lone_point(self, uuids, lon, lat): """ Checks to see if a lone point has enough items items in it to be considered a good cluster """ cluster_ok = False uuids = self.make_uuids_list(uuids) p_geo = Geospace.objects.filter(project_uuid__in=uuids, latitude=lat, longitude=lon)[:1] if len(p_geo) == 1: subj_uuid = p_geo[0].uuid cont = Containment() children = cont.get_children_by_parent_uuid(subj_uuid, True) if len(children) > 2: cluster_ok = True return cluster_ok
def _get_parent_slug(self, slug): ''' Takes a slug and returns the slug of its parent. Returns 'root' if a slug has no parent. ''' cache_key = self.m_cache.make_cache_key('par-slug', slug) parent_slug = self.m_cache.get_cache_object(cache_key) if parent_slug is None: contain_obj = Containment() contain_obj.use_cache = False # because it seems to introduce memory errors parent_slug = contain_obj.get_parent_slug_by_slug(slug) self.m_cache.save_cache_object(cache_key, parent_slug) if parent_slug: return parent_slug else: return 'root'
def _get_parent_slug(self, slug): ''' Takes a slug and returns the slug of its parent. Returns 'root' if a slug has no parent. ''' cache_key = self.mem_cache_obj.make_memory_cache_key('par-slug', slug) parent_slug = self.mem_cache_obj.get_cache_object(cache_key) if parent_slug is None: contain_obj = Containment() contain_obj.use_cache = False # because it seems to introduce memory errors parent_slug = contain_obj.get_parent_slug_by_slug(slug) self.mem_cache_obj.save_cache_object(cache_key, parent_slug) if parent_slug: return parent_slug else: return 'root'
def check_label_exists_in_scope(self, label): """ checks to see if a label already exists within the scope of a project or related context """ if self.uuid is not False: uuid_exclude = 'skip' else: uuid_exclude = self.uuid man_list = [] if self.context_uuid is not False: uuids = [] if self.item_type == 'subjects': # get items with the same parent context # that will be the scope for a unique label cont = Containment() child_trees = cont.get_children_by_parent_uuid( self.context_uuid) for tree_key, children in child_trees.items(): for child_uuid in children: if child_uuid not in uuids: uuids.append(child_uuid) else: # get items (of the same item_type) related to a context # that will be the scope for a unique label rel_list = Assertion.objects\ .filter(uuid=self.context_uuid, object_type=self.item_type) for rel in rel_list: if rel.uuid not in uuids: uuids.append(rel.uuid) if len(uuids) > 0: man_list = Manifest.objects\ .filter(project_uuid=self.project_uuid, label=label, uuid__in=uuids)\ .exclude(uuid=uuid_exclude) else: man_list = Manifest.objects\ .filter(project_uuid=self.project_uuid, label=label)\ .exclude(uuid=uuid_exclude) if len(man_list) > 0: label_exists = man_list[0].uuid else: label_exists = False return label_exists
def get_geo_chrono_metadata(self, uuid, parent_uuid, context_metadata): """ gets and saves geo and chrono metadata """ act_contain = Containment() geo_meta = False event_meta = False uuid_geo = Geospace.objects.filter(uuid=uuid)[:1] if len(uuid_geo) > 0: geo_meta = uuid_geo[0] else: # geo information for this item not found, look to parents if parent_uuid is not False \ and 'p_list' in context_metadata: # we have at least 1 parent if 'p_geo' not in context_metadata: # no saved geo information in this context path, so look it up p_list = context_metadata['p_list'] geo_meta = act_contain.get_geochron_from_subject_list( p_list, 'geo') context_metadata['p_geo'] = geo_meta self.parents[parent_uuid] = context_metadata else: # we have saved geo information for this context path so use it geo_meta = context_metadata['p_geo'] uuid_event = Event.objects.filter(uuid=uuid)[:1] if len(uuid_event) > 0: event_meta = uuid_event else: # chrono information for this item not found, look to parents if parent_uuid is not False \ and 'p_list' in context_metadata: # we have at least 1 parent if 'p_event' not in context_metadata: # no saved chrono information in this context path, so look it up p_list = context_metadata['p_list'] event_meta = act_contain.get_geochron_from_subject_list( p_list, 'event') context_metadata['p_event'] = event_meta self.parents[parent_uuid] = context_metadata else: # we have saved chrono information for this context path so use it event_meta = context_metadata['p_event'] context_metadata['geo'] = geo_meta context_metadata['event'] = event_meta return context_metadata
def get_containment_parent_slug(slug): '''Takes a slug and returns the slug of its parent. Returns 'root' if a slug has no parent. :param str slug: Slug identifying a subjects item. ''' m_cache = MemoryCache() cache_key = m_cache.make_cache_key('contain-par-slug', slug) parent_slug = m_cache.get_cache_object(cache_key) if parent_slug is None: contain_obj = Containment() # Because it seems to introduce memory errors, turn off # caching for this class instance. contain_obj.use_cache = False parent_slug = contain_obj.get_parent_slug_by_slug(slug) m_cache.save_cache_object(cache_key, parent_slug) if parent_slug: return parent_slug return 'root'
def get_geo_chrono_metadata(self, uuid, parent_uuid, context_metadata): """ gets and saves geo and chrono metadata """ act_contain = Containment() geo_meta = False event_meta = False uuid_geo = Geospace.objects.filter(uuid=uuid)[:1] if len(uuid_geo) > 0: geo_meta = uuid_geo[0] else: # geo information for this item not found, look to parents if parent_uuid is not False \ and 'p_list' in context_metadata: # we have at least 1 parent if 'p_geo' not in context_metadata: # no saved geo information in this context path, so look it up p_list = context_metadata['p_list'] geo_meta = act_contain.get_geochron_from_subject_list(p_list, 'geo') context_metadata['p_geo'] = geo_meta self.parents[parent_uuid] = context_metadata else: # we have saved geo information for this context path so use it geo_meta = context_metadata['p_geo'] uuid_event = Event.objects.filter(uuid=uuid)[:1] if len(uuid_event) > 0: event_meta = uuid_event else: # chrono information for this item not found, look to parents if parent_uuid is not False \ and 'p_list' in context_metadata: # we have at least 1 parent if 'p_event' not in context_metadata: # no saved chrono information in this context path, so look it up p_list = context_metadata['p_list'] event_meta = act_contain.get_geochron_from_subject_list(p_list, 'event') context_metadata['p_event'] = event_meta self.parents[parent_uuid] = context_metadata else: # we have saved chrono information for this context path so use it event_meta = context_metadata['p_event'] context_metadata['geo'] = geo_meta context_metadata['event'] = event_meta return context_metadata
def delete_by_uuid(self, delete_uuid): """ Deletes an item by uuid, returns dictionary object with information about deletion """ self.delete_uuid = delete_uuid output = {} output['done'] = False ok_delete = self.prep_delete_uuid(delete_uuid) if ok_delete: if self.delete_manifest_obj.item_type == 'subjects': cont = Containment() self.delete_children = cont.get_children_by_parent_uuid(delete_uuid, True) cont = Containment() parents = cont.get_parents_by_child_uuid(delete_uuid, False) if len(cont.contexts_list) > 0: parent_uuid = cont.contexts_list[0] # use the deleted item's parent as the new parent for it's child items output['containment'] = self.alter_assertions_by_role('subjects', delete_uuid, parent_uuid, Assertion.PREDICATES_CONTAINS) if ok_delete: output['assertions'] = self.alter_assertions(delete_uuid, False) output['annotations'] = self.alter_annotations(delete_uuid, False) output['altered_children'] = self.update_children_subjects(self.delete_children) output['message'] = 'Deleted item: ' + self.delete_manifest_obj.label + '(' + delete_uuid + ')' self.delete_manifest_obj.delete() # deletes object from the manifest self.delete_type_records(delete_uuid, delete_manifest_obj.item_type) output['done'] = True return output
def add_containment_assertion(self, parent_uuid, child_uuid): """ adds a new spatial containment assertion """ con_exists = Assertion.objects\ .filter(predicate_uuid=Assertion.PREDICATES_CONTAINS, object_uuid=child_uuid)[:1] if len(con_exists) < 1: # child is not contained by something else, so make a containment rel try: parent = Manifest.objects.get(uuid=parent_uuid) except Manifest.DoesNotExist: parent = False try: child = Manifest.objects.get(uuid=child_uuid) except Manifest.DoesNotExist: child = False if parent is not False and child is not False: if parent.item_type == 'subjects'\ and child.item_type == 'subjects': con_ass = Assertion() con_ass.uuid = parent.uuid con_ass.subject_type = parent.item_type con_ass.project_uuid = child.project_uuid con_ass.source_id = self.source_id con_ass.obs_node = self.contain_obs_node con_ass.obs_num = self.contain_obs_num con_ass.sort = self.contain_sort con_ass.visibility = self.visibility con_ass.predicate_uuid = Assertion.PREDICATES_CONTAINS con_ass.object_uuid = child.uuid con_ass.object_type = child.item_type con_ass.save() cont = Containment() child_children = cont.get_children_by_parent_uuid(child.uuid, True) dm = DeleteMerge() dm.update_children_subjects(child_children) else: print('Item already containted in: ' + con_exists[0].uuid)
def add_containment_assertion(self, parent_uuid, child_uuid): """ adds a new spatial containment assertion """ con_exists = Assertion.objects\ .filter(predicate_uuid=Assertion.PREDICATES_CONTAINS, object_uuid=child_uuid)[:1] if len(con_exists) < 1: # child is not contained by something else, so make a containment rel try: parent = Manifest.objects.get(uuid=parent_uuid) except Manifest.DoesNotExist: parent = False try: child = Manifest.objects.get(uuid=child_uuid) except Manifest.DoesNotExist: child = False if parent is not False and child is not False: if parent.item_type == 'subjects'\ and child.item_type == 'subjects': con_ass = Assertion() con_ass.uuid = parent.uuid con_ass.subject_type = parent.item_type con_ass.project_uuid = child.project_uuid con_ass.source_id = self.source_id con_ass.obs_node = self.contain_obs_node con_ass.obs_num = self.contain_obs_num con_ass.sort = self.contain_sort con_ass.visibility = self.visibility con_ass.predicate_uuid = Assertion.PREDICATES_CONTAINS con_ass.object_uuid = child.uuid con_ass.object_type = child.item_type con_ass.save() cont = Containment() child_children = cont.get_children_by_parent_uuid( child.uuid, True) dm = DeleteMerge() dm.update_children_subjects(child_children) else: print('Item already containted in: ' + con_exists[0].uuid)
def get_geo_event_metadata(self): """ gets geospatial and event metadata for the item """ if self.is_valid: act_contain = Containment() if self.manifest.item_type == 'subjects': parents = act_contain.get_parents_by_child_uuid(self.manifest.uuid) subject_list = act_contain.contexts_list subject_list.insert(0, self.manifest.uuid) self.geo_meta = act_contain.get_geochron_from_subject_list(subject_list, 'geo') self.event_meta = act_contain.get_geochron_from_subject_list(subject_list, 'event') if self.event_meta is not False and self.event_meta is not None: start = None stop = None for event in self.event_meta: if start is None: start = event.start if stop is None: stop = event.stop if start < event.start: start = event.start if stop > event.stop: stop = event.stop if stop is None: stop = start if start is not None: if stop < start: stop_temp = start start = stop stop = stop_temp # we have a start year, so make a temporal value in ISON 8601 format self.temporal = ISOyears().make_iso_from_float(start) if stop != start: # stop year different from start, so add a / sep and the stop # year in ISO 8601 format self.temporal += '/' + ISOyears().make_iso_from_float(stop)
def get_geo_event_metadata(self): """ gets geospatial and event metadata for the item """ if self.is_valid: act_contain = Containment() if self.manifest.item_type == 'subjects': parents = act_contain.get_parents_by_child_uuid( self.manifest.uuid) subject_list = act_contain.contexts_list subject_list.insert(0, self.manifest.uuid) self.geo_meta = act_contain.get_geochron_from_subject_list( subject_list, 'geo') self.event_meta = act_contain.get_geochron_from_subject_list( subject_list, 'event') else: self.geo_meta = act_contain.get_related_geochron( self.manifest.uuid, self.manifest.item_type, 'geo') self.event_meta = act_contain.get_related_geochron( self.manifest.uuid, self.manifest.item_type, 'event') if self.event_meta is not False and self.event_meta is not None: start = None stop = None for event in self.event_meta: if start is None: start = event.start if stop is None: stop = event.stop if start < event.start: start = event.start if stop > event.stop: stop = event.stop if stop is None: stop = start if start is not None: if stop < start: stop_temp = start start = stop stop = stop_temp # we have a start year, so make a temporal value in ISON 8601 format self.temporal = ISOyears().make_iso_from_float(start) if stop != start: # stop year different from start, so add a / sep and the stop # year in ISO 8601 format self.temporal += '/' + ISOyears().make_iso_from_float( stop) if self.temporal is None and self.manifest.item_type == 'projects': # get project teporal metadata via solr # now query Solr for temporal data cq = CompleteQuery() payload = {'proj': self.manifest.slug} ass_metadata = cq.get_json_query(payload, None) if 'dc-terms:temporal' in ass_metadata: self.temporal = ass_metadata['dc-terms:temporal']
def generate_save_context_path_from_uuid(self, uuid, do_children=True): """ Generates and saves a context path for a subject item by uuid """ output = False try: man_obj = Manifest.objects.get(uuid=uuid, item_type='subjects') except Manifest.DoesNotExist: man_obj = False if man_obj is not False: if man_obj.item_type == 'subjects': output = self.generate_save_context_path_from_manifest_obj( man_obj) if do_children: act_contain = Containment() # get the contents recusivelhy contents = act_contain.get_children_by_parent_uuid( uuid, True) if isinstance(contents, dict): for tree_node, children in contents.items(): for child_uuid in children: # do the children, but not recursively since we # already have a resurive look up of contents output = self.generate_save_context_path_from_uuid( child_uuid, False) return output
def generate_context_path(self, uuid, include_self=True, delim='/'): """ generates a context path for a subject with a given uuid """ path = False act_contain = Containment() contexts = [] r_contexts = act_contain.get_parents_by_child_uuid(uuid) for tree_node, r_parents in r_contexts.items(): # now reverse the list of parent contexts, so top most parent context is first, # followed by children contexts contexts = r_parents[::-1] if (include_self): contexts.append(uuid) if (len(contexts) > 0): path_items = [] for p_uuid in contexts: try: act_p = Manifest.objects.get(uuid=p_uuid) path_items.append(act_p.label) except Manifest.DoesNotExist: return False path = delim.join(path_items) return path
def get_geo_event_metadata(self): """ gets geospatial and event metadata for the item """ if self.is_valid: act_contain = Containment() if self.manifest.item_type == 'subjects': parents = act_contain.get_parents_by_child_uuid(self.manifest.uuid) subject_list = act_contain.contexts_list subject_list.insert(0, self.manifest.uuid) self.geo_meta = act_contain.get_geochron_from_subject_list(subject_list, 'geo') self.event_meta = act_contain.get_geochron_from_subject_list(subject_list, 'event') else: self.geo_meta = act_contain.get_related_geochron(self.manifest.uuid, self.manifest.item_type, 'geo') self.event_meta = act_contain.get_related_geochron(self.manifest.uuid, self.manifest.item_type, 'event') if self.event_meta is not False and self.event_meta is not None: start = None stop = None for event in self.event_meta: if start is None: start = event.start if stop is None: stop = event.stop if start < event.start: start = event.start if stop > event.stop: stop = event.stop if stop is None: stop = start if start is not None: if stop < start: stop_temp = start start = stop stop = stop_temp # we have a start year, so make a temporal value in ISON 8601 format self.temporal = ISOyears().make_iso_from_float(start) if stop != start: # stop year different from start, so add a / sep and the stop # year in ISO 8601 format self.temporal += '/' + ISOyears().make_iso_from_float(stop) if self.temporal is None and self.manifest.item_type == 'projects': # get project teporal metadata via solr # now query Solr for temporal data cq = CompleteQuery() payload = {'proj': self.manifest.slug} ass_metadata = cq.get_json_query(payload, None) if 'dc-terms:temporal' in ass_metadata: self.temporal = ass_metadata['dc-terms:temporal']
def get_related_uuids(self, uuid, inclusive=True): """ gets a list of uuids related to a given uuid if inclusive, include the UUID passed in the output list """ link_item_types = ['subjects', 'media', 'persons', 'documents', 'projects'] uuids = [] if isinstance(uuid, list): start_uuids = uuid else: start_uuids = [uuid] for uuid in start_uuids: try: m_obj = Manifest.objects.get(uuid=uuid) except Manifest.DoesNotExist: m_obj = False if m_obj is not False: if inclusive: uuids.append(m_obj.uuid) if m_obj.item_type == 'subjects': act_contain = Containment() # get the contents recusivelhy contents = act_contain.get_children_by_parent_uuid(m_obj.uuid, True) if isinstance(contents, dict): for tree_node, children in contents.items(): for child_uuid in children: if child_uuid not in uuids: uuids.append(child_uuid) elif m_obj.item_type == 'predicates': # reindex all of the items described by a given predicate # this can take a while! rel_objs = Assertion.objects\ .filter(predicate_uuid=m_obj.uuid) for rel_item in rel_objs: if rel_item.uuid not in uuids: uuids.append(rel_item.uuid) elif m_obj.item_type == 'types': # reindex all of the items described by a given predicate # this can take a while! rel_objs = Assertion.objects\ .filter(object_uuid=m_obj.uuid) for rel_item in rel_objs: if rel_item.uuid not in uuids: uuids.append(rel_item.uuid) rel_objs = Assertion.objects\ .filter(uuid=m_obj.uuid, object_type__in=link_item_types) for rel_item in rel_objs: if rel_item.object_uuid not in uuids: uuids.append(rel_item.object_uuid) rel_subs = Assertion.objects\ .filter(subject_type__in=link_item_types, object_uuid=m_obj.uuid) for rel_item in rel_subs: if rel_item.object_uuid not in uuids: uuids.append(rel_item.object_uuid) return uuids
def suggest_valid_label(self, prefix, num_id_len=False, label_increment=1): """ Suggests a valid label, unique for the whole project or for a context """ if self.uuid is not False: uuid_exclude = 'skip' else: uuid_exclude = self.uuid man_list = [] if self.context_uuid is not False: uuids = [] if self.item_type == 'subjects': # get items with the same parent context # that will be the scope for a unique label cont = Containment() cont.get_children_by_parent_uuid(self.context_uuid) for tree_key, children in cont.contents.items(): for child_uuid in children: if child_uuid not in uuids: uuids.append(child_uuid) else: # get items (of the same item_type) related to a context # that will be the scope for a unique label rel_list = Assertion.objects\ .filter(uuid=self.context_uuid, object_type=self.item_type) for rel in rel_list: if rel.uuid not in uuids: uuids.append(rel.uuid) if len(uuids) > 0: if len(prefix) < 1: man_list = Manifest.objects\ .filter(project_uuid=self.project_uuid, uuid__in=uuids)\ .exclude(uuid=uuid_exclude)\ .order_by('-label') else: man_list = Manifest.objects\ .filter(project_uuid=self.project_uuid, label__startswith=prefix, uuid__in=uuids)\ .exclude(uuid=uuid_exclude)\ .order_by('-label') else: # print('Params: ' + self.project_uuid + ', ' + self.item_type + ', ' + prefix); if len(prefix) < 1: man_list = Manifest.objects\ .filter(project_uuid=self.project_uuid, item_type=self.item_type)\ .exclude(uuid=uuid_exclude)\ .order_by('-label')[:1] else: man_list = Manifest.objects\ .filter(project_uuid=self.project_uuid, label__startswith=prefix, item_type=self.item_type)\ .exclude(uuid=uuid_exclude)\ .order_by('-label')[:1] if len(man_list) > 0: # print('Checking: ' + man_list[0].label) if len(prefix) > 0: label_id_part = man_list[0].label.replace(prefix, '') else: label_id_part = man_list[0].label vals = [] # get the numbers out q_nums_strs = re.findall(r'[-+]?\d*\.\d+|\d+', label_id_part) for q_num_str in q_nums_strs: vals.append(q_num_str) try: last_id = float(''.join(vals)) except: last_id = 0 new_id = last_id + label_increment else: new_id = label_increment new_label = prefix + self.prepend_zeros(new_id, num_id_len) new_label_exists = self.check_label_exists_in_scope(new_label) if new_label_exists is not False: # despite our best efforts, we made a new label that already exists # try again self.recusion_count += 1 label_increment += 1 if self.recusion_count <= 20: # do this recursively to make a new label new_label = self.suggest_valid_label(prefix, num_id_len, label_increment) return new_label
def get_spatial_temporal_context(self): """ gets the item spatial context """ act_contain = Containment() if self.manifest.item_type == 'subjects': # get item geospatial and chronological metadata if subject item # will do it differently if not a subject item parents = act_contain.get_parents_by_child_uuid(self.manifest.uuid) self.contexts = parents # prepare a list of contexts (including the current item) to check for # geospatial and event / chronology metadata subject_list = act_contain.contexts_list subject_list.insert(0, self.manifest.uuid) self.geo_meta = act_contain.get_geochron_from_subject_list( subject_list, 'geo') self.temporal_meta = act_contain.get_geochron_from_subject_list( subject_list, 'temporal') self.event_meta = act_contain.get_geochron_from_subject_list( subject_list, 'event') # now get any children items, contained in this "subjects" item act_contain = Containment() self.contents = act_contain.get_children_by_parent_uuid( self.manifest.uuid) else: parents = act_contain.get_related_context(self.manifest.uuid) self.contexts = False self.linked_contexts = parents if self.manifest.item_type == 'projects': # get project metadata objects directly pm = ProjectMeta() project = self.item_gen_cache.get_project_model_object( self.manifest.uuid) sub_projects = self.item_gen_cache.get_project_subprojects( self.manifest.uuid) if project is not None: if isinstance(project.meta_json, dict): if Project.META_KEY_GEO_SPECIFICITY in project.meta_json: # the project has some default geographic specificity noted # use this value when making geo_meta pm.project_specificity = project.meta_json[ Project.META_KEY_GEO_SPECIFICITY] self.geo_meta = pm.get_project_geo_from_db(self.manifest.uuid) if self.geo_meta is False: # make geospatial metadata for the project, and sub-projects if they exist pm.make_geo_meta(self.manifest.uuid, sub_projects) self.geo_meta = pm.geo_objs act_contain = Containment() if self.geo_meta is False: self.geo_meta = act_contain.get_related_geochron( self.manifest.uuid, self.manifest.item_type, 'geo') if self.temporal_meta is False: self.temporal_meta = act_contain.get_related_geochron( self.manifest.uuid, self.manifest.item_type, 'temporal') if self.temporal_meta is False: # now look in the project for temporal metadata self.temporal_meta = act_contain.get_temporal_from_project( self.manifest.project_uuid) if self.event_meta is False: self.event_meta = act_contain.get_related_geochron( self.manifest.uuid, self.manifest.item_type, 'event')