Ticket #3530: permalink_with_additional_function_arg.diff

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

simple patch to allow passing an additional function arg which returns the request object, or directly an urlconf list

  • django/db/models/__init__.py

     
    1818
    1919# Decorator. Takes a function that returns a tuple in this format:
    2020#     (viewname, viewargs, viewkwargs)
     21#   Optionally takes a function which should either return an object with
     22#     an attribute 'urlconf' or directly a python list which is used instead of
     23#     settings.ROOT_URLCONF
    2124# Returns a function that calls urlresolvers.reverse() on that data, to return
    2225# the URL for those parameters.
    23 def permalink(func):
     26def permalink(func, get_urlconf_func = None):
    2427    from django.core.urlresolvers import reverse
    2528    def inner(*args, **kwargs):
     29        # Find urlconf ...
     30        urlconf = None
     31        if get_urlconf_func != None:
     32            urlconf = get_urlconf_func()
     33            if type(urlconf) != list:
     34                # If type is no list, we assume it is a request object and
     35                # look for a 'urlconf' attribute
     36                urlconf = getattr(urlconf, 'urlconf')
     37       
    2638        bits = func(*args, **kwargs)
    2739        viewname = bits[0]
    28         return reverse(bits[0], None, *bits[1:3])
     40        return reverse(bits[0], urlconf, *bits[1:3])
    2941    return inner
    3042
    3143class LazyDate(object):
Back to Top