def create_v2_annotation(request, annotation_id: str, config: Dict) -> Optional[Dict]: # check for image annotations first fq: List[str] = ["type:image", f"id:{annotation_id}_image"] fl: List[str] = ["id", "surface_id", "width_i", "height_i", "object_id"] image_record = SolrConnection.search("*:*", fq=fq, fl=fl, rows=1) if image_record.hits != 0: image_annotation = ImageAnnotation(image_record.docs[0], context={ "request": request, "config": config, "direct_request": True }) return image_annotation.data # check if there's a non-image annotation matching the id # safest to put these in separate solr calls because of the child documents fq = ["type:annotation", f"id:{annotation_id}"] fl = [ "*", "[child parentFilter=type:annotation childFilter=type:annotation_body]" ] anno_record = SolrConnection.search("*:*", fq=fq, fl=fl, rows=1) if anno_record.hits == 0: return None annotation = TextAnnotation(anno_record.docs[0], context={ "request": request, "config": config, "direct_request": True }) return annotation.data
def create_v3_collection(request: Any, collection_id: str, config: Dict) -> Optional[Dict]: """ Retrieves a collection object from Solr. Collection records are stored as `type:collection` in Solr and collection IDs are attached to individual objects. This method first retrieves the collection, and then passes it along to the Collection serializer, which will manage the retrieval of the individual members of the collection. A special case is the 'top' collection, which will retrieve a list of all other collections (except 'top' and 'all'). The 'all' collection is a list of every manifest object we have in the Solr core. :param request: A sanic request object :param collection_id: A collection id to retrieve. :param config: The configuration dictionary :return: A Dict representing a IIIF-serialized Collection. """ fq: List = ["type:collection", f'collection_id:"{collection_id.lower()}"'] record: pysolr.Results = SolrConnection.search("*:*", fq=fq, rows=1) if record.hits == 0: return None object_record = record.docs[0] collection: Collection = Collection(object_record, context={ "request": request, "config": config }) return collection.data
def create_ordered_collection_page(request, page_id: int, config: Dict) -> Optional[Dict]: fq: List = ["type:object", "!all_collections_id_sm:talbot"] sort: str = "accessioned_dt asc, shelfmark_sort_ans asc, id asc" fl: List = ["id", "accessioned_dt", "full_shelfmark_s"] rows: int = config['solr']['pagesize'] start: int = page_id * int(rows) results: pysolr.Results = SolrConnection.search("*:*", fq=fq, fl=fl, sort=sort, rows=rows, start=start) if results.hits == 0: return None return OrderedCollectionPage({ 'results': results }, context={ 'request': request, 'config': config, 'page_id': page_id, 'direct_request': True }).data
def create_ordered_collection(request, req_id: str, config: Dict) -> Optional[Dict]: # pylint: disable-msg=unused-argument """ Creates the root object for Activity Stream responses. It is a minimal object, containing only pointers to the first and last pages, and the total number of results. :param request: A Sanic request object :param req_id: UNUSED. Added for API compatibility with other AS response functions :param config: A manifest server configuration dict :return: A dictionary for serialization as a JSON-LD response. """ fq: List = ["type:object", "!all_collections_id_sm:talbot"] fl: List = ["id", "accessioned_dt", "full_shelfmark_s"] # We only need the number of hits for this query, so we don't have to retrieve any documents, # only the total that would be returned rows: int = 0 results: pysolr.Results = SolrConnection.search("*:*", fq=fq, fl=fl, rows=rows) if results.hits == 0: return None return OrderedCollection({'results': results}, context={ "request": request, "config": config, "direct_request": True }).data
def create_v2_canvas(request, canvas_id: str, config: Dict) -> Optional[Dict]: """ Creates a new canvas in response to a request. Used for directly requesting canvases. :param request: A Sanic request object :param canvas_id: An ID passed in from the URL. Note that this lacks the '_surface' suffix :param config: A server configuration dictionary :return: A V2 Canvas object """ fq = ["type:surface", f"id:{canvas_id}_surface"] fl = ["*,[child parentFilter=type:surface childFilter=type:image]"] sort = "sort_i asc" record = SolrConnection.search("*:*", fq=fq, fl=fl, sort=sort, rows=1) if record.hits == 0: return None canvas_record = record.docs[0] canvas: Canvas = Canvas(canvas_record, context={ "request": request, "config": config, "direct_request": True }) return canvas.data
def get_within(self, obj: SolrResult) -> Optional[List]: """ When requested directly, give a within parameter to point back to the parent manuscript. """ direct_request: bool = self.context.get('direct_request') if not direct_request: return None req = self.context.get('request') cfg = self.context.get('config') manifest_tmpl: str = cfg['templates']['manifest_id_tmpl'] wid: str = get_identifier(req, obj.get('object_id'), manifest_tmpl) # get object shelfmark for the label fq = [f'id:"{obj.get("object_id")}"', 'type:object'] fl = ['full_shelfmark_s'] res = SolrConnection.search(q='*:*', fq=fq, fl=fl, rows=1) if res.hits == 0: return None object_record = res.docs[0] return [{ "@id": wid, "@type": "Manifest", "label": object_record.get('full_shelfmark_s') }]
def get_items(self, obj: SolrResult) -> Optional[List]: req = self.context.get('request') cfg = self.context.get('config') obj_id: str = obj.get('id') # Check if the canvases have annotations. We don't actually # need to retrieve them, just get the number of hits. has_annotations_res = SolrConnection.search( "*:*", fq=["type:annotationpage", f"object_id:{obj_id}"], rows=0) has_annotations = has_annotations_res.hits > 0 manager: SolrManager = SolrManager(SolrConnection) fq: List = ["type:surface", f"object_id:{obj_id}"] sort: str = "sort_i asc" fl: List = [ "*,[child parentFilter=type:surface childFilter=type:image]" ] rows: int = 100 manager.search("*:*", fq=fq, fl=fl, sort=sort, rows=rows) if manager.hits == 0: return None return Canvas(manager.results, context={ "request": req, "config": cfg, "has_annotations": has_annotations }, many=True).data
def create_v3_annotation_page(request, annotation_page_id: str, config: Dict) -> Optional[Dict]: fq = [f'id:"{annotation_page_id}_surface" OR id:{annotation_page_id}'] fl = [ '*,[child parentFilter="type:surface OR type:annotationpage" childFilter="type:image"]' ] sort = "sort_i asc" record = SolrConnection.search("*:*", fq=fq, fl=fl, sort=sort, rows=1) if record.hits == 0: return None annopage_record: Dict = record.docs[0] annopage: BaseAnnotationPage if annopage_record['type'] == "surface": annopage = ImageAnnotationPage(annopage_record, context={ "request": request, "config": config, "direct_request": True }) else: annopage = TextAnnotationPage(annopage_record, context={ "request": request, "config": config, "direct_request": True }) return annopage.data
def create_v2_sequence(request, sequence_id: str, config: Dict) -> Optional[Dict]: record: pysolr.Results = SolrConnection.search("*:*", fq=["type:object", f"id:{sequence_id}"], rows=1) if record.hits == 0: return None object_record = record.docs[0] sequence: Sequence = Sequence(object_record, context={"request": request, "config": config, "direct_request": True}) return sequence.data
def create_v2_manifest(request, manifest_id: str, config: Dict) -> Optional[Dict]: record: pysolr.Results = SolrConnection.search( "*:*", fq=["type:object", f"id:{manifest_id}"], rows=1) if record.hits == 0: return None object_record = record.docs[0] manifest: Manifest = Manifest(object_record, context={ "request": request, "config": config, "solr_conn": SolrConnection }) return manifest.data
def create_activity(request, manifest_id: str, config: Dict) -> Optional[Dict]: fq: List = ["type:object", f"id:{manifest_id}"] fl: List = ["id", "accessioned_dt", "full_shelfmark_s"] rows: int = 1 results: pysolr.Results = SolrConnection.search("*:*", fq=fq, fl=fl, rows=rows) if results.hits == 0: return None return Activity(results.docs[0], context={ 'request': request, 'config': config, 'direct_request': True }).data
def create_v2_annotation_list(request: Any, annotation_page_id: str, config: Dict) -> Optional[Dict]: """ :param request: A sanic request object :param annotation_page_id: An annotation page to retrieve annotations for :param config: The configuration dict :return: A Dict representing a IIIF-serialized Annotation List. """ fq: List = [f'id:"{annotation_page_id}"'] fl = ["id,label_s"] record = SolrConnection.search(q="*:*", fq=fq, fl=fl, rows=1) if record.hits == 0: return None annotation_list: AnnotationList = AnnotationList(record.docs[0], context={ "request": request, "config": config }) return annotation_list.data