Ticket #5925: 5925.reverse_lazy.2.diff

File 5925.reverse_lazy.2.diff, 5.8 KB (added by Julien Phalip, 12 years ago)

Added more tests for the original use case of this ticket (i.e. user_passes_test)

  • django/core/urlresolvers.py

    diff --git a/django/core/urlresolvers.py b/django/core/urlresolvers.py
    index a35287e..52d9090 100644
    a b from django.conf import settings 
    1515from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist
    1616from django.utils.datastructures import MultiValueDict
    1717from django.utils.encoding import iri_to_uri, force_unicode, smart_str
    18 from django.utils.functional import memoize
     18from django.utils.functional import memoize, lazy
    1919from django.utils.importlib import import_module
    2020from django.utils.regex_helper import normalize
    def reverse(viewname, urlconf=None, args=None, kwargs=None, prefix=None, current 
    390390    return iri_to_uri(u'%s%s' % (prefix, resolver.reverse(view,
    391391            *args, **kwargs)))
     393reverse_lazy = lazy(reverse, str)
    393395def clear_url_caches():
    394396    global _resolver_cache
    395397    global _callable_cache
  • docs/releases/1.4.txt

    diff --git a/docs/releases/1.4.txt b/docs/releases/1.4.txt
    index a63fff7..8ceed20 100644
    a b compatibility with old browsers, this change means that you can use any HTML5 
    3737features you need in admin pages without having to lose HTML validity or
    3838override the provided templates to change the doctype.
     43A lazily evaluated version of ``django.core.urlresolvers.reverse()`` was added
     44to allow using URL reversals before the project's URLConf gets loaded.
    4046.. _backwards-incompatible-changes-1.4:
    4248Backwards incompatible changes in 1.4
  • docs/topics/http/urls.txt

    diff --git a/docs/topics/http/urls.txt b/docs/topics/http/urls.txt
    index d721012..af66fa8 100644
    a b namespaces into URLs on specific application instances, according to the 
    827827    ``urllib.quote``) to the ouput of :meth:`~django.core.urlresolvers.reverse`
    828828    may produce undesirable results.
     833.. versionadded:: 1.4
     835A lazily evaluated version of `reverse()`_.
     837It is useful for when you need to use a URL reversal before your project's
     838URLConf is loaded. Some common cases where this method is necessary are::
     840* providing a reversed URL as the ``url`` attribute of a generic class-based
     841  view.
     843* providing a reversed URL to a decorator (such as the ``login_url`` argument
     844  for the ``django.contrib.auth.decorators.permission_required`` decorator).
     846* providing a reversed URL as a default value for a parameter in a function's
     847  signature.
  • new file tests/regressiontests/urlpatterns_reverse/reverse_lazy_urls.py

    diff --git a/tests/regressiontests/urlpatterns_reverse/reverse_lazy_urls.py b/tests/regressiontests/urlpatterns_reverse/reverse_lazy_urls.py
    new file mode 100644
    index 0000000..8f7b8a9
    - +  
     1from django.conf.urls.defaults import *
     3from views import empty_view, LazyRedictView, login_required_view
     6urlpatterns = patterns('',
     7    url(r'^redirected_to/$', empty_view, name='named-lazy-url-redirected-to'),
     8    url(r'^login/$', empty_view, name='some-login-page'),
     9    url(r'^login_required_view/$', login_required_view),
     10    url(r'^redirect/$', LazyRedictView.as_view()),
  • tests/regressiontests/urlpatterns_reverse/tests.py

    diff --git a/tests/regressiontests/urlpatterns_reverse/tests.py b/tests/regressiontests/urlpatterns_reverse/tests.py
    index 198d556..0cb678c 100644
    a b from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect 
    1010from django.shortcuts import redirect
    1111from django.test import TestCase
    1212from django.utils import unittest
     13from django.contrib.auth.models import User
    1415import urlconf_outer
    1516import urlconf_inner
    class ResolverTests(unittest.TestCase): 
    218219                        else:
    219220                            self.assertEqual(t.name, e['name'], 'Wrong URL name.  Expected "%s", got "%s".' % (e['name'], t.name))
     222class ReverseLazyTest(TestCase):
     223    urls = 'regressiontests.urlpatterns_reverse.reverse_lazy_urls'
     225    def test_redirect_with_lazy_reverse(self):
     226        response = self.client.get('/redirect/')
     227        self.assertRedirects(response, "/redirected_to/", status_code=301)
     229    def test_user_permission_with_lazy_reverse(self):
     230        user = User.objects.create_user('alfred', 'alfred@example.com', password='testpw')
     231        response = self.client.get('/login_required_view/')
     232        self.assertRedirects(response, "/login/?next=/login_required_view/", status_code=302)
     233        self.client.login(username='alfred', password='testpw')
     234        response = self.client.get('/login_required_view/')
     235        self.assertEqual(response.status_code, 200)
    221237class ReverseShortcutTests(TestCase):
    222238    urls = 'regressiontests.urlpatterns_reverse.urls'
  • tests/regressiontests/urlpatterns_reverse/views.py

    diff --git a/tests/regressiontests/urlpatterns_reverse/views.py b/tests/regressiontests/urlpatterns_reverse/views.py
    index fdd7423..271b0e9 100644
    a b  
    11from django.http import HttpResponse
     2from django.contrib.auth.decorators import user_passes_test
     3from django.views.generic import RedirectView
     4from django.core.urlresolvers import reverse_lazy
    36def empty_view(request, *args, **kwargs):
    47    return HttpResponse('')
    class ViewClass(object): 
    1619view_class_instance = ViewClass()
     21class LazyRedictView(RedirectView):
     22    url = reverse_lazy('named-lazy-url-redirected-to')
     24@user_passes_test(lambda u: u.is_authenticated(), login_url=reverse_lazy('some-login-page'))
     25def login_required_view(request):
     26    return HttpResponse('Hello you')
    1828def bad_view(request, *args, **kwargs):
    1929    raise ValueError("I don't think I'm getting good value for this view")
Back to Top