diff --git a/konova/management/commands/setup.py b/konova/management/commands/setup.py index 58e93355..7df15c1f 100644 --- a/konova/management/commands/setup.py +++ b/konova/management/commands/setup.py @@ -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") \ No newline at end of file + 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 + ) + ) \ No newline at end of file diff --git a/konova/management/commands/setup_data.py b/konova/management/commands/setup_data.py new file mode 100644 index 00000000..e2c6da35 --- /dev/null +++ b/konova/management/commands/setup_data.py @@ -0,0 +1,61 @@ +""" +Author: Michel Peltriaux +Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany +Contact: michel.peltriaux@sgdnord.rlp.de +Created on: 15.12.20 + +""" +from django.utils.translation import gettext_lazy as _ + +from konova.settings import ADD_INTERVENTION, CREATE_SHARE_LINK, DELETE_ECOACCOUNT, EDIT_ECOACCOUNT, ADD_ECOACCOUNT, \ + DELETE_COMPENSATION, EDIT_COMPENSATION, ADD_COMPENSATION, DELETE_INTERVENTION, EDIT_INTERVENTION, VALIDITY_CHECK, \ + DELETE_EMA, EDIT_EMA, ADD_EMA, RECORD_DATA + +TEST_ORGANISATION_DATA = [ + { + "name": "Test_Official_1", + }, + { + "name": "Test_Official_2", + }, + { + "name": "Test_NGO_1", + }, + { + "name": "Test_Company_1", + }, + ] + +# Groups permissions are declared in konova/settings.py +GROUPS_DATA = [ + { + "name": _("Default"), + "permissions": [ + ADD_INTERVENTION, + EDIT_INTERVENTION, + DELETE_INTERVENTION, + ADD_COMPENSATION, + EDIT_COMPENSATION, + DELETE_COMPENSATION, + ADD_ECOACCOUNT, + EDIT_ECOACCOUNT, + DELETE_ECOACCOUNT, + CREATE_SHARE_LINK, + ] + }, + { + "name": _("Registration office"), + "permissions": [ + VALIDITY_CHECK, + ] + }, + { + "name": _("Conservation office"), + "permissions": [ + RECORD_DATA, + ADD_EMA, + EDIT_EMA, + DELETE_EMA, + ] + }, + ] \ No newline at end of file diff --git a/konova/management/commands/setup_test_data.py b/konova/management/commands/setup_test_data.py deleted file mode 100644 index e31a53c8..00000000 --- a/konova/management/commands/setup_test_data.py +++ /dev/null @@ -1,49 +0,0 @@ -""" -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", - }, - ] \ No newline at end of file diff --git a/konova/models.py b/konova/models.py index 5bf5b2a8..689a46e3 100644 --- a/konova/models.py +++ b/konova/models.py @@ -7,7 +7,7 @@ Created on: 17.11.20 """ import uuid -from django.contrib.auth.models import User +from django.contrib.auth.models import User, Group from django.contrib.gis.db.models import MultiPolygonField from django.db import models @@ -43,6 +43,25 @@ class BaseObject(BaseResource): abstract = True +class KonovaPermission(models.Model): + """ + Custom permissions for konova + + """ + name = models.CharField(max_length=255, blank=False, null=False) + description = models.TextField() + is_active = models.BooleanField(default=True) + + +class KonovaGroup(models.Model): + """ + Custom group model + + """ + name = models.CharField(max_length=255, null=False, blank=False) + permissions = models.ManyToManyField(KonovaPermission) + + class Deadline(BaseResource): """ Defines a deadline, which can be used to define dates with a semantic meaning diff --git a/konova/settings.py b/konova/settings.py index 1cd573b5..141a0be4 100644 --- a/konova/settings.py +++ b/konova/settings.py @@ -9,6 +9,7 @@ https://docs.djangoproject.com/en/3.1/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/3.1/ref/settings/ """ +from django.utils.translation import gettext_lazy as _ # Load other settings from konova.sub_settings.django_settings import * @@ -51,25 +52,22 @@ DEFAULT_ZOOM = 8.0 # PERMISSION VALUES ## Group default -ADD_INTERVENTION = "add_intervention" -EDIT_INTERVENTION = "edit_intervention" -DELETE_INTERVENTION = "delete_intervention" - -ADD_COMPENSATION = "add_compensation" -EDIT_COMPENSATION = "edit_compensation" -DELETE_COMPENSATION = "delete_compensation" - -ADD_ECOACCOUNT = "add_ecoaccount" -EDIT_ECOACCOUNT = "edit_ecoaccount" -DELETE_ECOACCOUNT = "delete_ecoaccount" - -CREATE_SHARE_LINK = "create_share_link" +ADD_INTERVENTION = ("add_intervention", _("Add new intervention")) +EDIT_INTERVENTION = ("edit_intervention", _("Edit intervention")) +DELETE_INTERVENTION = ("delete_intervention", _("Delete intervention")) +ADD_COMPENSATION = ("add_compensation", _("Add new compensation")) +EDIT_COMPENSATION = ("edit_compensation", _("Edit compensation")) +DELETE_COMPENSATION = ("delete_compensation", _("Delete compensation")) +ADD_ECOACCOUNT = ("add_ecoaccount", _("Add new eco account")) +EDIT_ECOACCOUNT = ("edit_ecoaccount", _("Edit eco account")) +DELETE_ECOACCOUNT = ("delete_ecoaccount", _("Delete eco account")) +CREATE_SHARE_LINK = ("create_share_link", _("Create share link")) ## Group ZB -VALIDITY_CHECK = "validity_check" +VALIDITY_CHECK = ("validity_check", _("Confirm check on data")) ## Group ETS -RECORD_DATA = "record_data" # refers to 'verzeichnen' -ADD_EMA = "add_ema" -EDIT_EMA = "edit_ema" -DELETE_EMA = "delete_ema" +RECORD_DATA = ("record_data", _("Record data")) # refers to 'verzeichnen' +ADD_EMA = ("add_ema", _("Add new EMA")) +EDIT_EMA = ("edit_ema", _("Edit EMA")) +DELETE_EMA = ("delete_ema", _("Delete EMA"))