Code

Ticket #682: fully-decoupled-urlconfs-v2.diff

File fully-decoupled-urlconfs-v2.diff, 1.2 KB (added by sa@…, 8 years ago)

Cleaner impl that also works with generic views.

Line 
1Index: django/conf/urls/defaults.py
2===================================================================
3--- django/conf/urls/defaults.py        (revision 992)
4+++ django/conf/urls/defaults.py        (working copy)
5@@ -1,12 +1,32 @@
6+import sys
7 from django.core.urlresolvers import RegexURLPattern, RegexURLResolver
8 
9-__all__ = ['handler404', 'handler500', 'include', 'patterns']
10+__all__ = ['handler404', 'handler500', 'include', 'patterns', 'prefix']
11 
12 handler404 = 'django.views.defaults.page_not_found'
13 handler500 = 'django.views.defaults.server_error'
14 
15 include = lambda urlconf_module: [urlconf_module]
16 
17+def prefix(postfix):
18+    """
19+    Figures out and adds a prefix to the given postfix by
20+    examining the module name of the calling URLconf.
21+   
22+    if the given postfix is:
23+        'views.polls.vote'
24+    and this function is called from:
25+        'myproject.apps.polls.urls'
26+    the return value will be:
27+        'myproject.apps.polls.views.polls.vote'
28+    """
29+    f = sys._getframe(1)
30+    module = f.f_globals["__name__"]
31+    segs = module.split(".")
32+    segs = segs[:3]
33+    segs.append(postfix)
34+    return ".".join(segs)
35+
36 def patterns(prefix, *tuples):
37     pattern_list = []
38     for t in tuples: