Add command 'initialize' to init a few defaults

Currently creates sikadmin group and perms
This commit is contained in:
okalintu
2017-05-17 14:38:26 +03:00
parent 2b5101566c
commit afdb03e1c1
+34
View File
@@ -0,0 +1,34 @@
from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
class Command(BaseCommand):
'''
Creates initial skeleton for the webapp.
This command MUST do nothing if already run.
'''
def handle(self, *args, **options):
self.stdout.write("Creating sikadmin group")
sikadmin_group, created = Group.objects.get_or_create(name="sikadmin")
if not created:
self.stdout.write("The group 'sikadmin' already existed and was not therefore created")
self.stdout.write("Creating sikadmin permission")
group_ctype = ContentType.objects.get_for_model(Group) # TODO Use some sikadmin native model when such exists
sikadmin_permission, created = Permission.objects.get_or_create(codename='sikadmin',
content_type=group_ctype,
name='SIK Admin')
if not created:
self.stdout.write("The permission 'sikadmin' already existed and was not therefore created")
self.stdout.write("Giving sikadmin group permission to sikadmin")
if sikadmin_group.permissions.filter(id=sikadmin_permission.id).exists():
self.stdout.write("Permission already existed. skipping...")
else:
sikadmin_group.permissions.add(sikadmin_permission)
self.stdout.write("Initialization successful")