gestioCOF/gestion/forms.py

66 lines
1.5 KiB
Python
Raw Normal View History

2017-02-10 23:12:12 +01:00
from django import forms
from django.contrib.auth.models import User
from .models import Profile
2017-06-23 00:44:06 +02:00
# ---
# Profile edition
# ---
2017-02-10 23:12:12 +01:00
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ["first_name", "last_name"]
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
2017-02-10 23:12:12 +01:00
fields = ["phone", "departement"]
2017-06-23 00:44:06 +02:00
# ---
# Registration
# ---
class UserRegistrationForm(forms.ModelForm):
passwd1 = forms.CharField(
label="Mot de passe",
widget=forms.PasswordInput,
required=False,
)
passwd2 = forms.CharField(
label="Confirmation de mot de passe",
widget=forms.PasswordInput,
required=False,
)
def clean_passwd2(self):
passwd1 = self.cleaned_data["passwd1"]
passwd2 = self.cleaned_data["passwd2"]
if passwd1 and passwd2:
if passwd1 != passwd2:
raise forms.ValidationError("Mots de passes différents")
return passwd2
def save(self, commit=True, *args, **kwargs):
user = super().save(commit, *args, **kwargs)
if self.cleaned_data["passwd2"]:
user.set_password(self.cleaned_data["passwd2"])
if commit:
user.save()
return user
class Meta:
model = User
fields = ["username", "first_name", "last_name", "email"]
class ProfileRegistrationForm(forms.ModelForm):
class Meta:
model = Profile
fields = [
"login_clipper", "phone", "occupation", "departement", "comments"
]