Ticket #5034: set_urlconf.diff
File set_urlconf.diff, 9.3 KB (added by , 16 years ago) |
---|
-
django/core/urlresolvers.py
28 28 # be empty. 29 29 _prefixes = {} 30 30 31 # Overridden URLconfs for each thread are stored here. 32 _urlconfs = {} 33 31 34 class Resolver404(Http404): 32 35 pass 33 36 … … 296 299 return result + sub_match 297 300 298 301 def resolve(path, urlconf=None): 302 if urlconf is None: 303 urlconf = get_urlconf() 299 304 return get_resolver(urlconf).resolve(path) 300 305 301 306 def reverse(viewname, urlconf=None, args=None, kwargs=None, prefix=None): 307 if urlconf is None: 308 urlconf = get_urlconf() 302 309 args = args or [] 303 310 kwargs = kwargs or {} 304 311 if prefix is None: … … 328 335 """ 329 336 return _prefixes.get(currentThread(), u'/') 330 337 338 def set_urlconf(urlconf_name): 339 """ 340 Sets the URLconf for the current thread (overriding the default one in 341 settings). Set to None to revert back to the default. 342 """ 343 thread = currentThread() 344 if urlconf_name: 345 _urlconfs[thread] = urlconf_name 346 else: 347 # This is a lot faster than wrapping in a try/except. 348 if thread in _urlconfs: 349 del _urlconfs[thread] 350 351 def get_urlconf(default=None): 352 """ 353 Returns the root URLconf to use for the current thread if it has been 354 changed from the default one. 355 """ 356 thread = currentThread() 357 # It's faster to check first than to do _urlconfs.get(thread) 358 if thread in _urlconfs: 359 return _urlconfs[thread] 360 return default -
django/core/handlers/base.py
63 63 from django.core import exceptions, urlresolvers 64 64 from django.conf import settings 65 65 66 # Reset the urlconf for this thread. 67 urlresolvers.set_urlconf(None) 68 66 69 # Apply request middleware 67 70 for middleware_method in self._request_middleware: 68 71 response = middleware_method(request) 69 72 if response: 70 73 return response 71 74 72 # Get urlconf from request object, if available. Otherwise use default. 73 urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF) 75 # Get overridden urlconf for this thread, if available. Otherwise use 76 # default. 77 urlconf = urlresolvers.get_urlconf() 78 if urlconf is None: 79 # Get urlconf from request object, if available (deprecated). 80 if hasattr(request, 'urlconf'): 81 import warnings 82 warnings.warn("The 'request.urlconf' attribute is deprecated. " 83 "Use the 'django.core.urlresolvers::set_urlconf'" 84 " method instead.", DeprecationWarning) 85 urlconf = request.urlconf 86 urlresolvers.set_urlconf(urlconf) 87 else: 88 urlconf = settings.ROOT_URLCONF 74 89 75 90 resolver = urlresolvers.RegexURLResolver(r'^/', urlconf) 76 91 try: -
tests/regressiontests/urlpatterns_reverse/middleware.py
1 from django.core.urlresolvers import set_urlconf 2 3 import urlconf_inner 4 5 class ChangeUrlconfMiddleware: 6 def process_request(self, request): 7 set_urlconf(urlconf_inner.__name__) -
tests/regressiontests/urlpatterns_reverse/tests.py
1 1 "Unit tests for reverse URL lookup" 2 2 3 import re 4 import unittest 5 3 6 from django.core.urlresolvers import reverse_helper, NoReverseMatch 4 import re, unittest 7 from django.conf import settings 8 from django.test import TestCase 5 9 10 import urlconf_outer 11 import urlconf_inner 12 import middleware 13 6 14 test_data = ( 7 15 ('^places/(\d+)/$', 'places/3/', [3], {}), 8 16 ('^places/(\d+)/$', 'places/3/', ['3'], {}), … … 36 44 else: 37 45 self.assertEquals(got, expected) 38 46 39 if __name__ == "__main__": 40 run_tests(1) 47 class SetUrlConf(TestCase): 48 def setUp(self): 49 self.root_urlconf = settings.ROOT_URLCONF 50 self.middleware_classes = settings.MIDDLEWARE_CLASSES 51 settings.ROOT_URLCONF = urlconf_outer.__name__ 52 53 def tearDown(self): 54 settings.ROOT_URLCONF = self.root_urlconf 55 settings.MIDDLEWARE_CLASSES = self.middleware_classes 56 57 def test_handler(self): 58 response = self.client.get('/test/me/') 59 self.assertEqual(response.status_code, 200) 60 self.assertEqual(response.content, 'outer:/test/me/,' 61 'inner:/inner_urlconf/second_test/') 62 response = self.client.get('/inner_urlconf/second_test/') 63 self.assertEqual(response.status_code, 200) 64 response = self.client.get('/second_test/') 65 self.assertEqual(response.status_code, 404) 66 67 def test_handler_overridden(self): 68 settings.MIDDLEWARE_CLASSES += ( 69 '%s.ChangeUrlconfMiddleware' % middleware.__name__, 70 ) 71 response = self.client.get('/test/me/') 72 self.assertEqual(response.status_code, 404) 73 response = self.client.get('/inner_urlconf/second_test/') 74 self.assertEqual(response.status_code, 404) 75 response = self.client.get('/second_test/') 76 self.assertEqual(response.status_code, 200) 77 self.assertEqual(response.content, 'outer:,inner:/second_test/') -
tests/regressiontests/urlpatterns_reverse/urlconf_inner.py
1 from django.conf.urls.defaults import * 2 from django.template import Template, Context 3 from django.http import HttpResponse 4 5 def inner_view(request): 6 content = Template('outer:{% url outer %},' 7 'inner:{% url inner %}').render(Context()) 8 return HttpResponse(content) 9 10 urlpatterns = patterns('teacher_survey.views', 11 url(r'^second_test/$', inner_view, name='inner'), 12 ) 13 -
tests/regressiontests/urlpatterns_reverse/urlconf_outer.py
1 from django.conf.urls.defaults import * 2 3 import urlconf_inner 4 5 urlpatterns = patterns('', 6 url(r'^test/me/$', urlconf_inner.inner_view, name='outer'), 7 url(r'^inner_urlconf/', include(urlconf_inner.__name__)) 8 ) 9 -
docs/url_dispatch.txt
34 34 35 35 1. Django determines the root URLconf module to use. Ordinarily, 36 36 this is the value of the ``ROOT_URLCONF`` setting in your 37 `settings file`_, but if the incoming ``HttpRequest`` object38 has an attribute called ``urlconf``, its value will be usedin39 place of the ``ROOT_URLCONF`` setting.37 `settings file`_, but this can be overridden per request by calling the 38 ``set_urlconf`` method found in ``django.core.urlresolvers`` (passing in 39 the value to be used in place of the ``ROOT_URLCONF`` setting). 40 40 2. Django loads that Python module and looks for the variable 41 41 ``urlpatterns``. This should be a Python list, in the format returned by 42 42 the function ``django.conf.urls.defaults.patterns()``. -
docs/settings.txt
388 388 389 389 Still, note that there are always going to be sections of your debug output that 390 390 are inappropriate for public consumption. File paths, configuration options, and 391 the like all give attackers extra information about your server. 391 the like all give attackers extra information about your server. 392 392 393 393 It is also important to remember that when running with ``DEBUG`` turned on, Django 394 394 will remember every SQL query it executes. This is useful when you are debugging, … … 820 820 821 821 Default: Not defined 822 822 823 A string representing the full Python import path to your root URLconf. For example:824 ``"mydjangoapps.urls"``. Can be overridden on a per-request basis by825 setting the attribute ``urlconf`` on the incoming ``HttpRequest`` 826 object. See`How Django processes a request`_ for details.823 A string representing the full Python import path to your root URLconf. For 824 example: ``"mydjangoapps.urls"``. Can be overridden on a per-request basis by 825 using the ``set_urlconf`` method in ``django.core.urlresolvers``. See 826 `How Django processes a request`_ for details. 827 827 828 828 .. _How Django processes a request: ../url_dispatch/#how-django-processes-a-request 829 829