Login

app based group permissions pre load without foreign keys

Author:
ZaherSarieddine
Posted:
June 4, 2015
Language:
Python
Version:
1.7
Score:
0 (after 0 ratings)

Use post_migrate to load bulk permissions to grant groups full access to certain apps using the group name and the app name only.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@receiver(post_migrate)
def post_post_migrate(sender=None, verbosity=0, **kwargs):
# check one of your installed apps to ensure that the script runs only once
# this is important because post_migrate is fired for all apps even when migrating a specific app
    if sender.name != 'myapps.administration':
        return

    # Put your code here.
    group_perms = {
        u'staff': {
            'apps': (u'fees', )
        },
        u'users': {
            'apps': ('tpes',)
        },
        u'admin': {
            'apps': ('administration',)
       
    }
    for group_name in group_perms.keys():
        print 'adding permissions for {0}'.format(group_name)
        group = Group.objects.get(name=group_name)
        for app_label in group_perms[group_name]['apps']:
            for content_type in ContentType.objects.filter(app_label=app_label):
                for perm in Permission.objects.filter(content_type=content_type):
                    group.permissions.add(perm)
        group.save()

More like this

  1. Browser-native date input field by kytta 1 month ago
  2. Generate and render HTML Table by LLyaudet 1 month, 2 weeks ago
  3. My firs Snippets by GutemaG 1 month, 2 weeks ago
  4. FileField having auto upload_to path by junaidmgithub 2 months, 3 weeks ago
  5. LazyPrimaryKeyRelatedField by LLyaudet 3 months ago

Comments

Please login first before commenting.