Permission values
* adds raw permission values for all groups
This commit is contained in:
@@ -7,11 +7,11 @@ Created on: 15.12.20
|
||||
"""
|
||||
from getpass import getpass
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib.auth.models import User, Group, Permission
|
||||
from django.core.management import BaseCommand
|
||||
from django.db import transaction
|
||||
|
||||
from konova.management.commands.setup_test_data import TEST_ORGANISATION_DATA
|
||||
from konova.management.commands.setup_data import TEST_ORGANISATION_DATA, GROUPS_DATA
|
||||
from organisation.models import Organisation
|
||||
|
||||
CREATED_TEMPLATE = "{} created"
|
||||
@@ -23,39 +23,28 @@ class Command(BaseCommand):
|
||||
def handle(self, *args, **options):
|
||||
try:
|
||||
with transaction.atomic():
|
||||
self.__init_superuser()
|
||||
self.__init_test_organisation()
|
||||
self._init_superuser()
|
||||
self._init_test_organisation()
|
||||
self._init_default_groups()
|
||||
except KeyboardInterrupt:
|
||||
self.__break_line()
|
||||
self._break_line()
|
||||
exit(-1)
|
||||
|
||||
def __init_superuser(self):
|
||||
def _init_superuser(self):
|
||||
""" Create a superuser by user prompt input
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
self.stdout.write(
|
||||
self.style.WARNING(
|
||||
"--- Superuser ---",
|
||||
)
|
||||
)
|
||||
self._write_warning("--- Superuser ---")
|
||||
username = input("Superuser name: ")
|
||||
if User.objects.filter(username=username).exists():
|
||||
self.stdout.write(
|
||||
self.style.ERROR(
|
||||
"Name already taken!"
|
||||
)
|
||||
)
|
||||
self._write_error("Name already taken!")
|
||||
exit(-1)
|
||||
pw = getpass("Password: ")
|
||||
pw_confirm = getpass("Confirm password : ")
|
||||
if pw != pw_confirm:
|
||||
self.stdout.write(
|
||||
self.style.ERROR(
|
||||
"Passwords did not match!"
|
||||
)
|
||||
)
|
||||
self._write_error("Passwords did not match!")
|
||||
exit(-1)
|
||||
|
||||
# Create superuser
|
||||
@@ -65,39 +54,75 @@ class Command(BaseCommand):
|
||||
superuser.is_staff = True
|
||||
superuser.set_password(pw)
|
||||
superuser.save()
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS(
|
||||
"Superuser {} created".format(username)
|
||||
)
|
||||
)
|
||||
self.__break_line()
|
||||
self._write_success("Superuser {} created".format(username))
|
||||
self._break_line()
|
||||
|
||||
def __init_test_organisation(self):
|
||||
def _init_test_organisation(self):
|
||||
""" Creates test organisations from predefined data
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
self.stdout.write(
|
||||
self.style.WARNING(
|
||||
"--- Organisations ---"
|
||||
)
|
||||
)
|
||||
self._write_warning("--- Organisations ---")
|
||||
for org in TEST_ORGANISATION_DATA:
|
||||
db_org = Organisation.objects.get_or_create(
|
||||
**org
|
||||
)[0]
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS(
|
||||
CREATED_TEMPLATE.format(db_org.name)
|
||||
)
|
||||
)
|
||||
self.__break_line()
|
||||
self._write_success(CREATED_TEMPLATE.format(db_org.name))
|
||||
self._break_line()
|
||||
|
||||
def __break_line(self):
|
||||
def _init_default_groups(self):
|
||||
""" Creates the default groups for konova:
|
||||
* Group default
|
||||
* Group ZB (registration office employees)
|
||||
* Group ETS (conservation office employees)
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
self._write_warning("--- Groups ---")
|
||||
for group_data in GROUPS_DATA:
|
||||
name = group_data.get("name")
|
||||
perms_data = group_data.get("permissions")
|
||||
perms_objects = [
|
||||
Permission.objects.get_or_create(
|
||||
codename=perm[0],
|
||||
name=perm[1]
|
||||
)
|
||||
for perm in perms_data
|
||||
]
|
||||
group = Group.objects.get_or_create(
|
||||
name=name,
|
||||
)
|
||||
group.permissions.set(perms_objects)
|
||||
|
||||
self._break_line()
|
||||
|
||||
def _break_line(self):
|
||||
""" Simply prints a line break
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
self.stdout.write("\n")
|
||||
self.stdout.write("\n")
|
||||
|
||||
def _write_warning(self, txt: str):
|
||||
self.stdout.write(
|
||||
self.style.WARNING(
|
||||
txt
|
||||
)
|
||||
)
|
||||
|
||||
def _write_success(self, txt: str):
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS(
|
||||
txt
|
||||
)
|
||||
)
|
||||
|
||||
def _write_error(self, txt: str):
|
||||
self.stdout.write(
|
||||
self.style.ERROR(
|
||||
txt
|
||||
)
|
||||
)
|
||||
Reference in New Issue
Block a user