Code

Ticket #682: fully-decoupled-urlconfs.diff

File fully-decoupled-urlconfs.diff, 1.2 KB (added by sa@…, 9 years ago)
Line 
1Index: django/conf/urls/defaults.py
2===================================================================
3--- django/conf/urls/defaults.py        (revision 981)
4+++ django/conf/urls/defaults.py        (working copy)
5@@ -1,3 +1,4 @@
6+import sys
7 from django.core.urlresolvers import RegexURLPattern, RegexURLResolver
8 
9 __all__ = ['handler404', 'handler500', 'include', 'patterns']
10@@ -7,7 +8,33 @@
11 
12 include = lambda urlconf_module: [urlconf_module]
13 
14+
15+def _get_prefix():
16+    """
17+    Figures out the prefix for the calling URLconf module.
18+
19+    If the calling URLconf is at:
20+        myproject.apps.polls.urls
21+    the returned prefix will be:
22+        myproject.apps.polls.views.polls
23+    """
24+    f = sys._getframe(2)
25+    module = f.f_globals["__name__"]
26+    segs = module.split(".")
27+    # get rid of the 'urls' segment
28+    segs.pop()
29+    # now the last segment is the apps name
30+    app_name = segs[-1]
31+    # inject the 'views' module
32+    segs.append("views")
33+    # add the apps name to the end
34+    segs.append(app_name)
35+    # return the generated prefix
36+    return ".".join(segs)
37+
38 def patterns(prefix, *tuples):
39+    if prefix is None:
40+        prefix = _get_prefix()
41     pattern_list = []
42     for t in tuples:
43         if type(t[1]) == list: