def get_target_list(self, annotation): target_list = annotation.get_targets_info() deeper_targets = [] for target in target_list: if self.is_annotation(target): if target["id"] == annotation.id: raise AnnotationError( message="Annotation cannot target itself") if self.is_deleted(target["id"]): continue target_annotation = self.get_annotation_es(target['id'], params={ "username": None, "action": "traverse" }) deeper_targets += self.get_target_list( Annotation(target_annotation)) target_ids = [target["id"] for target in target_list] for target in deeper_targets: if target not in target_ids: target_list += [target] target_ids += [target["id"]] return target_list
def add_annotation_to_collection_es(self, annotation_id, collection_id, params): # check that user is allowed to edit collection collection = self.get_from_index_if_allowed( collection_id, username=params["username"], action="edit", annotation_type="AnnotationCollection") # check if collection contains annotation if collection.has_annotation(annotation_id): raise AnnotationError( message="Collection already contains this annotation") # check that user is allowed to see annotation self.get_from_index_if_allowed(annotation_id, username=params["username"], action="see", annotation_type="Annotation") # add annotation collection.add_annotation(annotation_id) # add permissions for access (see) and update (edit) permissions.add_permissions(collection, params) self.update_in_index(collection.to_json(), "AnnotationCollection") # set index needs refresh before next GET self.set_index_needs_refresh() # return collection metadata return collection.to_clean_json(params)
def remove_annotation(self, annotation_id): try: self.items.remove(annotation_id) except ValueError: raise AnnotationError( message= "Annotation Collection does not contain annotation with id %s" % (annotation_id)) self.modified = datetime.datetime.now(pytz.utc).isoformat()
def should_not_exist(self, annotation_id, annotation_type="_all"): if self.es.exists(index=self.es_index, doc_type=annotation_type, id=annotation_id): raise AnnotationError( message="Annotation with id %s already exists" % (annotation_id)) else: return True
def should_exist(self, annotation_id, annotation_type="_all"): if self.es.exists(index=self.es_index, doc_type=annotation_type, id=annotation_id): if not self.is_deleted(annotation_id, annotation_type): return True raise AnnotationError(message="Annotation with id %s does not exist" % (annotation_id), status_code=404)
def should_have_target_list(self, annotation): if "status" in annotation and annotation["status"] == "deleted": return False if annotation["type"] == "AnnotationCollection": return False if "target_list" not in annotation or not annotation["target_list"]: raise AnnotationError(message="{t} should have target list".format( t=annotation["type"])) return True
def set_container_content(self, data, total): data_json = self.make_json(data) if self.is_annotation_collection(data_json): self.items = data_json["items"] self.generate_metadata_from_collection(data_json) elif self.is_annotation_list(data_json): self.items = data_json self.generate_metadata_from_annotations(data_json, total) else: raise AnnotationError(message="data should be an AnnotationCollection or a list of Annotations")
def make_json(self, data): if isinstance(data, AnnotationCollection): return data.to_json() if isinstance(data, Annotation): return data.data elif isinstance(data, list): return [self.make_json(item) for item in data] elif isinstance(data, dict) and "type" in data: return data else: raise AnnotationError(message="data should be an AnnotationCollection or a list of Annotations")
def set_view(self, view): if view == "PreferMinimalContainer": self.view = self.viewMinimalContainer self.iris=1 elif view == "PreferContainedIRIs": self.view = self.viewContainedIRIs self.iris=1 elif view == "PreferContainedDescriptions": self.view = self.viewContainedDescriptions self.iris=0 else: raise AnnotationError(message="%s is not a valid container option. Value MUST be one of PreferMinimalContainer, PreferContainedIRIs, PreferContainedDescriptions" % (view)) self.base_url = self.update_url(self.base_url, {"iris": self.iris})
def update_chained_annotations(self, annotation_id): # first refresh the index self.es.indices.refresh(index=self.es_index) chain_annotations = self.get_from_index_by_target( {"id": annotation_id}) for chain_annotation in chain_annotations: if chain_annotation["id"] == annotation_id: raise AnnotationError( message="Annotation cannot target itself") chain_annotation["target_list"] = self.get_target_list( Annotation(chain_annotation)) # don't use permission parameters for chained annotations self.update_annotation_es(chain_annotation, params={ "username": None, "action": "traverse" })
def remove_annotation_from_collection_es(self, annotation_id, collection_id, params): # check that user is allowed to edit collection collection = self.get_from_index_if_allowed( collection_id, username=params["username"], action="edit", annotation_type="AnnotationCollection") # check if collection contains annotation if not collection.has_annotation(annotation_id): raise AnnotationError( message="Collection doesn't contain this annotation") # check that user is allowed to see annotation self.get_from_index_if_allowed(annotation_id, username=params["username"], action="see", annotation_type="Annotation") # remove annotation collection.remove_annotation(annotation_id) self.update_in_index(collection.to_json(), "AnnotationCollection") # return collection metadata return collection.to_json()
def view_page(self, page=0): if not isinstance(page, int) or page < 0: raise AnnotationError(message="Parameter page must be non-negative integer") return self.generate_page_metadata(page)
def set_page_size(self, page_size): if type(page_size) != int or page_size < 1: raise AnnotationError(message='page_size must be a positive integer value') self.page_size = page_size