Skip to content

Commit

Permalink
Add a deprecated_redirect interstitial view.
Browse files Browse the repository at this point in the history
sumo.views.deprecated_redirect shows a message informing the user that the
URL has changed and they should update their bookmarks. It takes one required
argument, `url` which is passed directly to `reverse()`. Any additional kwargs
are also passed to `reverse()` as the `kwargs` argument.
  • Loading branch information
James Socol committed Apr 15, 2011
1 parent bed3d8d commit 593127c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
19 changes: 19 additions & 0 deletions apps/sumo/templates/sumo/deprecated.html
@@ -0,0 +1,19 @@
{# vim: set ts=2 et sts=2 sw=2: #}
{% extends "base.html" %}
{% set title = _('Redirecting') %}
{% set refresh = (dest, 10) %}

{% block content %}
<article id="error-page" class="main">
<h1>{{ _('A URL has changed!') }}</h1>

<p>
{% trans %}
It looks like you've bookmarked an old URL. The new URL for this page
is below. You should update your bookmarks. We will forward you to the
new URL in 10 seconds.
{% endtrans %}
</p>
<p><strong>{{ proto }}{{ host }}{{ dest }}</strong></p>
</article>
{% endblock %}
23 changes: 21 additions & 2 deletions apps/sumo/tests/test_views.py
@@ -1,15 +1,19 @@
from django.conf import settings
from django.contrib.sites.models import Site
from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect

import mock
from nose.tools import eq_
from pyquery import PyQuery as pq
from test_utils import RequestFactory

from sumo.middleware import LocaleURLMiddleware
from sumo.tests import TestCase
from sumo.urlresolvers import reverse
from sumo.views import redirect_to
from sumo.views import deprecated_redirect, redirect_to


class RedirectToTestcase(TestCase):
class RedirectTests(TestCase):
rf = RequestFactory()

def test_redirect_to(self):
Expand All @@ -28,6 +32,21 @@ def test_redirect_kwargs(self):
eq_(reverse('users.confirm_email', args=['1234']),
resp['location'])

@mock.patch.object(Site.objects, 'get_current')
def test_deprecated_redirect(self, get_current):
get_current.return_value.domain = 'su.mo.com'
req = self.rf.get('/en-US/')
# Since we're rendering a template we need this to run.
LocaleURLMiddleware().process_request(req)
resp = deprecated_redirect(req, url='home')
eq_(200, resp.status_code)
doc = pq(resp.content)
assert doc('meta[http-equiv=refresh]')
refresh = doc('meta[http-equiv=refresh]')
timeout, url = refresh.attr('content').split(';url=')
eq_('10', timeout)
eq_(reverse('home'), url)


class RobotsTestCase(TestCase):
# Use the hard-coded URL because it's well-known.
Expand Down
12 changes: 12 additions & 0 deletions apps/sumo/views.py
Expand Up @@ -4,6 +4,7 @@
import StringIO

from django.conf import settings
from django.contrib.sites.models import Site
from django.core.cache import parse_backend_uri
from django.http import (HttpResponsePermanentRedirect, HttpResponseRedirect,
HttpResponse)
Expand Down Expand Up @@ -48,6 +49,17 @@ def redirect_to(request, url, permanent=True, **kwargs):
return HttpResponseRedirect(dest)


def deprecated_redirect(request, url, **kwargs):
"""Redirect with an interstitial page telling folks to update their
bookmarks.
"""
dest = reverse(url, kwargs=kwargs)
proto = 'https://' if request.is_secure() else 'http://'
host = Site.objects.get_current().domain
return jingo.render(request, 'sumo/deprecated.html',
{'dest': dest, 'proto': proto, 'host': host})


def robots(request):
"""Generate a robots.txt."""
if not settings.ENGAGE_ROBOTS:
Expand Down
4 changes: 4 additions & 0 deletions templates/base.html
Expand Up @@ -38,6 +38,10 @@
{% endfor %}
{% endif %}

{% if refresh %}
<meta http-equiv="refresh" content="{{ refresh[1] }};url={{ refresh[0] }}"/>
{% endif %}

{% if canonical_url %}
<link rel="canonical" href="{{ canonical_url }}" />
{% endif %}
Expand Down

0 comments on commit 593127c

Please sign in to comment.