diff --git a/refarm_redirects/__init__.py b/refarm_redirects/__init__.py new file mode 100644 index 0000000..9311f41 --- /dev/null +++ b/refarm_redirects/__init__.py @@ -0,0 +1 @@ +default_app_config = 'refarm_redirects.apps.RedirectsConfig' diff --git a/refarm_redirects/apps.py b/refarm_redirects/apps.py new file mode 100644 index 0000000..352af75 --- /dev/null +++ b/refarm_redirects/apps.py @@ -0,0 +1,7 @@ +from django.apps import AppConfig +from django.utils.translation import ugettext_lazy as _ + + +class RedirectsConfig(AppConfig): + name = 'refarm_redirects' + verbose_name = _('refarm_redirects') diff --git a/refarm_redirects/middleware.py b/refarm_redirects/middleware.py new file mode 100644 index 0000000..17b14c3 --- /dev/null +++ b/refarm_redirects/middleware.py @@ -0,0 +1,36 @@ +from django.conf import settings +from django.contrib.redirects.models import Redirect +from django.contrib.redirects.middleware \ + import RedirectFallbackMiddleware \ + as DjangoRedirectFallbackMiddleware +from django.contrib.sites.shortcuts import get_current_site + + +class RedirectAllMiddleware(DjangoRedirectFallbackMiddleware): + # reloaded this method + # just to drop `response.status_code` status check + # in base class + def process_response(self, request, response): + full_path = request.get_full_path() + current_site = get_current_site(request) + + r = None + try: + r = Redirect.objects.get(site=current_site, old_path=full_path) + except Redirect.DoesNotExist: + pass + if r is None and settings.APPEND_SLASH and not request.path.endswith('/'): + try: + r = Redirect.objects.get( + site=current_site, + old_path=request.get_full_path(force_append_slash=True), + ) + except Redirect.DoesNotExist: + pass + if r is not None: + if r.new_path == '': + return self.response_gone_class() + return self.response_redirect_class(r.new_path) + + # No redirect was found. Return the response. + return response diff --git a/tests/refarm_redirects/__init__.py b/tests/refarm_redirects/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/refarm_redirects/test_views.py b/tests/refarm_redirects/test_views.py new file mode 100644 index 0000000..784f633 --- /dev/null +++ b/tests/refarm_redirects/test_views.py @@ -0,0 +1,2 @@ +# @todo:30m Move test case to custom redirects app +# You can find example at SE's tests_views.Redirects app diff --git a/tests/test_settings.py b/tests/test_settings.py index 7ff04cd..842af2d 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -59,8 +59,13 @@ 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', + 'refarm_redirects.middleware.RedirectAllMiddleware', ) +# @todo #140:60m Use `refarm-site's` packages with it's prefix. +# For example do `INSTALLED_APPS = ['refarm-site.pages', ...]` +# instead of just `['pages']`. + INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', @@ -76,6 +81,7 @@ 'ecommerce', 'pages', 'search', + 'refarm_redirects', 'generic_admin', 'tests', )