def permalink(func):
    """
    Decorator that calls urlresolvers.reverse() to return a URL using
    parameters returned by the decorated function "func".

    "func" should be a function that returns a tuple in one of the
    following formats:
        (viewname, viewargs)
        (viewname, viewargs, viewkwargs)
    """
    from django.core.urlresolvers import reverse
    def inner(self, *args, **kwargs):
        bits = func(self, *args, **kwargs)
        if isinstance(self.pluggable_url, dict):
            view_name_prefix = self.pluggable_url.get('prefix', '')
            view_name = '%s%s' % (view_name_prefix and ('%s_' % view_name_prefix) or '', bits[0])
            parent_args = self.pluggable_url.get('parent_args', [])
            parent_kwargs = self.pluggable_url.get('parent_kwargs', {})
            if len(bits) >= 2 and bits[1]:
                parent_args = parent_args + bits[1]
            if len(bits) >= 3 and bits[2]:
                parent_kwargs.update(bits[2])
            bits = [view_name, parent_args, parent_kwargs]
        return reverse(bits[0], None, *bits[1:3])
    return inner
dataset/ETHPy150Open nowells/django-pluggables/pluggables/__init__.py/permalink/original