Utils

Autodiscover

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')

field_lookup

A module to lookup field of object.

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

Lookup django model field in similar way of django query lookup.

Parameters:
  • 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)
>>> article.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'))

Handlers utils

A utilities of permission handler

class permission.utils.handlers.PermissionHandlerRegistry[source]

Bases: object

A registry class of permission handler

get_handlers()[source]

Get registered handler instances

Returns:

permission handler tuple

Return type:

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, string, or None) – A permission handler class or a dotted path

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

Raises:

KeyError – Raise when the model have not registered in registry yet.

Logics utils

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)

Permissions utils

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 permission-string 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:

A set of perms of the specified django application.

Return type:

set

Examples

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

Get permission-string list of a specified django model.

Parameters:

model (model class) – A model class to specify the particular django model.

Returns:

A set of perms of the specified django model.

Return type:

set

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'
>>> get_perm_codename('app_label.codename')
'codename'
>>> get_perm_codename('codename_model')
'codename_model'
>>> get_perm_codename('codename')
'codename'
>>> get_perm_codename('app_label.app_label.codename_model')
'app_label.codename_model'
permission.utils.permissions.perm_to_permission(perm)[source]

Convert a permission-string to a permission instance.

Examples

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

Convert a permission instance to a permission-string.

Examples

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