Skip to content

Commit

Permalink
Merge pull request #1333 from maykinmedia/tasks/2648-legacy-case-deta…
Browse files Browse the repository at this point in the history
…il-url-redirect

[#2648] handle legacy case detail URLs with redirect
  • Loading branch information
alextreme authored Aug 6, 2024
2 parents 4f5f7d0 + d8e6694 commit 81fa5d6
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/open_inwoner/cms/cases/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
CaseDocumentUploadFormView,
InnerCaseDetailView,
InnerCaseListView,
LegacyCaseDetailHandler,
OuterCaseDetailView,
OuterCaseListView,
)
Expand Down Expand Up @@ -61,4 +62,14 @@
),
path("content/", InnerCaseListView.as_view(), name="cases_content"),
path("", OuterCaseListView.as_view(), name="index"),
# Legacy redirects for hard-coded case detail urls lacking a ZGW api group reference
# in the url (e.g. from old notification mails). This redirects those URLs
# to the new case detail URL with a reference to the only ZGW api group (if 1),
# or the case list page with an expiration notice (because we can't be sure which
# api group to use).
path(
"<str:object_id>/status/",
LegacyCaseDetailHandler.as_view(),
name="legacy_case_detail",
),
]
2 changes: 2 additions & 0 deletions src/open_inwoner/cms/cases/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
CaseDocumentDownloadView,
CaseDocumentUploadFormView,
InnerCaseDetailView,
LegacyCaseDetailHandler,
OuterCaseDetailView,
)

Expand All @@ -14,5 +15,6 @@
"CaseDocumentDownloadView",
"CaseDocumentUploadFormView",
"InnerCaseDetailView",
"LegacyCaseDetailHandler",
"OuterCaseDetailView",
]
51 changes: 50 additions & 1 deletion src/open_inwoner/cms/cases/views/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
from django.conf import settings
from django.contrib import messages
from django.core.exceptions import ObjectDoesNotExist, PermissionDenied
from django.http import Http404, StreamingHttpResponse
from django.http import (
Http404,
HttpRequest,
HttpResponseRedirect,
StreamingHttpResponse,
)
from django.urls import reverse
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -1045,3 +1050,47 @@ def get_context_data(self, **kwargs):
"cases:case_detail_contact_form", kwargs=self.kwargs
)
return context


class LegacyCaseDetailHandler(View):
"""Redirect the legacy case detail to the current version with ZGW API group ref."""

def get(
self,
request: HttpRequest,
object_id: str,
):
redirect_url = None
match ZGWApiGroupConfig.objects.count():
case 1:
target_api_group = ZGWApiGroupConfig.objects.get()
redirect_url = reverse(
"cases:case_detail",
kwargs={
"api_group_id": target_api_group.id,
"object_id": object_id,
},
)
case count if count > 1:
messages.add_message(
request,
messages.ERROR,
_(
"The link you clicked on has expired. Please find your case in the"
" list below."
),
)
logger.warning(
"Could not automatically handle legacy case detail URL due to multiple"
" ZGWApiGroupConfig objects"
)
redirect_url = reverse("cases:index")
case 0:
# This is an invariant violation: there should always be at least
# one ZGWApiGroupConfig.
logger.error(
"Legacy redirect invoked without any configured API groups"
)
raise Http404

return HttpResponseRedirect(redirect_url)
65 changes: 65 additions & 0 deletions src/open_inwoner/openzaak/tests/test_case_detail_redirects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from django.contrib.messages import get_messages
from django.test import TestCase, override_settings
from django.urls import reverse

from open_inwoner.openzaak.tests.factories import ZGWApiGroupConfigFactory


@override_settings(
ROOT_URLCONF="open_inwoner.cms.tests.urls",
)
class LegacyCaseDetailUrlRedirectTest(TestCase):
def test_legacy_url_redirects_to_only_api_group_prefix(self):
sole_api_group = ZGWApiGroupConfigFactory()

object_id = "test_object_id"
legacy_handler_url = reverse(
"cases:legacy_case_detail",
kwargs={"object_id": object_id},
)
target_url = reverse(
"cases:case_detail",
kwargs={"api_group_id": sole_api_group.id, "object_id": object_id},
)

response = self.client.get(legacy_handler_url)
self.assertEqual(response.status_code, 302)
self.assertEqual(response["Location"], target_url)

def test_legacy_url_redirects_to_case_list_when_multiple_api_groups(self):
ZGWApiGroupConfigFactory(), ZGWApiGroupConfigFactory()

object_id = "test_object_id"
legacy_handler_url = reverse(
"cases:legacy_case_detail",
kwargs={"object_id": object_id},
)
target_url = reverse("cases:index")

response = self.client.get(legacy_handler_url)
messages = [str(m) for m in get_messages(response.wsgi_request)]

self.assertEqual(response.status_code, 302)
self.assertEqual(response["Location"], target_url)
self.assertEqual(
messages,
[
"The link you clicked on has expired. Please find your case in the"
" list below."
],
)

def test_legacy_url_redirect_returns_404_on_missing_api_groups(self):
object_id = "test_object_id"
legacy_handler_url = reverse(
"cases:legacy_case_detail",
kwargs={"object_id": object_id},
)

legacy_handler_url = reverse(
"cases:legacy_case_detail",
kwargs={"object_id": object_id},
)

response = self.client.get(legacy_handler_url)
self.assertEqual(response.status_code, 404)

0 comments on commit 81fa5d6

Please sign in to comment.