FloriCore/member/forms.py

48 lines
1.4 KiB
Python
Raw Permalink Normal View History

# forms.py
from django import forms
2025-07-05 11:49:07 +02:00
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'}),
}
2025-07-05 11:49:07 +02:00
class AccountForm(forms.ModelForm):
password=forms.CharField(widget=forms.PasswordInput(), required=False)
confirm_password=forms.CharField(widget=forms.PasswordInput(), required=False)
2025-07-05 11:49:07 +02:00
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
2025-07-05 11:49:07 +02:00
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
2025-07-05 11:49:07 +02:00