Add permission post init to initialize signal handler and registry file
This commit is contained in:
26
apps/permissions/post_init.py
Normal file
26
apps/permissions/post_init.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from django.db.models.signals import post_save
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
|
||||
|
||||
def user_post_save(sender, instance, **kwargs):
|
||||
from .settings import DEFAULT_ROLES
|
||||
|
||||
if kwargs.get('created', False):
|
||||
for default_role in SETTING_DEFAULT_ROLES:
|
||||
if isinstance(default_role, Role):
|
||||
#If a model is passed, execute method
|
||||
default_role.add_member(instance)
|
||||
else:
|
||||
#If a role name is passed, lookup the corresponding model
|
||||
try:
|
||||
role = Role.objects.get(name=default_role)
|
||||
role.add_member(instance)
|
||||
except ObjectDoesNotExist:
|
||||
pass
|
||||
|
||||
|
||||
def init_signal_handler():
|
||||
post_save.connect(user_post_save, sender=User)
|
||||
25
apps/permissions/registry.py
Normal file
25
apps/permissions/registry.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from __future__ import absolute_import
|
||||
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from smart_settings import LocalScope
|
||||
|
||||
from .icons import icon_permissions
|
||||
from .links import role_list
|
||||
|
||||
name = 'permissions'
|
||||
label = _(u'Permissions')
|
||||
description = _(u'Handles the permissions in a project.')
|
||||
icon = icon_permissions
|
||||
dependencies = ['app_registry', 'smart_settings']
|
||||
|
||||
settings=[
|
||||
{
|
||||
'name': 'DEFAULT_ROLES',
|
||||
'default': [],
|
||||
'description': _(u'A list of existing roles that are automatically assigned to newly created users'),
|
||||
'scopes': [LocalScope()]
|
||||
}
|
||||
]
|
||||
|
||||
setup_links=[role_list]
|
||||
Reference in New Issue
Block a user