diff --git a/webapp/management/commands/initialize.py b/webapp/management/commands/initialize.py new file mode 100644 index 0000000..ab239bf --- /dev/null +++ b/webapp/management/commands/initialize.py @@ -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") +