Code

Ticket #3530: permalink_with_additional_function_arg_fixed.diff

File permalink_with_additional_function_arg_fixed.diff, 1.3 KB (added by Herbert Poul <herbert.poul@…>, 7 years ago)

discovered a small bug in the patch

Line 
1Index: django/db/models/__init__.py
2===================================================================
3--- django/db/models/__init__.py        (revision 4549)
4+++ django/db/models/__init__.py        (working copy)
5@@ -18,14 +18,26 @@
6 
7 # Decorator. Takes a function that returns a tuple in this format:
8 #     (viewname, viewargs, viewkwargs)
9+#   Optionally takes a function which should either return an object with
10+#     an attribute 'urlconf' or directly a python list which is used instead of
11+#     settings.ROOT_URLCONF
12 # Returns a function that calls urlresolvers.reverse() on that data, to return
13 # the URL for those parameters.
14-def permalink(func):
15+def permalink(func, get_urlconf_func = None):
16     from django.core.urlresolvers import reverse
17     def inner(*args, **kwargs):
18+        # Find urlconf ...
19+        urlconf = None
20+        if get_urlconf_func != None:
21+            urlconf = get_urlconf_func()
22+            if type(urlconf) != list:
23+                # If type is no list, we assume it is a request object and
24+                # look for a 'urlconf' attribute
25+                urlconf = getattr(urlconf, 'urlconf', None)
26+       
27         bits = func(*args, **kwargs)
28         viewname = bits[0]
29-        return reverse(bits[0], None, *bits[1:3])
30+        return reverse(bits[0], urlconf, *bits[1:3])
31     return inner
32 
33 class LazyDate(object):