Skip to content

Commit

Permalink
Redirect to ManuscriptListView when manuscript is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
dchiller committed Nov 20, 2023
1 parent 1933cba commit c56ee21
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion app/public/cantusdata/views/manuscript.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from rest_framework import generics
from django.views import generic
from cantusdata.models.manuscript import Manuscript
from cantusdata.serializers.manuscript import (
ManuscriptSerializer,
ManuscriptListSerializer,
)
from cantusdata.renderers import templated_view_renderers
from django.http import Http404, HttpResponseRedirect
from rest_framework.response import Response
from django.urls import reverse


class ManuscriptList(generics.ListAPIView):
Expand All @@ -22,3 +24,19 @@ class ManuscriptDetail(generics.RetrieveAPIView):
serializer_class = ManuscriptSerializer
template_name = "require.html"
renderer_classes = templated_view_renderers

def retrieve(self, request, *args, **kwargs):
"""
Overrides the default retrieval method
to return a redirect to the manuscript list
page if the manuscript is not found. Implemented
to prevent 404 errors from links to specific manuscripts
made before manuscript ids were stable.
"""

try:
instance = self.get_object()
serializer = self.get_serializer(instance)
return Response(serializer.data)
except Http404:
return HttpResponseRedirect(reverse("manuscript-list"))

0 comments on commit c56ee21

Please sign in to comment.