Refactoring to konova
This commit is contained in:
@@ -9,14 +9,8 @@ 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):
|
||||
"""
|
||||
@@ -97,45 +91,3 @@ class RemoveForm(BaseForm):
|
||||
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
|
||||
Reference in New Issue
Block a user