Skip to content

Commit

Permalink
Fix 404, redirect old urls to prefix /registre_ads
Browse files Browse the repository at this point in the history
  • Loading branch information
brmzkw committed Jan 12, 2024
1 parent 78ce756 commit 0a5a9fb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
13 changes: 0 additions & 13 deletions mesads/app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,6 @@
staff_member_required(views.DashboardsDetailView.as_view()),
name="app.dashboards.detail",
),
# When the user makes a request to become an ADSManager, we send an email to
# the administrator. This email used to contain a link to /admin_gestion.
# For backward compatibility, we redirect the user to the new URL
# /registre_ads/admin_gestion.
path(
"admin_gestion",
RedirectView.as_view(url="/registre_ads/admin_gestion", permanent=True),
),
# Backward compatibility
path(
"gestion",
RedirectView.as_view(url="/registre_ads/gestion", permanent=True),
),
path(
"registre_ads/admin_gestion",
ads_manager_administrator_required(views.ADSManagerAdminView.as_view()),
Expand Down
25 changes: 25 additions & 0 deletions mesads/middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from django.shortcuts import redirect
from django.urls import resolve
from django.urls.exceptions import Resolver404


class BackwardCompatibilityURLMiddleware:
"""In the past, all the URLs of the app were in the root of the website and
only the ADS register existed. Now, the ADS register is in /registre_ads and
the other URLs are in /other subpaths. This middleware redirects the old
URLs to the new ones for backward compatibility.
"""

def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
response = self.get_response(request)
if response.status_code == 404:
url = f"/registre_ads{request.path}"
try:
resolve(url)
except Resolver404:
return response
return redirect(url, permanent=True)
return response
1 change: 1 addition & 0 deletions mesads/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def parse_env_bool(key, default):
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"mesads.middleware.BackwardCompatibilityURLMiddleware",
]

ROOT_URLCONF = "mesads.urls"
Expand Down

0 comments on commit 0a5a9fb

Please sign in to comment.