Refactoring to konova

This commit is contained in:
mipel
2021-07-01 14:38:57 +02:00
parent a5e8bcfa8c
commit 4084373e2b
60 changed files with 103 additions and 17901 deletions

View File

@@ -0,0 +1,129 @@
"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 15.12.20
"""
from getpass import getpass
from django.contrib.auth.models import User
from django.core.management import BaseCommand
from django.db import transaction
from konova.management.commands.setup_test_data import TEST_ORGANISATION_DATA, TEST_ROLE_GROUPS_DATA
from konova.models import RoleType, RoleGroup
from organisation.models import Organisation
CREATED_TEMPLATE = "{} created"
class Command(BaseCommand):
help = "Initializes database with basic data"
def handle(self, *args, **options):
try:
with transaction.atomic():
self.__init_superuser()
self.__init_test_organisation()
self.__init_role_groups()
except KeyboardInterrupt:
self.__break_line()
exit(-1)
def __init_superuser(self):
""" Create a superuser by user prompt input
Returns:
"""
self.stdout.write(
self.style.WARNING(
"--- Superuser ---",
)
)
username = input("Superuser name: ")
if User.objects.filter(username=username).exists():
self.stdout.write(
self.style.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!"
)
)
exit(-1)
# Create superuser
superuser = User()
superuser.username = username
superuser.is_superuser = True
superuser.is_staff = True
superuser.set_password(pw)
superuser.save()
self.stdout.write(
self.style.SUCCESS(
"Superuser {} created".format(username)
)
)
self.__break_line()
def __init_test_organisation(self):
""" Creates test organisations from predefined data
Returns:
"""
self.stdout.write(
self.style.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()
def __init_role_groups(self):
""" Creates test role groups from predefined data
Returns:
"""
self.stdout.write(
self.style.WARNING(
"--- Role Groups ---"
)
)
for group_data in TEST_ROLE_GROUPS_DATA:
group_data["organisation"] = Organisation.objects.get(name=group_data["organisation"])
group_data["role"] = RoleType.objects.get(type=group_data["role"])
group = RoleGroup.objects.get_or_create(
**group_data
)[0]
self.stdout.write(
self.style.SUCCESS(
CREATED_TEMPLATE.format(group.name)
)
)
self.__break_line()
def __break_line(self):
""" Simply prints a line break
Returns:
"""
self.stdout.write("\n")

View File

@@ -0,0 +1,49 @@
"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 15.12.20
"""
TEST_ORGANISATION_DATA = [
{
"name": "Test_Official_1",
"is_active": True,
"is_deleted": False,
},
{
"name": "Test_Official_2",
"is_active": True,
"is_deleted": False,
},
{
"name": "Test_NGO_1",
"is_active": True,
"is_deleted": False,
},
{
"name": "Test_Company_1",
"is_active": True,
"is_deleted": False,
},
]
TEST_ROLE_GROUPS_DATA = [
{
"name": "Registration office Test_Official_1",
"organisation": "Test_Official_1",
},
{
"name": "Licensing authority Test_Official_1",
"organisation": "Test_Official_1",
},
{
"name": "Dataprovider Test_Official_2",
"organisation": "Test_Official_2",
},
{
"name": "Dataprovider Test_Company_1",
"organisation": "Test_Company_1",
},
]