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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.