Django authentication and authorization utilities
Project description
Django authentication and authorization utilities.
Installation
pip install django-auth-utils
Supported and tested on:
Python: 2.7, 3.4, 3.5, PyPy
Django: 1.8, 1.9
Configuration
In order to use the auth_utils template tag library, add auth_utils to your INSTALLED_APPS.
Alternatively, since Django 1.9, you can add auth_utils.templatetags.auth_utils to your DjangoTemplates OPTIONS.
Usage
Permission-checking views
The ObjectPermissionRequiredMixin view combines Django’s PermissionRequiredMixin and SingleObjectMixin views, and performs the permission check against the object that was looked up.
Use it like the base classes:
from auth_utils.views import ObjectPermissionRequiredMixin
class ArticleDetail(ObjectPermissionRequiredMixin, generic.DetailView):
model = Article
permission_required = ['news.read_article']
class ArticleUpdate(ObjectPermissionRequiredMixin, generic.UpdateView):
model = Article
permission_required = ['news.change_article']
Permission-checking in templates
Load the template tag library:
{% load auth_utils %}
The perms filter allows checking object-level permissions with a convenient syntax:
{% if perm in user|perms:object %} ... {% endif %}
The object argument is optional. If omitted, the global permission is checked, similar to Django’s perms object.
Examples:
{% if 'news.read_article' in user|perms:article %}
{{ article.text }}
{% else %}
You do not have permission to read this article.
{% endif %}
{% if 'news.change_article' in user|perms:article %}
<a href="...">Edit article</a>
{% endif %}
{% if 'news.delete_article' in user|perms:article %}
<a href="...">Delete article</a>
{% endif %}
The library provides can_change and can_delete shorthands for checking Django’s default app.change_model and app.delete_model model permissions:
{% if user|can_change:article %} <a href="...">Edit</a> {% endif %}
{% if user|can_delete:article %} <a href="...">Delete</a> {% endif %}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for django_auth_utils-0.1rc1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 720aee9a64c365b932eb5158cd4cf1cfd957d917b281f980d0b1505b416d1d1d |
|
MD5 | b27dffea5faa32546f55ce6b5eef7e30 |
|
BLAKE2b-256 | 5995656836572148641c84e95dc090eb911ea7c72882215f44b795db14a5468a |
Hashes for django_auth_utils-0.1rc1-py2-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 026dfb11d9c32c74e0d044130421eaf05914d1bf7cb07e3f9c757a2d9ee20f91 |
|
MD5 | b845a99dd41e09d6351f7b9f740c3c0e |
|
BLAKE2b-256 | bb6d7782296ed8e0a2f39e72320d918e3ef26adabb3935a6bf1fa042ae668da8 |