-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix 404, redirect old urls to prefix /registre_ads
- Loading branch information
Showing
3 changed files
with
26 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters