Created
September 21, 2012 15:06
-
-
Save garrypolley/3762045 to your computer and use it in GitHub Desktop.
Django decorate 3rd party app's views
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from django.conf.urls import include | |
def decorated_include(urls, decorator) | |
"""Used to decorate all urls in a 3rd party app with a specific decorator""" | |
urls_to_decorate = include(urls) | |
for url_pattern in urls_to_decorate | |
url_pattern._callback = decorator(url_pattern._callback) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from django.conf.urls import patterns, include, url | |
from django.contrib.auth.decorators import login_required | |
from .decorators import decorated_include | |
urlpatterns = patterns('', | |
url(r'^admin/', decorated_include('mongoanut.urls', login_required), | |
url(r'^auth/', include('social_auth.urls')), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code seems to not cover the case where a URL pattern contains a
RegexURLResolver
rather than aRegexURLPattern
(i.e., an nestedinclude()
call in the examinedinclude()
call). This is my solution around that: