Initial
This commit is contained in:
153
kspneo/management/commands/setup.py
Normal file
153
kspneo/management/commands/setup.py
Normal file
@@ -0,0 +1,153 @@
|
||||
"""
|
||||
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.enums import RoleTypeEnum
|
||||
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_types()
|
||||
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_role_types(self):
|
||||
""" Initializes available role types according to RoleTypeEnum
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
self.stdout.write(
|
||||
self.style.WARNING(
|
||||
"--- Role types ---"
|
||||
)
|
||||
)
|
||||
for role_type_enum in RoleTypeEnum:
|
||||
role_type = RoleType.objects.get_or_create(
|
||||
type=role_type_enum.value
|
||||
)[0]
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS(
|
||||
CREATED_TEMPLATE.format(role_type.type)
|
||||
)
|
||||
)
|
||||
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")
|
||||
58
kspneo/management/commands/setup_test_data.py
Normal file
58
kspneo/management/commands/setup_test_data.py
Normal file
@@ -0,0 +1,58 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 15.12.20
|
||||
|
||||
"""
|
||||
from organisation.enums import OrganisationTypeEnum, RoleTypeEnum
|
||||
|
||||
TEST_ORGANISATION_DATA = [
|
||||
{
|
||||
"name": "Test_Official_1",
|
||||
"is_active": True,
|
||||
"is_deleted": False,
|
||||
"type": OrganisationTypeEnum.OFFICIAL.value,
|
||||
},
|
||||
{
|
||||
"name": "Test_Official_2",
|
||||
"is_active": True,
|
||||
"is_deleted": False,
|
||||
"type": OrganisationTypeEnum.OFFICIAL.value,
|
||||
},
|
||||
{
|
||||
"name": "Test_NGO_1",
|
||||
"is_active": True,
|
||||
"is_deleted": False,
|
||||
"type": OrganisationTypeEnum.NGO.value,
|
||||
},
|
||||
{
|
||||
"name": "Test_Company_1",
|
||||
"is_active": True,
|
||||
"is_deleted": False,
|
||||
"type": OrganisationTypeEnum.COMPANY.value,
|
||||
},
|
||||
]
|
||||
|
||||
TEST_ROLE_GROUPS_DATA = [
|
||||
{
|
||||
"name": "Registration office Test_Official_1",
|
||||
"organisation": "Test_Official_1",
|
||||
"role": RoleTypeEnum.REGISTRATIONOFFICE.value,
|
||||
},
|
||||
{
|
||||
"name": "Licensing authority Test_Official_1",
|
||||
"organisation": "Test_Official_1",
|
||||
"role": RoleTypeEnum.LICENSINGAUTHORITY.value,
|
||||
},
|
||||
{
|
||||
"name": "Dataprovider Test_Official_2",
|
||||
"organisation": "Test_Official_2",
|
||||
"role": RoleTypeEnum.LICENSINGAUTHORITY.value,
|
||||
},
|
||||
{
|
||||
"name": "Dataprovider Test_Company_1",
|
||||
"organisation": "Test_Company_1",
|
||||
"role": RoleTypeEnum.DATAPROVIDER.value,
|
||||
},
|
||||
]
|
||||
Reference in New Issue
Block a user