The Mixin approach for applying permissions to CBV views suffers from 3 issues:
1. you need to read the code to see what permissions are being applied to a View
2. multiple bits of disparate code required to specify, e.g., a simple permission check
3. permissions set on a base class are overridden by permission set on sub-class, unless special care is taken
Here's a nice trick, using only built-in django machinery, apply a decorator intended to decorate a django view function to a CBV view. https://docs.djangoproject.com/en/1.11/topics/class-based-views/intro/#decorating-the-class
This approach works for any function decorators with arguments - simply wrap it in a function that takes the same arguments:
def my_cbv_decorator(*args **kwargs):
return method_decorator(a_view_function_decorator(*args, **kwargs), name='dispatch')
Use your new CBV decorator to decorate View sub-classes:
@my_cbv_decorator('some_parameter')
class MyCBView(django.views.generic.TemplateView):
pass # dispatch method for this view is now wrapped by a_view_function_decorator
Note: you can also pass decorator parameter directly to method_decorator, but wrapping it up like this makes the code read nicer.
- view
- decorator
- permissions
- cbv
This view parses complex tag queries. It generates a list of model instances matching an expression of tags. The expression currently supports intersection, union and subtraction. Expressions can also be put in parenthesis and then combined with other expressions.
The expression must be passed to this view in the tag_expression argument. In my application this is simply passed from the URL querystring.
This snippet uses the django-tagging app.
It assumes that tags are composed of alphanumeric characters, underscores, hyphens and spaces, but the django-tagging application allows tags with other characters, so you might either want to restrict users to using tags that only contain the above characters, or you might prefer to improve this snippet.
Example: This URL
http://example.com/people/?(deceased&parrot)|"Monty Python"
will retrieve all people who are either deceased parrots or members of Monty Python.
In the tag_expression argument:
* ALL is treated as a keyword. If you happen (by some sad chance) to have a tag called ALL and want to use it in the expression, surround it in quotation marks. E.g. "ALL"
* Examples:
- famous -returns all instances of the model tagged with famous
- famous&deceased -returns all instances of the model tagged both famous and deceased.
- famous|deceased -returns all instances of the model tagged famous or deceased.
- parrot-deceased -returns all alive parrots in the model.
- ALL-deceased -returns all instances of the model that are not tagged deceased.
- ALL -returns all instances of the model
- "ALL" -returns all instances of the model that are tagged ALL
- "great author"&deceased -returns all models tagged as great authors and deceased.
Arguments:
* request -- HTTP Request object
* tag_expression -- a set expression of tags, supporting intersection, union, parenthesis and difference
* app_name -- App for the model we're working on (defaults to pubman)
* model_name -- Model on which to apply the set operations (defaults to Article)
* view -- view to redirect to after the model instance list has been
constructed
* html_template -- HTML template to redirect (defaults to 'pubman/tag.html')
- view
- django-tagging
- tag expression