djblets.util.decorators¶
-
simple_decorator
(decorator)[source]¶ This decorator can be used to turn simple functions into well-behaved decorators, so long as the decorators are fairly simple. If a decorator expects a function and returns a function (no descriptors), and if it doesn’t modify function attributes or docstring, then it is eligible to use this. Simply apply @simple_decorator to your decorator and it will automatically preserve the docstring and function attributes of functions to which it is applied.
-
augment_method_from
(klass)[source]¶ Augments a class’s method with new decorators or documentation.
This is useful when a class needs to add new decorators or new documentation to a parent class’s method, without changing the behavior or burying the existing decorators.
The methods using this decorator can provide code to run at the end of the parent function. Usually, though, it will just have an empty body of
pass
.
-
basictag
(takes_context=False)[source]¶ Create a basic template tag with context.
This is similar to Django’s
@register.simple_tag
that optionally takes a context parameter. This condenses many tag implementations down to a few lines of code.For example:
@register.tag @basictag(takes_context=True) def printuser(context): return context['user']
-
blocktag
(*args, **kwargs)[source]¶ Creates a block template tag with beginning/end tags.
This does all the hard work of creating a template tag that can parse the arguments passed in and then parse all nodes between a beginning and end tag (such as myblock/endmyblock).
By default, the end tag is prefixed with “end”, but that can be changed by passing
end_prefix="end_"
or similar to @blocktag.blocktag will call the wrapped function with context and nodelist parameters, as well as any parameters passed to the tag. It will also ensure that a proper error is raised if too many or too few parameters are passed.
For example:
@register.tag @blocktag def divify(context, nodelist, div_id=None): s = "<div" if div_id: s += " id='%s'" % div_id return s + ">" + nodelist.render(context) + "</div>"
-
class
cached_property
(func)[source]¶ Bases:
django.utils.functional.cached_property
Decorator for creating a read-only property that caches a value.
This is a drop-in replacement for Django’s
cached_property
that retains the docstring and attributes of the original method.While Django 1.8+ does retain the docstring, it does not retain the attributes.