예제 #1
0
    def get_items(self,
                  obj) -> List[Dict]:  # pylint: disable-msg=unused-argument
        req = self.context.get('request')
        cfg = self.context.get('config')

        coll_id_tmpl: str = cfg['templates']['collection_id_tmpl']
        coll_top_id: str = get_identifier(req, "top", coll_id_tmpl)
        coll_all_id: str = get_identifier(req, "all", coll_id_tmpl)

        as_id_tmpl: str = cfg['templates']['activitystream_id_tmpl']
        as_top_id: str = get_identifier(req, "all-changes", as_id_tmpl)

        return [{
            "id": coll_top_id,
            "type": "Collection",
            "name": "Top-level collection"
        }, {
            "id": coll_all_id,
            "type": "Collection",
            "name": "All manifests"
        }, {
            "id": as_top_id,
            "type": "OrderedCollection",
            "name": "ActivityStream"
        }]
예제 #2
0
    def get_mid(self, obj: SolrResult) -> str:
        req = self.context.get('request')
        cfg = self.context.get('config')
        manifest_id: str = obj.get('id')
        manifest_id_tmpl: str = cfg['templates']['manifest_id_tmpl']

        return get_identifier(req, manifest_id, manifest_id_tmpl)
예제 #3
0
    def get_cid(self, obj: SolrResult) -> str:
        req = self.context.get('request')
        cfg = self.context.get('config')

        tmpl: str = cfg['templates']['annolist_id_tmpl']

        return get_identifier(req, obj['id'], tmpl)
예제 #4
0
    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')
        }]
예제 #5
0
    def get_cid(self, obj: SolrResult) -> str:
        req = self.context.get('request')
        cfg = self.context.get('config')
        tmpl: str = cfg['templates']['collection_id_tmpl']
        cid: str = obj.get('collection_id')

        return get_identifier(req, cid, tmpl)
예제 #6
0
    def get_id(self, obj: Dict) -> str:  # pylint: disable-msg=unused-argument
        req = self.context.get('request')
        conf = self.context.get('config')
        streams_tmpl: str = conf['templates']['activitystream_id_tmpl']
        page_id: int = self.context.get('page_id')

        return get_identifier(req, f"page-{page_id}", streams_tmpl)
예제 #7
0
def compute_v2_hierarchy(results: List, object_id: str, request,
                         config: Dict) -> Dict:
    """
    Takes a list of work object results and returns a dictionary
    corresponding to the work hierarchy (ranges and sub-ranges).

    :param results: A list of Solr work results
    :param object_id: An Object ID
    :param request: A Sanic request object
    :param config: A manifest server config dict
    :return: A work object hierarchy dictionary keyed by URI
    """
    hierarchy: Dict = {}

    for res in results:
        parent_wk_id: str = res.get('parent_work_id', None)
        wk_id: str = res.get('work_id')

        if not parent_wk_id:
            continue

        if parent_wk_id not in hierarchy:
            hierarchy[parent_wk_id] = []

        range_tmpl: str = config['templates']['range_id_tmpl']
        ident: str = get_identifier(request,
                                    object_id,
                                    range_tmpl,
                                    range_id=wk_id)

        hierarchy[parent_wk_id].append(ident)

    return hierarchy
예제 #8
0
    def get_sid(self, obj: Dict) -> str:
        req = self.context.get('request')
        cfg = self.context.get('config')
        obj_id = obj.get('id')
        sequence_tmpl = cfg['templates']['sequence_id_tmpl']

        return get_identifier(req, obj_id, sequence_tmpl)
예제 #9
0
def create_v3_range(request, range_id: str, config: Dict) -> Optional[Dict]:
    """
    Handles a lookup for a particular range ID. Works by constructing the whole
    tree of results, and then searching in that result for the requested range. This is
    necessary because the results stored in Solr are flattened, so we need to know
    the nested relationships of all objects before selecting just the sub-part of the tree.

    :param request: A Sanic request object
    :param range_id: A range ID that has been requested
    :param config: A configuration dictionary
    :return: A Dictionary containing the requested part of the tree.
    """
    if "/" not in range_id:
        return None

    obj_id, log_id = range_id.split("/")

    range_tmpl: str = config['templates']['range_id_tmpl']
    ident: str = get_identifier(request, obj_id, range_tmpl, range_id=log_id)
    tree: Optional[List] = create_v3_structures(request, obj_id, config, direct_request=True)

    if not tree:
        return None

    res: Dict = __find_in_tree(tree, ident)

    return res
예제 #10
0
    def get_homepage(self, obj: SolrResult) -> List:
        req = self.context.get('request')
        cfg = self.context.get('config')

        tmpl: str = cfg['templates']['digital_bodleian_permalink_tmpl']
        uuid: str = obj.get("id")

        conn: SolrManager = SolrManager(SolrConnection)
        fq: List = ['type:link', f"object_id:{uuid}"]

        conn.search("*:*", fq=fq)

        links: List = [{
            'id': get_identifier(req, uuid, tmpl),
            'type': "Text",
            "label": {
                "en": ["View on Digital Bodleian"]
            },
            "format": "text/html",
            "language": ["en"]
        }]

        if conn.hits > 0:
            for r in conn.results:
                links.append({
                    'id': r.get('target_s'),
                    'type': "Text",
                    "label": {
                        "en": [r.get('label_s')]
                    },
                    "format": "text/html",
                    "language": ["en"]
                })

        return links
예제 #11
0
    def get_id(self, obj: SolrResult) -> str:
        req = self.context.get('request')
        cfg = self.context.get('config')
        activity_create_id_tmpl: str = cfg['templates'][
            'activitystream_create_id_tmpl']

        return get_identifier(req, obj.get('id'), activity_create_id_tmpl)
예제 #12
0
    def get_service(self, obj: SolrResult) -> Dict:
        req = self.context.get('request')
        cfg = self.context.get('config')
        image_tmpl = cfg['templates']['image_id_tmpl']
        identifier = re.sub(IMAGE_ID_SUB, "", obj.get("id"))
        image_id = get_identifier(req, identifier, image_tmpl)

        return {"type": "ImageService2", "profile": "level1", "id": image_id}
예제 #13
0
    def get_part_of(self,
                    obj: Dict) -> Dict:  # pylint: disable-msg=unused-argument
        req = self.context.get('request')
        conf = self.context.get('config')
        streams_tmpl: str = conf['templates']['activitystream_id_tmpl']

        parent_id = get_identifier(req, 'all-changes', streams_tmpl)

        return {"id": parent_id, "type": "OrderedCollection"}
예제 #14
0
    def get_first(self, obj: Dict) -> Dict:  # pylint: disable-msg=unused-argument
        req = self.context.get('request')
        cfg = self.context.get('config')
        page_tmpl: str = cfg['templates']['activitystream_id_tmpl']

        return {
            "id": get_identifier(req, 'page-0', page_tmpl),
            "type": "OrderedCollectionPage"
        }
예제 #15
0
    def get_sid(self, obj: SolrResult) -> str:
        req = self.context.get('request')
        cfg = self.context.get('config')
        range_tmpl: str = cfg['templates']['range_id_tmpl']

        identifier: str = obj.get("object_id")
        range_id: str = obj.get("work_id")

        return get_identifier(req, identifier, range_tmpl, range_id=range_id)
예제 #16
0
    def get_aid(self, obj: SolrResult) -> str:
        req = self.context.get('request')
        cfg = self.context.get('config')
        annotation_tmpl: str = cfg['templates']['annotation_id_tmpl']

        # The substitution here is only needed for image annotations, but won't affect other annotations
        annotation_id: str = re.sub(IMAGE_ID_SUB, "", obj["id"])

        return get_identifier(req, annotation_id, annotation_tmpl)
예제 #17
0
    def get_cid(self, obj: SolrResult) -> str:
        req = self.context.get('request')
        cfg = self.context.get('config')
        canvas_tmpl = cfg['templates']['canvas_id_tmpl']

        # Surfaces have the suffix "_surface" in Solr. Strip it off for this identifier
        canvas_id = re.sub(SURFACE_ID_SUB, "", obj.get("id"))

        return get_identifier(req, canvas_id, canvas_tmpl)
예제 #18
0
    def get_iid(self, obj: SolrResult) -> str:
        cfg = self.context.get('config')
        req = self.context.get('request')

        image_tmpl = cfg['templates']['image_id_tmpl']

        # Images have the suffix "_image" in Solr.
        identifier = re.sub(IMAGE_ID_SUB, "", obj.get("id"))
        return get_identifier(req, identifier, image_tmpl)  # type: ignore
예제 #19
0
    def get_object(self, obj: SolrResult) -> Dict:
        req = self.context.get('request')
        cfg = self.context.get('config')

        manifest_tmpl: str = cfg['templates']['manifest_id_tmpl']
        mfid = get_identifier(req, obj.get('id'), manifest_tmpl)
        label: str = obj.get("full_shelfmark_s")

        return {"id": mfid, "type": "Manifest", "name": label}
예제 #20
0
    def get_aid(self, obj: SolrResult) -> str:
        req = self.context.get('request')
        cfg = self.context.get('config')
        annopage_tmpl: str = cfg['templates']['annopage_id_tmpl']

        # Surfaces have the suffix "_surface" in Solr. Strip it off for this identifier
        # this isn't necessary for annotationpage ids, but also won't affect them
        annopage_id: str = re.sub(SURFACE_ID_SUB, "", obj.get("id"))

        return get_identifier(req, annopage_id, annopage_tmpl)
예제 #21
0
    def get_service(self, obj: SolrResult) -> Dict:
        req = self.context.get('request')
        cfg = self.context.get('config')
        image_tmpl = cfg['templates']['image_id_tmpl']
        identifier = re.sub(IMAGE_ID_SUB, "", obj.get("id"))
        image_id = get_identifier(req, identifier, image_tmpl)  # type: ignore

        return {
            "@context": "http://iiif.io/api/image/2/context.json",
            "profile": "http://iiif.io/api/image/2/level1.json",
            "@id": image_id
        }
예제 #22
0
    def get_target(self, obj: SolrResult) -> str:
        """
        This method may be overridden in subclasses to e.g. reference a specific region of the canvas
        :param obj: A Solr Result object
        :return: the uri for the canvas this annotation is attached to
        """
        req = self.context.get('request')
        cfg = self.context.get('config')
        canvas_tmpl: str = cfg['templates']['canvas_id_tmpl']

        identifier: str = re.sub(SURFACE_ID_SUB, "", obj['surface_id'])
        return get_identifier(req, identifier, canvas_tmpl)
예제 #23
0
    def get_last(self, obj: Dict) -> Dict:
        req = self.context.get('request')
        cfg = self.context.get('config')

        pagesize: int = int(cfg['solr']['pagesize'])
        hits: int = int(obj.get('results').hits)
        page_no: int = math.floor(hits / pagesize)
        page_id: str = f"page-{page_no}"
        page_tmpl: str = cfg['templates']['activitystream_id_tmpl']

        return {
            "id": get_identifier(req, page_id, page_tmpl),
            "type": "OrderedCollectionPage"
        }
예제 #24
0
    def get_on(self, obj: SolrResult) -> Union[str, List[Dict]]:
        """
        This method may be overridden in subclasses to e.g. reference a specific region of the canvas
        :param obj:
        :return: the uri for the canvas this annotation is attached to
        """
        req = self.context.get('request')
        cfg = self.context.get('config')
        canvas_tmpl = cfg['templates']['canvas_id_tmpl']

        identifier = re.sub(SURFACE_ID_SUB, "", obj['surface_id'])
        identifier_uri = get_identifier(req, identifier, canvas_tmpl)

        return identifier_uri
예제 #25
0
    def get_rendering(self, obj: SolrResult) -> Optional[Dict]:
        # Talbot manifests are a bit different, so exclude their metadata
        if 'talbot' in obj.get('all_collections_id_sm', []):  # type: ignore
            return None

        req = self.context.get('request')
        cfg = self.context.get('config')
        tmpl = cfg['templates']['digital_bodleian_permalink_tmpl']

        ident: str = get_identifier(req, obj.get('id'), tmpl)

        return {
            "@id": ident,
            "label": "View on Digital Bodleian",
            "format": "text/html"
        }
예제 #26
0
    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)

        return [{"id": wid, "type": "Manifest"}]
예제 #27
0
    def get_prev(
            self, obj: Dict
    ) -> Optional[Dict]:  # pylint: disable-msg=unused-argument
        req = self.context.get('request')
        cfg = self.context.get('config')

        page_id: int = self.context.get("page_id")
        prev_page: int = page_id - 1

        # If we're on the first page, don't show the 'prev' key
        if prev_page < 0:
            return None

        page_tmpl: str = cfg['templates']['activitystream_id_tmpl']
        prev_page_id: str = get_identifier(req, f"page-{prev_page}", page_tmpl)

        return {"id": prev_page_id, "type": "OrderedCollectionPage"}
예제 #28
0
    def get_metadata(self, obj: SolrResult) -> Optional[List[Dict]]:
        # Talbot manifests are a bit different, so exclude their metadata
        if 'talbot' in obj.get('all_collections_id_sm', []):  # type: ignore
            return None

        req = self.context.get('request')
        cfg = self.context.get('config')
        tmpl: str = cfg['templates']['digital_bodleian_permalink_tmpl']
        ident: str = get_identifier(req, obj.get('id'), tmpl)
        val: str = '<span><a href="{0}">View on Digital Bodleian</a></span>'.format(
            ident)

        metadata: List = [{"label": "Homepage", "value": val}]

        metadata += get_links(obj, 2)
        metadata += v2_metadata_block(obj)

        return metadata
예제 #29
0
    def get_next(
            self, obj: Dict
    ) -> Optional[Dict]:  # pylint: disable-msg=unused-argument
        req = self.context.get('request')
        cfg = self.context.get('config')

        hits: int = obj.get('results').hits
        pagesize: int = int(cfg['solr']['pagesize'])

        next_page = self.context.get("page_id") + 1
        last_page = math.floor(hits / pagesize)

        # If we're on the last page, don't show the next key
        if next_page > last_page:
            return None

        page_tmpl: str = cfg['templates']['activitystream_id_tmpl']
        next_page_id: str = get_identifier(req, f"page-{next_page}", page_tmpl)

        return {"id": next_page_id, "type": "OrderedCollectionPage"}
예제 #30
0
    def get_canvases(self, obj: SolrResult) -> Optional[List]:
        hierarchy = self.context.get('hierarchy')
        wk_id = obj.get("work_id")

        # If the work id is in the hierarchy, this contains
        # a list of ranges; return None.
        if wk_id in hierarchy:
            return None

        req = self.context.get('request')
        cfg = self.context.get('config')
        surfaces: List = obj.get('surfaces_sm')
        canvas_tmpl: str = cfg['templates']['canvas_id_tmpl']

        ret: List = []
        for s in surfaces:
            ret.append(
                get_identifier(req, re.sub(SURFACE_ID_SUB, "", s),
                               canvas_tmpl))

        return ret