aboutsummaryrefslogtreecommitdiffstats
path: root/modules/django_application/files/django_add_permission_to_group.py
blob: 4fa77e8d0f74deccd017b9102c327ea792dee6c3 (plain)
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
#!/usr/bin/python
import sys
group_name = sys.argv[1]
permission = sys.argv[2]

# as codename is not unique, we need to give the application name
app = ''
if len(sys.argv) > 3:
        app = sys.argv[3]

from django.contrib.auth.models import Group, Permission
group = Group.objects.get(name=group_name)

permissions = Permission.objects.filter(codename=permission)
if app:
    permissions = permissions.filter(content_type__app_label__exact=app)

if len(permissions) > 1:
	print "Error, result not unique, please give the application among :"
	print ' '.join([p.content_type.app_label for p in permissions])
	sys.exit(1)
elif len(permissions) < 1:
	print "Error, wrong codename"
	sys.exit(1)

group.permissions.add(permissions[0])
group.save()