diff --git a/konova/decorators.py b/konova/decorators.py index 5c4795ca..f4a3ae8a 100644 --- a/konova/decorators.py +++ b/konova/decorators.py @@ -13,6 +13,8 @@ from django.shortcuts import redirect from django.urls import reverse from django.utils.translation import gettext_lazy as _ +from konova.settings import DEFAULT_GROUP, ETS_GROUP, ZB_GROUP + def staff_required(function): """ @@ -41,4 +43,64 @@ def superuser_required(function): else: messages.info(request, _("You need to be administrator to perform this action!")) return redirect(request.META.get("HTTP_REFERER", reverse("home"))) + return wrap + + +def default_group_required(function): + """ + A decorator for functions which shall only be usable for users of specific groups. + Group identifiers can be found in konova/settings.py + + """ + @wraps(function) + def wrap(request, *args, **kwargs): + user = request.user + has_group = user.groups.filter( + name=DEFAULT_GROUP + ).exists() + if has_group: + return function(request, *args, **kwargs) + else: + messages.info(request, _("You need to be part of another user group.")) + return redirect(request.META.get("HTTP_REFERER", reverse("home"))) + return wrap + + +def registration_office_group_required(function): + """ + A decorator for functions which shall only be usable for users of specific groups. + Group identifiers can be found in konova/settings.py + + """ + @wraps(function) + def wrap(request, *args, **kwargs): + user = request.user + has_group = user.groups.filter( + name=ZB_GROUP + ).exists() + if has_group: + return function(request, *args, **kwargs) + else: + messages.info(request, _("You need to be part of another user group.")) + return redirect(request.META.get("HTTP_REFERER", reverse("home"))) + return wrap + + +def conservation_office_group_required(function): + """ + A decorator for functions which shall only be usable for users of specific groups. + Group identifiers can be found in konova/settings.py + + """ + @wraps(function) + def wrap(request, *args, **kwargs): + user = request.user + has_group = user.groups.filter( + name=ETS_GROUP + ).exists() + if has_group: + return function(request, *args, **kwargs) + else: + messages.info(request, _("You need to be part of another user group.")) + return redirect(request.META.get("HTTP_REFERER", reverse("home"))) return wrap \ No newline at end of file diff --git a/konova/management/commands/setup_data.py b/konova/management/commands/setup_data.py index 659032f1..b135bd10 100644 --- a/konova/management/commands/setup_data.py +++ b/konova/management/commands/setup_data.py @@ -27,13 +27,13 @@ TEST_ORGANISATION_DATA = [ GROUPS_DATA = [ { - "name": DEFAULT_GROUP, + "name": _(DEFAULT_GROUP), }, { - "name": ZB_GROUP, + "name": _(ZB_GROUP), }, { - "name": ETS_GROUP, + "name": _(ETS_GROUP), }, ] diff --git a/konova/models.py b/konova/models.py index 86587627..5bf5b2a8 100644 --- a/konova/models.py +++ b/konova/models.py @@ -11,8 +11,6 @@ from django.contrib.auth.models import User from django.contrib.gis.db.models import MultiPolygonField from django.db import models -from konova.enums import ServerMessageImportance - class BaseResource(models.Model): """ diff --git a/konova/settings.py b/konova/settings.py index fc70de48..c43da632 100644 --- a/konova/settings.py +++ b/konova/settings.py @@ -52,9 +52,9 @@ DEFAULT_LON = 7.00 DEFAULT_ZOOM = 8.0 # GROUPS -DEFAULT_GROUP = _("Default") -ZB_GROUP = _("Registration office") -ETS_GROUP = _("Conservation office") +DEFAULT_GROUP = "Default" +ZB_GROUP = "Registration office" +ETS_GROUP = "Conservation office" # ServerMessageImportance bootstrap resolver SVI_BOOTSTRAP_CLS_MAP = {