Loading src/jama/tests.py +32 −1 Original line number Diff line number Diff line Loading @@ -26,7 +26,11 @@ from jama.admin_services import tasks as task_services from jama import settings as jama_settings from annotations.models import Annotation from jama.iiif import serialize_jama_collection from jama.views import ark_resource_or_collection_iiif_manifest from jama.views import ( ark_resource_iiif_manifest_thumbnail, ark_resource_or_collection_iiif_manifest, ark_resource_public_download, ) from resources import acl from resources.models import ( APIKey, Loading @@ -35,6 +39,7 @@ from resources.models import ( File, FileType, Project, Resource, Role, ObjectPermission, ) Loading Loading @@ -913,3 +918,29 @@ class IiifSerializationTestCase(TestCase): request, "12345", "annotated-collection" ) self.assertNotContains(response, "Sensitive") def test_ark_file_views_return_404_for_plain_resource(self): Resource.objects.create( title="plain resource", ptr_project=self.project, ark="12345/plain-resource", ) request = self.factory.get("/ark/12345/plain-resource/") view_calls = ( ( ark_resource_or_collection_iiif_manifest, (request, "12345", "plain-resource"), ), ( ark_resource_iiif_manifest_thumbnail, (request, "12345", "plain-resource", 300), ), ( ark_resource_public_download, (request, "12345", "plain-resource"), ), ) for view, arguments in view_calls: with self.subTest(view=view.__name__), self.assertRaises(Http404): view(*arguments) src/jama/views.py +10 −10 Original line number Diff line number Diff line Loading @@ -2,7 +2,7 @@ from django.http import HttpResponse, HttpRequest from django.shortcuts import render, get_object_or_404, redirect, Http404 from jama import settings from django.contrib.auth.models import User from resources.models import Resource, Collection, Project from resources.models import Resource, Collection, File, Project from urllib.parse import urlparse, parse_qs, urlencode, urlunparse from .iiif import serialize_jama_collection Loading Loading @@ -82,13 +82,13 @@ def ark_resource_or_collection_iiif_manifest( request: HttpRequest, naan: str, assigned_name: str ) -> HttpResponse: ark_name = f"{naan}/{assigned_name}" resource = Resource.objects.filter( file_instance = File.objects.filter( deleted_at__isnull=True, ark=ark_name, ptr_project__restore_state=Project.RESTORE_STATE_READY, ).first() if resource and resource.file and resource.file.iiif_infos_url(): return redirect(resource.file.iiif_infos_url()) if file_instance and file_instance.should_have_iiif: return redirect(file_instance.iiif_infos_url()) collection = Collection.objects.filter( deleted_at__isnull=True, ark=ark_name, Loading @@ -104,13 +104,13 @@ def ark_resource_iiif_manifest_thumbnail( request: HttpRequest, naan: str, assigned_name: str, size: int ) -> HttpResponse: ark_name = f"{naan}/{assigned_name}" resource = Resource.objects.filter( file_instance = File.objects.filter( deleted_at__isnull=True, ark=ark_name, ptr_project__restore_state=Project.RESTORE_STATE_READY, ).first() if resource and resource.file and resource.file.should_have_iiif: return redirect(resource.file.iiif_thumbnail_url(size)) if file_instance and file_instance.should_have_iiif: return redirect(file_instance.iiif_thumbnail_url(size)) raise Http404("no such resource") Loading Loading @@ -173,11 +173,11 @@ def ark_resource_public_download( request: HttpRequest, naan: str, assigned_name: str ) -> HttpResponse: ark_name = f"{naan}/{assigned_name}" resource = Resource.objects.filter( file_instance = File.objects.filter( deleted_at__isnull=True, ark=ark_name, ptr_project__restore_state=Project.RESTORE_STATE_READY, ).first() if resource and resource.file: return redirect("serve_file_public", resource.file.hash) if file_instance: return redirect("serve_file_public", file_instance.hash) raise Http404("no such resource") Loading
src/jama/tests.py +32 −1 Original line number Diff line number Diff line Loading @@ -26,7 +26,11 @@ from jama.admin_services import tasks as task_services from jama import settings as jama_settings from annotations.models import Annotation from jama.iiif import serialize_jama_collection from jama.views import ark_resource_or_collection_iiif_manifest from jama.views import ( ark_resource_iiif_manifest_thumbnail, ark_resource_or_collection_iiif_manifest, ark_resource_public_download, ) from resources import acl from resources.models import ( APIKey, Loading @@ -35,6 +39,7 @@ from resources.models import ( File, FileType, Project, Resource, Role, ObjectPermission, ) Loading Loading @@ -913,3 +918,29 @@ class IiifSerializationTestCase(TestCase): request, "12345", "annotated-collection" ) self.assertNotContains(response, "Sensitive") def test_ark_file_views_return_404_for_plain_resource(self): Resource.objects.create( title="plain resource", ptr_project=self.project, ark="12345/plain-resource", ) request = self.factory.get("/ark/12345/plain-resource/") view_calls = ( ( ark_resource_or_collection_iiif_manifest, (request, "12345", "plain-resource"), ), ( ark_resource_iiif_manifest_thumbnail, (request, "12345", "plain-resource", 300), ), ( ark_resource_public_download, (request, "12345", "plain-resource"), ), ) for view, arguments in view_calls: with self.subTest(view=view.__name__), self.assertRaises(Http404): view(*arguments)
src/jama/views.py +10 −10 Original line number Diff line number Diff line Loading @@ -2,7 +2,7 @@ from django.http import HttpResponse, HttpRequest from django.shortcuts import render, get_object_or_404, redirect, Http404 from jama import settings from django.contrib.auth.models import User from resources.models import Resource, Collection, Project from resources.models import Resource, Collection, File, Project from urllib.parse import urlparse, parse_qs, urlencode, urlunparse from .iiif import serialize_jama_collection Loading Loading @@ -82,13 +82,13 @@ def ark_resource_or_collection_iiif_manifest( request: HttpRequest, naan: str, assigned_name: str ) -> HttpResponse: ark_name = f"{naan}/{assigned_name}" resource = Resource.objects.filter( file_instance = File.objects.filter( deleted_at__isnull=True, ark=ark_name, ptr_project__restore_state=Project.RESTORE_STATE_READY, ).first() if resource and resource.file and resource.file.iiif_infos_url(): return redirect(resource.file.iiif_infos_url()) if file_instance and file_instance.should_have_iiif: return redirect(file_instance.iiif_infos_url()) collection = Collection.objects.filter( deleted_at__isnull=True, ark=ark_name, Loading @@ -104,13 +104,13 @@ def ark_resource_iiif_manifest_thumbnail( request: HttpRequest, naan: str, assigned_name: str, size: int ) -> HttpResponse: ark_name = f"{naan}/{assigned_name}" resource = Resource.objects.filter( file_instance = File.objects.filter( deleted_at__isnull=True, ark=ark_name, ptr_project__restore_state=Project.RESTORE_STATE_READY, ).first() if resource and resource.file and resource.file.should_have_iiif: return redirect(resource.file.iiif_thumbnail_url(size)) if file_instance and file_instance.should_have_iiif: return redirect(file_instance.iiif_thumbnail_url(size)) raise Http404("no such resource") Loading Loading @@ -173,11 +173,11 @@ def ark_resource_public_download( request: HttpRequest, naan: str, assigned_name: str ) -> HttpResponse: ark_name = f"{naan}/{assigned_name}" resource = Resource.objects.filter( file_instance = File.objects.filter( deleted_at__isnull=True, ark=ark_name, ptr_project__restore_state=Project.RESTORE_STATE_READY, ).first() if resource and resource.file: return redirect("serve_file_public", resource.file.hash) if file_instance: return redirect("serve_file_public", file_instance.hash) raise Http404("no such resource")