poulpe/user/forms.py

37 lines
1.2 KiB
Python
Raw Normal View History

2017-02-18 01:45:22 +01:00
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.conf import settings
class CreateUserForm(UserCreationForm):
key = forms.CharField(
2017-02-21 17:25:46 +01:00
label="Clef de sécurité",
2017-02-18 01:45:22 +01:00
widget=forms.PasswordInput,
2017-02-21 17:25:46 +01:00
help_text="Cette clef est fournie par l'administrat·rice·eur "
2017-02-18 01:45:22 +01:00
"du site. Pour en obtenir une veuillez la-le contacter."
)
2017-02-21 17:25:46 +01:00
error_m = {'wrong_key': "La clef fournie est erronée."}
2017-02-18 01:45:22 +01:00
class Meta:
model = User
fields = ('username', 'first_name', 'last_name',
'email', 'password1', 'password2',)
def save(self, commit=True):
user = super(CreateUserForm, self).save(commit=False)
user.email = self.cleaned_data["email"]
user.first_name = self.cleaned_data["first_name"]
user.last_name = self.cleaned_data["last_name"]
if commit:
user.save()
return user
def clean_key(self):
key = self.cleaned_data.get("key")
if key != settings.CREATE_USER_KEY:
raise forms.ValidationError(
self.error_m['wrong_key'],
code='wrong_key')
return key