From 1889021db20532b284eae8e21d425601b18a1648 Mon Sep 17 00:00:00 2001 From: Lucas Date: Thu, 8 Aug 2024 14:46:27 +0000 Subject: [PATCH] fix(redirects): add permanent=True to redirects - fix corresponding tests from status code 302 to 301 --- .../main_app/tests/test_views.py | 14 +++++++------- .../main_app/views/redirect.py | 18 +++++++++--------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/django/cantusdb_project/main_app/tests/test_views.py b/django/cantusdb_project/main_app/tests/test_views.py index 4f8cf94a8..6f8d5eda3 100644 --- a/django/cantusdb_project/main_app/tests/test_views.py +++ b/django/cantusdb_project/main_app/tests/test_views.py @@ -5898,7 +5898,7 @@ def test_chant_redirect(self): ) expected_url = reverse("chant-detail", args=[example_chant_id]) - self.assertEqual(response_1.status_code, 302) + self.assertEqual(response_1.status_code, 301) self.assertEqual(response_1.url, expected_url) def test_source_redirect(self): @@ -5914,7 +5914,7 @@ def test_source_redirect(self): ) expected_url = reverse("source-detail", args=[example_source_id]) - self.assertEqual(response_1.status_code, 302) + self.assertEqual(response_1.status_code, 301) self.assertEqual(response_1.url, expected_url) def test_sequence_redirect(self): @@ -5929,7 +5929,7 @@ def test_sequence_redirect(self): ) expected_url = reverse("sequence-detail", args=[example_sequence_id]) - self.assertEqual(response_1.status_code, 302) + self.assertEqual(response_1.status_code, 301) self.assertEqual(response_1.url, expected_url) def test_article_redirect(self): @@ -5945,7 +5945,7 @@ def test_article_redirect(self): ) expected_url = reverse("article-detail", args=[example_article_id]) - self.assertEqual(response_1.status_code, 302) + self.assertEqual(response_1.status_code, 301) self.assertEqual(response_1.url, expected_url) def test_indexer_redirect(self): @@ -5962,7 +5962,7 @@ def test_indexer_redirect(self): ) expected_url = reverse("user-detail", args=[example_matching_user_id]) - self.assertEqual(response_1.status_code, 302) + self.assertEqual(response_1.status_code, 301) self.assertEqual(response_1.url, expected_url) def test_bad_redirect(self): @@ -6005,7 +6005,7 @@ def test_indexer_redirect_good(self): ) expected_url = reverse("user-detail", args=[example_matching_user_id]) - self.assertEqual(response_1.status_code, 302) + self.assertEqual(response_1.status_code, 301) self.assertEqual(response_1.url, expected_url) def test_indexer_redirect_bad(self): @@ -6033,7 +6033,7 @@ def test_document_redirects(self): for path in old_document_paths: # each path should redirect to the new path response = self.client.get(path) - self.assertEqual(response.status_code, 302) + self.assertEqual(response.status_code, 301) # In Aug 2023, Jacob struggled to get the following lines to work - # I was getting 404s when I expected 200s. This final step would be nice # to test properly - if a future developer who is cleverer than me can diff --git a/django/cantusdb_project/main_app/views/redirect.py b/django/cantusdb_project/main_app/views/redirect.py index f144556d2..6a6cd0b4c 100644 --- a/django/cantusdb_project/main_app/views/redirect.py +++ b/django/cantusdb_project/main_app/views/redirect.py @@ -16,7 +16,7 @@ def csv_export_redirect_from_old_path(request, source_id): - return redirect(reverse("csv-export", args=[source_id])) + return redirect(reverse("csv-export", args=[source_id]), permanent=True) def redirect_node_url(request, pk: int) -> HttpResponse: @@ -37,12 +37,12 @@ def redirect_node_url(request, pk: int) -> HttpResponse: user_id = get_user_id_from_old_indexer_id(pk) if get_user_id_from_old_indexer_id(pk) is not None: - return redirect("user-detail", user_id) + return redirect("user-detail", user_id, permanent=True) for rec_type, view in NODE_TYPES_AND_VIEWS: if record_exists(rec_type, pk): # if an object is found, a redirect() call to the appropriate view is returned - return redirect(view, pk) + return redirect(view, pk, permanent=True) # if it reaches the end of the types with finding an existing object, a 404 will be returned raise Http404("No record found matching the /node/ query.") @@ -60,7 +60,7 @@ def redirect_indexer(request, pk: int) -> HttpResponse: """ user_id = get_user_id_from_old_indexer_id(pk) if get_user_id_from_old_indexer_id(pk) is not None: - return redirect("user-detail", user_id) + return redirect("user-detail", user_id, permanent=True) raise Http404("No indexer found matching the query.") @@ -75,7 +75,7 @@ def redirect_office(request) -> HttpResponse: Returns: HttpResponse """ - return redirect("service-list") + return redirect("service-list", permanent=True) def redirect_offices(request) -> HttpResponse: @@ -88,7 +88,7 @@ def redirect_offices(request) -> HttpResponse: Returns: HttpResponse """ - return redirect("service-list") + return redirect("service-list", permanent=True) def redirect_office_id(request, pk: int) -> HttpResponse: @@ -102,7 +102,7 @@ def redirect_office_id(request, pk: int) -> HttpResponse: Returns: HttpResponse """ - return redirect(reverse("service-detail", args=[pk])) + return redirect(reverse("service-detail", args=[pk]), permanent=True) def redirect_genre(request) -> HttpResponse: @@ -115,7 +115,7 @@ def redirect_genre(request) -> HttpResponse: Returns: HttpResponse """ - return redirect("genre-list") + return redirect("genre-list", permanent=True) def redirect_search(request: HttpRequest) -> HttpResponse: @@ -170,7 +170,7 @@ def redirect_documents(request) -> HttpResponse: new_path = mapping[old_path] except KeyError as exc: raise Http404 from exc - return redirect(new_path) + return redirect(new_path, permanent=True) def redirect_chants(request) -> HttpResponse: