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") # TODO Use some sikadmin native model when such exists group_ctype = ContentType.objects.get_for_model(Group) 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")