diff options
Diffstat (limited to 'webapp/django/contrib/redirects')
-rw-r--r-- | webapp/django/contrib/redirects/README.TXT | 8 | ||||
-rw-r--r-- | webapp/django/contrib/redirects/__init__.py | 0 | ||||
-rw-r--r-- | webapp/django/contrib/redirects/admin.py | 11 | ||||
-rw-r--r-- | webapp/django/contrib/redirects/middleware.py | 27 | ||||
-rw-r--r-- | webapp/django/contrib/redirects/models.py | 20 |
5 files changed, 66 insertions, 0 deletions
diff --git a/webapp/django/contrib/redirects/README.TXT b/webapp/django/contrib/redirects/README.TXT new file mode 100644 index 0000000000..3544904a40 --- /dev/null +++ b/webapp/django/contrib/redirects/README.TXT @@ -0,0 +1,8 @@ +This is an optional add-on app, redirects. + +For full documentation, see either of these: + + * The file django/docs/redirects.txt in the Django distribution + * http://www.djangoproject.com/documentation/redirects/ on the Web + +Both have identical content.
\ No newline at end of file diff --git a/webapp/django/contrib/redirects/__init__.py b/webapp/django/contrib/redirects/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/webapp/django/contrib/redirects/__init__.py diff --git a/webapp/django/contrib/redirects/admin.py b/webapp/django/contrib/redirects/admin.py new file mode 100644 index 0000000000..a9b2a3228f --- /dev/null +++ b/webapp/django/contrib/redirects/admin.py @@ -0,0 +1,11 @@ + +from django.contrib import admin +from django.contrib.redirects.models import Redirect + +class RedirectAdmin(admin.ModelAdmin): + list_display = ('old_path', 'new_path') + list_filter = ('site',) + search_fields = ('old_path', 'new_path') + radio_fields = {'site': admin.VERTICAL} + +admin.site.register(Redirect, RedirectAdmin)
\ No newline at end of file diff --git a/webapp/django/contrib/redirects/middleware.py b/webapp/django/contrib/redirects/middleware.py new file mode 100644 index 0000000000..8998c2ce3e --- /dev/null +++ b/webapp/django/contrib/redirects/middleware.py @@ -0,0 +1,27 @@ +from django.contrib.redirects.models import Redirect +from django import http +from django.conf import settings + +class RedirectFallbackMiddleware(object): + def process_response(self, request, response): + if response.status_code != 404: + return response # No need to check for a redirect for non-404 responses. + path = request.get_full_path() + try: + r = Redirect.objects.get(site__id__exact=settings.SITE_ID, old_path=path) + except Redirect.DoesNotExist: + r = None + if r is None and settings.APPEND_SLASH: + # Try removing the trailing slash. + try: + r = Redirect.objects.get(site__id__exact=settings.SITE_ID, + old_path=path[:path.rfind('/')]+path[path.rfind('/')+1:]) + except Redirect.DoesNotExist: + pass + if r is not None: + if r.new_path == '': + return http.HttpResponseGone() + return http.HttpResponsePermanentRedirect(r.new_path) + + # No redirect was found. Return the response. + return response diff --git a/webapp/django/contrib/redirects/models.py b/webapp/django/contrib/redirects/models.py new file mode 100644 index 0000000000..4233d55793 --- /dev/null +++ b/webapp/django/contrib/redirects/models.py @@ -0,0 +1,20 @@ +from django.db import models +from django.contrib.sites.models import Site +from django.utils.translation import ugettext_lazy as _ + +class Redirect(models.Model): + site = models.ForeignKey(Site) + old_path = models.CharField(_('redirect from'), max_length=200, db_index=True, + help_text=_("This should be an absolute path, excluding the domain name. Example: '/events/search/'.")) + new_path = models.CharField(_('redirect to'), max_length=200, blank=True, + help_text=_("This can be either an absolute path (as above) or a full URL starting with 'http://'.")) + + class Meta: + verbose_name = _('redirect') + verbose_name_plural = _('redirects') + db_table = 'django_redirect' + unique_together=(('site', 'old_path'),) + ordering = ('old_path',) + + def __unicode__(self): + return "%s ---> %s" % (self.old_path, self.new_path) |