""" Author: Michel Peltriaux Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany Contact: michel.peltriaux@sgdnord.rlp.de Created on: 16.11.20 """ from abc import abstractmethod from django import forms from django.http import HttpRequest from django.urls import reverse from django.utils.translation import gettext_lazy as _ from konova.models import RoleGroup from konova.utils.session import set_session_user_role from organisation.settings import ROLE_TYPE_STRINGS class BaseForm(forms.Form): """ Basic form for that holds attributes needed in all other forms """ action_url = None form_title = None cancel_redirect = None form_caption = None instance = None # The data holding model object def __init__(self, *args, **kwargs): self.instance = kwargs.pop("instance", None) super().__init__(*args, **kwargs) @abstractmethod def save(self): # To be implemented in subclasses! pass def disable_form_field(self, field: str): """ Disables a form field for user editing """ self.fields[field].widget.attrs["readonly"] = True self.fields[field].disabled = True self.fields[field].widget.attrs["title"] = _("Not editable") def initialize_form_field(self, field: str, val): """ Initializes a form field with a value """ self.fields[field].initial = val def load_initial_data(self, form_data: dict, disabled_fields: list): """ Initializes form data from instance Inserts instance data into form and disables form fields Returns: """ if self.instance is None: return for k, v in form_data.items(): self.initialize_form_field(k, v) for field in disabled_fields: self.disable_form_field(field) class RemoveForm(BaseForm): check = forms.BooleanField( label=_("Confirm"), label_suffix=_(""), required=True, ) def __init__(self, *args, **kwargs): self.object_to_remove = kwargs.pop("object_to_remove", None) self.remove_post_url = kwargs.pop("remove_post_url", "") self.cancel_url = kwargs.pop("cancel_url", "") super().__init__(*args, **kwargs) self.form_title = _("Remove") if self.object_to_remove is not None: self.form_caption = _("You are about to remove {} {}").format(self.object_to_remove.__class__.__name__, self.object_to_remove) self.action_url = self.remove_post_url self.cancel_redirect = self.cancel_url def is_checked(self) -> bool: return self.cleaned_data.get("check", False) def save(self): if self.object_to_remove is not None and self.is_checked(): self.object_to_remove.is_active = False self.object_to_remove.is_deleted = True self.object_to_remove.save() return self.object_to_remove class ChangeUserRoleForm(BaseForm): """ Form for a user to change the current role """ role = forms.ChoiceField( label=_("You are working as"), label_suffix="", choices=[], widget=forms.Select( attrs={ "onchange": "submit();", } ) ) def __init__(self, *args, **kwargs): user = kwargs.pop("user", None) super().__init__(*args, **kwargs) self.action_url = reverse("home") self.cancel_redirect = reverse("home") role_groups = RoleGroup.get_users_role_groups(user) choices = [] for group in role_groups: choices.append( (group.id, "{} ({})".format(ROLE_TYPE_STRINGS.get(group.role.type, None), group.organisation)) ) self.fields["role"].choices = choices def save(self, request: HttpRequest) -> RoleGroup: """ Custom save method for storing the newly selected role Args: request (HttpRequest): Returns: """ role_group = RoleGroup.get_users_role_groups(request.user).get(id=self.cleaned_data.get("role", -1)) set_session_user_role(request, role_group) return role_group