Login

Decorating urlpatterns

Author:
miracle2k
Posted:
January 1, 2008
Language:
Python
Version:
.96
Score:
3 (after 7 ratings)

One thing I wanted for a while was the ability to basically apply something like @login_required to a bunch of urlpatterns in one go, instead of having to decorate each and every view manually.

In this example, the latter two views will always raise a 404.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from django.core.urlresolvers import RegexURLPattern
from django.conf.urls.defaults import patterns

class DecoratedURLPattern(RegexURLPattern):
    def resolve(self, *args, **kwargs):
        result = RegexURLPattern.resolve(self, *args, **kwargs)
        if result:
            result = list(result)
            result[0] = self._decorate_with(result[0])
        return result

def decorated_patterns(prefix, func, *args):
    result = patterns(prefix, *args)
    if func:
        for p in result:
            if isinstance(p, RegexURLPattern):
                p.__class__ = DecoratedURLPattern
                p._decorate_with = func
    return result

def control_access(view_func):
    def _checklogin(request, *args, **kwargs):
        raise Http404()
    return _checklogin

urlpatterns = patterns('views',
    # unprotected views
    (r'^public/contact/$',      'contact'),
    (r'^public/imprint/$',        'imprint'),
) + decorated_patterns('views', control_access,
    (r'^admin/add/$',      'add'),
    (r'^admin/edit/$',      'edit'),
)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

david_bgk (on January 2, 2008):

Why don't you use directly decorators in urls?

from foo.views import contact, imprint, add, edit

urlpatterns = patterns('',
    # unprotected views
    (r'^public/contact/$', contact),
    (r'^public/imprint/$', imprint),
    # protected views
    (r'^admin/add/$',  login_required(add)),
    (r'^admin/edit/$', login_required(edit)),
)

#

miracle2k (on January 3, 2008):

@david_bgk: It would break reverse().

Sure, I could name the patterns, and I have considered doing that from time to time, but so far I am not yet sure if I want to.

#

timbroder (on February 5, 2010):

if you want to user included url files, like so

  • decorated_patterns('', control_access, (r'^comments/', include('django.contrib.comments.urls')), )

modify decorated_patterns to be:

def decorated_patterns(prefix, func, *args):

result = patterns(prefix, *args)
if func:
    for p in result:
        if isinstance(p, RegexURLPattern):
            p.__class__ = DecoratedURLPattern
            p._decorate_with = func
        elif isinstance(p, RegexURLResolver):
            for pp in p._get_url_patterns():
                if isinstance(pp, RegexURLPattern):
                    pp.__class__ = DecoratedURLPattern
                    pp._decorate_with = func

return result

#

jeverling (on March 23, 2010):

This snippet works great, and allows you to enforce some precondition for a large number of URLs in one go (when using the fix from comment 3.).

If you add **kwargs to decorated_patterns, it also works with named patterns.

In my opinion, this is extremely useful, and the snippet is greatly underrated.

Thanks for sharing this!

#

aehlke (on March 1, 2011):

This is broken in Django 1.3.

To fix it, replace the DecoratedURLPattern class with this:

class DecoratedURLPattern(RegexURLPattern):
    def resolve(self, *args, **kwargs):
        result = RegexURLPattern.resolve(self, *args, **kwargs)
        if result:
            result.func = self._decorate_with(result.func)
        return result

#

jeverling (on July 21, 2011):

Thanks aehlke! :)

#

aehlke (on October 13, 2013):

Aaand to fix it with Django 1.6, replace _get_url_patterns() with url_patterns

:) Oh the joys of maintenance.

#

Please login first before commenting.