mpeltriaux
02970b19b4
* refactors django User model to custom User model to provide further attributes and methods directly on the user model
116 lines
3.4 KiB
Python
116 lines
3.4 KiB
Python
"""
|
|
Author: Michel Peltriaux
|
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
|
Created on: 08.07.21
|
|
|
|
"""
|
|
from django import forms
|
|
from django.urls import reverse
|
|
from django.utils.translation import gettext_lazy as _
|
|
from user.models import User
|
|
|
|
from konova.forms import BaseForm, BaseModalForm
|
|
from user.models import UserNotification
|
|
|
|
|
|
class UserNotificationForm(BaseForm):
|
|
""" Form for changing the notification settings of a user
|
|
|
|
"""
|
|
notifications = forms.MultipleChoiceField(
|
|
label_suffix="",
|
|
label=_("Notifications"),
|
|
required=False, # allow total disabling of all notifications
|
|
help_text=_("Select the situations when you want to receive a notification"),
|
|
widget=forms.CheckboxSelectMultiple(
|
|
attrs={
|
|
"class": "list-unstyled",
|
|
}
|
|
),
|
|
choices=[]
|
|
)
|
|
|
|
def __init__(self, user: User, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.user = user
|
|
self.form_title = _("Edit notifications")
|
|
self.form_caption = _("")
|
|
self.action_url = reverse("user:notifications")
|
|
self.cancel_redirect = reverse("user:index")
|
|
|
|
# Insert all notifications into form field by creating choices as tuples
|
|
notifications = UserNotification.objects.filter(
|
|
is_active=True,
|
|
)
|
|
choices = []
|
|
for n in notifications:
|
|
choices.append(
|
|
(n.id, _(n.name))
|
|
)
|
|
self.fields["notifications"].choices = choices
|
|
|
|
users_current_notifications = self.user.notifications.all()
|
|
users_current_notifications = [str(n.id) for n in users_current_notifications]
|
|
self.fields["notifications"].initial = users_current_notifications
|
|
|
|
def save(self):
|
|
""" Stores the changes in the user konova_extension
|
|
|
|
Returns:
|
|
|
|
"""
|
|
selected_notification_ids = self.cleaned_data.get("notifications", [])
|
|
notifications = UserNotification.objects.filter(
|
|
id__in=selected_notification_ids,
|
|
)
|
|
self.user.notifications.set(notifications)
|
|
|
|
|
|
class UserContactForm(BaseModalForm):
|
|
name = forms.CharField(
|
|
label=_("Username"),
|
|
label_suffix="",
|
|
required=False,
|
|
widget=forms.TextInput(
|
|
attrs={
|
|
"readonly": True,
|
|
"class": "form-control",
|
|
}
|
|
),
|
|
)
|
|
person_name = forms.CharField(
|
|
label=_("Person name"),
|
|
label_suffix="",
|
|
required=False,
|
|
widget=forms.TextInput(
|
|
attrs={
|
|
"readonly": True,
|
|
"class": "form-control",
|
|
}
|
|
),
|
|
)
|
|
mail = forms.EmailField(
|
|
label=_("E-Mail"),
|
|
label_suffix="",
|
|
required=False,
|
|
widget=forms.TextInput(
|
|
attrs={
|
|
"readonly": True,
|
|
"class": "form-control",
|
|
}
|
|
),
|
|
)
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.render_submit = False
|
|
self.form_title = _("User contact data")
|
|
self.form_caption = ""
|
|
|
|
self.initialize_form_field("name", self.instance.username)
|
|
self.initialize_form_field("person_name", "{} {}".format(self.instance.first_name, self.instance.last_name))
|
|
self.initialize_form_field("mail", self.instance.email)
|
|
|
|
|