from django import forms from django.contrib.auth.models import User from .models import Profile # --- # Profile edition # --- class UserForm(forms.ModelForm): class Meta: model = User fields = ["first_name", "last_name"] class ProfileForm(forms.ModelForm): class Meta: model = Profile fields = ["phone", "departement"] # --- # 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" ]