コード例 #1
0
ファイル: iiif_utils.py プロジェクト: Princeton-CDH/geniza
def empty_iiif_canvas():
    canvas = new_iiif_canvas()
    canvas.id = absolutize_url(EMPTY_CANVAS_ID)
    # set sizes (these are arbitrary)
    canvas.width = 3200
    canvas.height = 4000
    canvas.label = "image unavailable"
    return canvas
コード例 #2
0
 def test_get_absolute_url(self, document):
     """should return doc permalink"""
     doc_detail_view = DocumentDetailView()
     doc_detail_view.object = document
     doc_detail_view.kwargs = {"pk": document.pk}
     assert doc_detail_view.get_absolute_url() == absolutize_url(
         document.get_absolute_url()
     )
コード例 #3
0
 def test_get_absolute_url(self, document, source):
     """should return scholarship permalink"""
     Footnote.objects.create(content_object=document, source=source)
     doc_detail_view = DocumentScholarshipView()
     doc_detail_view.object = document
     doc_detail_view.kwargs = {"pk": document.pk}
     assert doc_detail_view.get_absolute_url() == absolutize_url(
         f"{document.get_absolute_url()}scholarship/"
     )
コード例 #4
0
    def test_get_absolute_url(
        self, mock_view_iiifpres, mock_model_iiifpres, document, source
    ):
        """should return manifest permalink"""

        view = DocumentManifestView()
        view.object = document
        view.kwargs = {"pk": document.pk}
        assert view.get_absolute_url() == absolutize_url(
            f"{document.get_absolute_url()}iiif/manifest/"
        )
コード例 #5
0
 def test_past_id_mixin(self, db, client, source):
     """should redirect from 404 to new pgpid when an old_pgpid is matched"""
     response_404 = client.get(reverse("corpus:document-scholarship", args=[2]))
     assert response_404.status_code == 404
     doc = Document.objects.create(id=1, old_pgpids=[2])
     Footnote.objects.create(content_object=doc, source=source)
     response_301 = client.get(reverse("corpus:document-scholarship", args=[2]))
     assert response_301.status_code == 301
     assert response_301.url == absolutize_url(
         f"{doc.get_absolute_url()}scholarship/"
     )
コード例 #6
0
    def test_past_id_mixin(self, db, client):
        """should redirect from 404 to new pgpid when an old_pgpid is matched"""
        response_404 = client.get(reverse("corpus:document", args=(2,)))
        assert response_404.status_code == 404
        doc = Document.objects.create(id=1, old_pgpids=[2])
        response_301 = client.get(reverse("corpus:document", args=(2,)))
        assert response_301.status_code == 301
        assert response_301.url == absolutize_url(doc.get_absolute_url())

        # Test when pgpid not first in the list
        response_404_notfirst = client.get(reverse("corpus:document", args=(71,)))
        assert response_404_notfirst.status_code == 404
        doc.old_pgpids = [5, 6, 71]
        doc.save()
        response_301_notfirst = client.get(reverse("corpus:document", args=(71,)))
        assert response_301_notfirst.status_code == 301
        assert response_301_notfirst.url == absolutize_url(doc.get_absolute_url())

        # Test partial matching pgpid
        response_404_partialmatch = client.get(reverse("corpus:document", args=(7,)))
        assert response_404_partialmatch.status_code == 404
コード例 #7
0
    def get(self, request, *args, **kwargs):
        document = self.get_object()
        # should 404 if no images or no transcription
        if not document.has_transcription() and not document.has_image():
            raise Http404

        iiif_urls = document.iiif_urls()
        local_manifest_id = self.get_absolute_url()
        first_canvas = None

        manifest = iiif_utils.new_iiif_manifest()
        manifest.id = local_manifest_id
        manifest.label = document.title
        # we probably want other metadata as well...
        manifest.metadata = [{"label": "PGP ID", "value": str(document.id)}]
        manifest.description = document.description

        canvases = []
        # keep track of unique attributions so we can include them all
        attributions = set()
        for url in iiif_urls:
            remote_manifest = IIIFPresentation.from_url(url)
            # CUDL attribution has some variation in tags;
            # would be nice to preserve tagged version,
            # for now, ignore tags so we can easily de-dupe
            try:
                attributions.add(strip_tags(remote_manifest.attribution))
            except AttributeError:
                # attribution is optional, so ignore if not present
                pass
            for canvas in remote_manifest.sequences[0].canvases:
                # do we want local canvas id, or rely on remote id?
                local_canvas = dict(canvas)
                if first_canvas is None:
                    first_canvas = local_canvas
                # TODO: can we build this from djiffy canvas?

                # adding provenance per recommendation from folks on IIIf Slack
                # to track original source of this canvas
                local_canvas["partOf"] = [{
                    "@id": str(remote_manifest.id),
                    "@type": "sc:Manifest",
                    "label": {
                        "en": ["original source: %s" % remote_manifest.label]
                    },
                }]

                # NOTE: would be nice to attach the annotation list to every canvas,
                # so transcription will be available with any page,
                # but canvas id needs to match annotation target
                canvases.append(local_canvas)

        manifest.sequences = [{"@type": "sc:Sequence", "canvases": canvases}]
        # TODO: add a PGP logo here? combine logos?
        # NOTE: multiple logos do not seem to work with iiif presentation 2.0;
        # (or at least, do not display in Mirador)
        # in 3.0 we can use multiple provider blocks, but no viewer supports it yet

        manifest.attribution = corpus_extras.format_attribution(
            document.attribution())

        # if transcription is available, add an annotation list to first canvas
        if document.has_transcription():
            other_content = {
                "@context":
                "http://iiif.io/api/presentation/2/context.json",
                "@id":
                absolutize_url(
                    reverse("corpus:document-annotations", args=[document.pk]),
                    request=
                    request,  # request is needed to avoid getting https:// urls in dev
                ),
                "@type":
                "sc:AnnotationList",
            }
            # if there are no images available, use an empty canvas
            if not first_canvas:
                first_canvas = iiif_utils.empty_iiif_canvas()
                canvases.append(first_canvas)

            # attach annotation list
            first_canvas["otherContent"] = other_content

        return JsonResponse(dict(manifest), encoder=iiif_utils.AttrDictEncoder)
コード例 #8
0
 def get_absolute_url(self):
     """Get the permalink to this page."""
     return absolutize_url(reverse(self.viewname, args=[self.kwargs["pk"]]))
コード例 #9
0
ファイル: models.py プロジェクト: Princeton-CDH/geniza
 def permalink(self):
     # generate permalink without language url so that all versions
     # have the same link and users will be directed preferred language
     # - get current active language, or default langue if not active
     lang = get_language() or settings.LANGUAGE_CODE
     return absolutize_url(self.get_absolute_url().replace(f"/{lang}/", "/"))
コード例 #10
0
 def test_permalink(self, document, client):
     """should contain permalink generated from absolutize_url"""
     response = client.get(reverse("corpus:document", args=(document.id,)))
     permalink = absolutize_url(document.get_absolute_url()).replace("/en/", "/")
     assertContains(response, f'<link rel="canonical" href="{permalink}"')