Skip to content

Commit

Permalink
fix(redirects): add permanent=True to redirects
Browse files Browse the repository at this point in the history
- fix corresponding tests from status code 302 to 301
  • Loading branch information
lucasmarchd01 committed Aug 8, 2024
1 parent 6d62fe9 commit 1889021
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
14 changes: 7 additions & 7 deletions django/cantusdb_project/main_app/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions django/cantusdb_project/main_app/views/redirect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.")
Expand All @@ -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.")

Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 1889021

Please sign in to comment.