FloriCore/member/forms.py
Michael Bergbauer 1e720228be Reworking account handling
* allowing role management
* allowing user change without the need to update the password
2025-07-19 09:08:40 +02:00

48 lines
1.4 KiB
Python

# forms.py
from django import forms
from .models import Person, UserAccount
class PersonForm(forms.ModelForm):
class Meta:
model = Person
fields = ['vorname', 'nachname', 'geburtsdatum', 'aktiv']
widgets = {
'geburtsdatum': forms.DateInput(attrs={'type': 'date'}),
}
class AccountForm(forms.ModelForm):
password=forms.CharField(widget=forms.PasswordInput(), required=False)
confirm_password=forms.CharField(widget=forms.PasswordInput(), required=False)
class Meta:
model = UserAccount
fields = ['username', 'password', 'role']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.instance.pk is None:
self.fields['password'].required=True
self.fields['confirm_password'].required=True
def clean(self):
cleaned_data = super(AccountForm, self).clean()
passwordStr = cleaned_data.get("password")
confirm_passwordStr = cleaned_data.get("confirm_password")
if passwordStr != confirm_passwordStr:
raise forms.ValidateError("password and confirm_password does not match")
def save(self, commit=True):
user = super().save(commit=False)
pw = self.cleaned_data.get("password")
if pw:
user.set_password(pw)
if commit:
user.save()
return user