permission.utils package

Submodules

permission.utils.autodiscover module

permission.utils.autodiscover.autodiscover(module_name=None)[source]

Autodiscover INSTALLED_APPS perms.py modules and fail silently when not present. This forces an import on them to register any permissions bits they may want.

permission.utils.autodiscover.discover(app, module_name=None)[source]

Automatically apply the permission logics written in the specified module.

Examples

Assume if you have a perms.py in your_app as:

from permission.logics import AuthorPermissionLogic
PERMISSION_LOGICS = (
    ('your_app.your_model', AuthorPermissionLogic),
)

Use this method to apply the permission logics enumerated in PERMISSION_LOGICS variable like:

>>> discover('your_app')

permission.utils.field_lookup module

permission.utils.field_lookup.field_lookup(obj, field_path)[source]

Lookup django model field in similar way of django query lookup

Args:
obj (instance): Django Model instance field_path (str): ‘__’ separated field path
Example:
>>> from django.db import model
>>> from django.contrib.auth.models import User
>>> class Article(models.Model):
>>>     title = models.CharField('title', max_length=200)
>>>     author = models.ForeignKey(User, null=True,
>>>             related_name='permission_test_articles_author')
>>>     editors = models.ManyToManyField(User,
>>>             related_name='permission_test_articles_editors')
>>> user = User.objects.create_user('test_user', 'password')
>>> article = Article.objects.create(title='test_article',
...                                  author=user)
>>> aritcle.editors.add(user)
>>> assert 'test_article' == field_lookup(article, 'title')
>>> assert 'test_user' == field_lookup(article, 'user__username')
>>> assert ['test_user'] == list(field_lookup(article,
...                                           'editors__username'))

permission.utils.handlers module

A utilities of permission handler

class permission.utils.handlers.PermissionHandlerRegistry[source]

Bases: object

A registry class of permission handler

Methods

get_handlers()[source]

Get registered handler instances

Returns:

tuple

permission handler tuple

register(model, handler=None)[source]

Register a permission handler to the model

Parameters:

model : django model class

A django model class

handler : permission handler class or None

A permission handler class

Raises:

ImproperlyConfigured

Raise when the model is abstract model

KeyError

Raise when the model is already registered in registry The model cannot have more than one handler.

unregister(model)[source]

Unregister a permission handler from the model

Parameters:

model : django model class

A django model class

handler : permission handler class or None

A permission handler class

Raises:

KeyError

Raise when the model have not registered in registry yet.

permission.utils.logics module

Permission logic utilities

permission.utils.logics.add_permission_logic(model, permission_logic)[source]

Add permission logic to the model

Parameters:

model : django model class

A django model class which will be treated by the specified permission logic

permission_logic : permission logic instance

A permission logic instance which will be used to determine permission of the model

Examples

>>> from django.db import models
>>> from permission.logics import PermissionLogic
>>> class Mock(models.Model):
...     name = models.CharField('name', max_length=120)
>>> add_permission_logic(Mock, PermissionLogic())
permission.utils.logics.remove_permission_logic(model, permission_logic, fail_silently=True)[source]

Remove permission logic to the model

Parameters:

model : django model class

A django model class which will be treated by the specified permission logic

permission_logic : permission logic class or instance

A permission logic class or instance which will be used to determine permission of the model

fail_silently : boolean

If True then do not raise KeyError even the specified permission logic have not registered.

Examples

>>> from django.db import models
>>> from permission.logics import PermissionLogic
>>> class Mock(models.Model):
...     name = models.CharField('name', max_length=120)
>>> logic = PermissionLogic()
>>> add_permission_logic(Mock, logic)
>>> remove_permission_logic(Mock, logic)

permission.utils.permissions module

Permission utility module.

In this module, term perm indicate the identifier string permission written in ‘app_label.codename’ format.

permission.utils.permissions.get_app_perms(model_or_app_label)[source]

Get perm (a string in format of ‘app_label.codename’) list of the specified django application.

Parameters:

model_or_app_label : model class or string

A model class or app_label string to specify the particular django application.

Returns:

set

A set of perms of the specified django application.

Examples

>>> perms1 = get_app_perms('auth')
>>> perms2 = get_app_perms(Permission)
>>> perms1 == perms2
True
permission.utils.permissions.get_model_perms(model)[source]

Get perm (a string in format of ‘app_label.codename’) list of the specified django model.

Parameters:

model : model class

A model class to specify the particular django model.

Returns:

set

A set of perms of the specified django model.

Examples

>>> sorted(get_model_perms(Permission)) == ['auth.add_permission', 'auth.change_permission', 'auth.delete_permission']
True
permission.utils.permissions.get_perm_codename(perm, fail_silently=True)[source]

Get permission codename from permission string

Examples

>>> get_perm_codename('app_label.codename_model') == 'codename_model'
True
>>> get_perm_codename('app_label.codename') == 'codename'
True
>>> get_perm_codename('codename_model') == 'codename_model'
True
>>> get_perm_codename('codename') == 'codename'
True
>>> get_perm_codename('app_label.app_label.codename_model') == 'app_label.codename_model'
True
permission.utils.permissions.perm_to_permission(perm)[source]

Convert a identifier string permission format in ‘app_label.codename’ (teremd as perm) to a django permission instance.

Examples

>>> permission = perm_to_permission('auth.add_user')
>>> permission.content_type.app_label == 'auth'
True
>>> permission.codename == 'add_user'
True
permission.utils.permissions.permission_to_perm(permission)[source]

Convert a django permission instance to a identifier string permission format in ‘app_label.codename’ (termed as perm).

Examples

>>> permission = Permission.objects.get(
...     content_type__app_label='auth',
...     codename='add_user',
... )
>>> permission_to_perm(permission) == 'auth.add_user'
True

Module contents