Conditional Django form validation - python

For a Django project, I have a customized User model:
class User(AbstractUser):
username = None
email = models.EmailField(_('e-mail address'),
unique=True)
first_name = models.CharField(_('first name'),
max_length=150,
blank=False)
last_name = models.CharField(_('last name'),
max_length=150,
blank=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name']
objects = UserManager()
def __str__(self):
return self.email
I'm creating a new user registration form:
class UserRegistrationForm(forms.ModelForm):
auto_password = forms.BooleanField(label=_('Generate password and send by mail'),
required=False,
initial=True)
password = forms.CharField(label=_('Password'),
widget=forms.PasswordInput)
password2 = forms.CharField(label=_('Repeat password'),
widget=forms.PasswordInput)
class Meta:
model = User
fields = ('email', 'first_name', 'last_name', 'is_staff',
'is_superuser')
def clean_password2(self):
cd = self.cleaned_data
if cd['password'] != cd['password2']:
raise forms.ValidationError(_("Passwords don't match."))
return cd['password2']
My form has a auto_password boolean field. When this checkbox is set, the password and password2 fields must not be checked, as their content (or the absence of content) has no importance. On the opposite, when the auto_password checkbox is unset, the password and password2 must be checked.
Is there a way to optionnally disable the Django form checks whenever needed?
Thanks for the help.

You add this to the the condition in the clean method:
class UserRegistrationForm(forms.ModelForm):
auto_password = forms.BooleanField(
label=_('Generate password and send by mail'),
required=False,
initial=True
)
password = forms.CharField(
label=_('Password'),
widget=forms.PasswordInput
)
password2 = forms.CharField(
label=_('Repeat password'),
widget=forms.PasswordInput
)
class Meta:
model = User
fields = ('email', 'first_name', 'last_name', 'is_staff',
'is_superuser')
def clean(self):
data = super().clean()
if not data['auto_password'] and data['password'] != data['password2']:
raise forms.ValidationError(_('Passwords don't match.'))
return data
The not data['auto_password'] will thus return False in case the checkbox is checked, and in that case the the check of data['password'] != data['password2'] will not run, nor will it raise a ValidationError.
You can also remove the required=True properties, and check if the password contains at least one character by checking it truthiness:
class UserRegistrationForm(forms.ModelForm):
auto_password = forms.BooleanField(
label=_('Generate password and send by mail'),
# no required=True
initial=True
)
password = forms.CharField(
label=_('Password'),
widget=forms.PasswordInput
)
password2 = forms.CharField(
label=_('Repeat password'),
widget=forms.PasswordInput
)
class Meta:
model = User
fields = ('email', 'first_name', 'last_name', 'is_staff',
'is_superuser')
def clean(self):
data = super().clean()
manual = not data['auto_password']
if manual and not data['password']:
raise forms.ValidationError(_('Password is empty.'))
if manual and data['password'] != data['password2']:
raise forms.ValidationError(_('Passwords don't match.'))
return data

Can't you just include it in your logic?
if not cd['auto_password'] and (cd['password'] != cd['password2']):
raise forms.ValidationError(_("Passwords don't match."))

Related

How to make sequential signup pages with Django allauth?

I currently have a single page signup form implemented with allauth
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
email = models.EmailField(_('Professional email address'), unique=True)
username = models.CharField(_("User Name"), blank=False, max_length=255, unique=True)
first_name = models.CharField(_("First Name"), null=True, max_length=255, default='')
last_name = models.CharField(_("Last Name"), null=True, max_length=255, default='')
country = CountryField(_("Country of Practice"), blank_label='(Country of Practice)', blank = False, default='GB')
terms = models.BooleanField(verbose_name=_('I have read and agree to the terms and conditions'), default=False)
def get_absolute_url(self):
return reverse(
"users:detail", kwargs={"username": self.username}
)
objects = UserManager()
And this is the forms.py
class UserCreationForm(forms.UserCreationForm):
error_message = forms.UserCreationForm.error_messages.update(
{"duplicate_username": _("This username has already been taken.")}
)
username = CharField(label='User Name',
widget=TextInput(attrs={'placeholder': 'User Name'}))
class Meta(forms.UserCreationForm.Meta):
model = User
fields = ['username', 'email', 'first_name', 'last_name', 'password1', 'password2', 'terms']
field_order = ['username', 'email', 'first_name', 'last_name', 'password1', 'password2', 'terms']
def clean_terms(self):
is_filled = self.cleaned_data['terms']
if not is_filled:
raise forms.ValidationError('This field is required')
return is_filled
def clean_username(self):
username = self.cleaned_data["username"]
if self.instance.username == username:
return username
try:
User._default_manager.get(username=username)
except User.DoesNotExist:
return username
raise ValidationError(
self.error_messages["duplicate_username"]
)
I would like however for the first sign up page to have a ‘next’ button at the bottom and then there would be a second page where the user input separate details (the data input here might vary based on the inputs in the first page). The Django ‘form tools’ form wizard seems well suite to this but I can’t work out how to integrate it with all auth
Any suggestions much appreciated

'super' object has no attribute 'clean_password1'

I'm trying to reuse a code for registration form from my older project. The problem is that Django says that UserCreationForm hasn't attribute clean_password1.
Do you know where is the problem? I see that there is not such attribute in UserCreationForm but it worked before.
What should I do to make it work?
EDIT: It's because it calls super(...).clean_password1 which isn't in super class. So I've tried to put there super(...).cleaned_data.get("password1"), but it raises error that cleaned_data isn't there (in superclass).
class UserProfileCreationForm(UserCreationForm):
username = forms.CharField(label="Username", max_length=40, min_length=5)
email = forms.EmailField(label="Email address", max_length=40)
first_name = forms.CharField(label='First name', max_length=40, required=False)
last_name = forms.CharField(label='Last name', max_length=40, required=False)
password1 = forms.CharField(label="Password", widget=forms.PasswordInput, min_length=5)
password2 = forms.CharField(label="Password confirmation", widget=forms.PasswordInput)
class Meta():
model = UserProfile
fields = (
'username', 'email', 'type_of_user', 'first_name', 'last_name', 'password1', 'password2','IBAN',
)
def clean_password1(self):
password = self.cleaned_data.get('password1')
if len(password) < 8:
raise ValidationError('Password has to be of size at least 8 characters')
return super(UserProfileCreationForm, self).clean_password1()
def clean_password2(self):
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError("Password mismatch")
return password2
def save(self, commit=True):
try:
with transaction.atomic():
user = User(username=self.cleaned_data['username'],
first_name=self.cleaned_data['first_name'],
last_name=self.cleaned_data['last_name'],
email=self.cleaned_data['email'])
user.save()
user.set_password(self.cleaned_data["password1"])
user_profile = UserProfile(user=user,
IBAN=self.cleaned_data['IBAN'],
type_of_user=self.cleaned_data['type_of_user']
)
user_profile.save()
except:
raise #TODO: HANDLE THE EXCEPTION
return user
You can safely remove clean_password2 - Django already validates if the passwords match (I assume you're using the current version for your new project). As for clean_password1 I'd recommend to remove it too and read documentation on password validation (new in Django 1.9).
There's no reason to call super inside a clean_field method; the field doesn't exist on the base class, so neither does the clean_field.

How to eliminate username from authentication

I want to eliminate the concept of a username from the django auth in my app. I want the email of the user to take place of the username. So I extended AbstractBaseUser with the code below
class AuthUserManager(BaseUserManager):
def create_user(self, email, password=None):
if not email:
raise ValueError("Users must have an email address")
user = self.model(email=self.normalize_email(email),)
user.is_active = True
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, password):
user = self.create_user(email=email, password=password)
user.is_staff = True
user.is_superuser = True
user.save(using=self._db)
return user
class AuthUser(AbstractBaseUser, PermissionsMixin):
alphanumeric = RegexValidator(r'^[0-9a-zA-Z]*$', message = "Only alphanumeric characters are allowed")
email = models.EmailField(verbose_name='email address',unique=True, max_length=255)
first_name = models.CharField(unique=True, max_length=20, validators=[alphanumeric])
last_name = models.CharField(unique=True, max_length=20, validators=[alphanumeric])
date_joined = models.DateTimeField(auto_now_add=True)
is_active = models.BooleanField(default=True, null=False)
is_staff = models.BooleanField(default=False, null=False)
#custom fields
company_name = models.CharField(max_length=100)
telephone_number = models.CharField(max_length=100)
website_url = models.CharField(max_length=100)
objects = AuthUserManager()
USERNAME_FIELD = 'email'
#REQUIRED_FIELDS = ['first_name']
def get_full_name(self):
fullname = self.first_name + self.last_name
return fullname
def get_short_name(self):
return self.email
def __unicode__(self):
return self.email
In my serializers.py I have this
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = AuthUser
fields = ('email', 'first_name', 'last_name')
def create(self, validated_data):
user = AuthUser.objects.create(**validated_data)
return user
def update(self, instance, validated_data):
instance.email = validated_data.get('email', instance.email)
instance.first_name = validated_data.get('first_name', instance.first_name)
instance.last_name = validated_data.get('last_name', instance.last_name)
instance.save()
return instance
But when I try to register I get the following error:
{
"username": [
"This field is required."
]
}
What am I doing wrong?
In your serializer, you need to exclude username field, because you are inheriting from AbstractBaseUser and you still have 'username' field, so add this exclude = ('username',)
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = AuthUser
fields = ('email', 'first_name', 'last_name')
exclude = ('username',)
def create(self, validated_data):
user = AuthUser.objects.create(**validated_data)
return user

Django custom user

I created custom user
class CoinUser(AbstractBaseUser):
username = models.CharField(_('username'), max_length=30, unique=True,
help_text=_('Required. 30 characters or fewer. Letters, numbers and #/./+/-/_ characters'),
validators=[
validators.RegexValidator(re.compile('^[\w.#+-]+$'), _('Enter a valid username.'), _('invalid'))
])
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
first_name = models.CharField(_('first name'), max_length=30, blank=True, null=True)
patronymic = models.CharField(_('patronymic'), max_length=30,
help_text=_('Required. 30 characters or fewer. Letters, numbers and #/./+/-/_ characters'))
last_name = models.CharField(_('last name'), max_length=30, blank=True, null=True)
email = models.EmailField(_('email address'), max_length=255, unique=True,
validators=[
validators.RegexValidator(re.compile('^.+#.+\..+$', flags=re.IGNORECASE), _('Enter a valid email.'), _('invalid'))
])
post_code = models.CharField(_('post code'), max_length=10)
is_staff = models.BooleanField(_('staff status'), default=False,
help_text=_('Designates whether the user can log into this admin site.'))
is_active = models.BooleanField(_('active'), default=False,
help_text=_('Designates whether this user should be treated as active. Unselect this instead of deleting accounts.'))
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
phone = models.CharField(_('phone number'), max_length=12, blank=True, null=True, unique=True,
validators=[
validators.RegexValidator(re.compile('^[0-9]{12,12}$', flags=re.IGNORECASE), _('Enter a valid phone number.'), _('invalid'))
])
objects = CoinUserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email', 'phone']
def get_full_name(self):
full_name = '%s %s' % (self.first_name, self.last_name)
return full_name.strip()
def get_short_name(self):
return self.first_name
def __str__(self):
return self.username
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return True
#property
def is_staff(self):
return self.is_admin
class Meta:
managed = True
db_table = "co_user"
verbose_name = _("Coin user")
verbose_name_plural = _("Coin users")
then I created UserAdmin
class CoinUserAdmin(UserAdmin):
form = UserChangeFrom
add_form = UserCreationForm
list_display = ('email', 'username', 'phone')
list_filter = ('is_admin',)
fieldsets = (
(None, {'fields': ('username',)}),
('Contact info', {'fields':('email','phone')}),
)
search_fields = ('username', 'email', 'phone')
ordering = ('username', 'email',)
filter_horizontal = ()
and added UserCreationForm
class UserCreationForm(forms.ModelForm):
"""docstring for UserCreationForm"""
password1 = forms.CharField(label=_('Password'), widget=forms.PasswordInput)
password2 = forms.CharField(label=_('Password confirmation'), widget=forms.PasswordInput)
class Meta:
model = CoinUser
fields = ('email', 'password', 'username', 'phone')
def clean_password2(self):
#Check that two password entires match
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError(_("Passwords don't match"))
return password2
def save(self, commit=True):
#Save the provided password in hashed format
user = super(UserCreationForm, self).save(commit=False)
# user.phone = self.cleaned_data["phone"]
user.set_password(self.cleaned_data["password1"])
if commit:
user.save()
return user
It works fine but when I tried to add email and phone
email = forms.CharField(label=_("Email"), widget=forms.EmailField)
phone = forms.CharField(label=_("Phone number"), widget=forms.TextInput)
fields to UserCreationForm they didn't view and when I tried to save my user without these field I got error "'EmailField' object has no attribute 'value_from_datadict'"
EmailField actually is form field. Try to use EmailInput widget.

keeping User.email unique from Django Admin

I am using Django 1.3/Python 2.7.
My application is using django.contrib.auth for User management. For the main user registration I have a form which checks if the 'email' has already been registered. So it makes sure all email fields are unique.
But it happens that some admins use the django admin interface to add users, and end up adding duplicate accounts with the same email.
For this purpose I have created a NewUserCreationForm, where I've duplicated the django.contrib.auth.UserCreationForm and added an 'email' field to it.
class NewUserCreationForm(forms.ModelForm):
"""
A form that creates a user, with no privileges, from the given username and password.
"""
username = forms.RegexField(label=_("Username"), max_length=30, regex=r'^[\w.#+-]+$',
help_text = _("Required. 30 characters or fewer. Letters, digits and #/./+/-/_ only."),
error_messages = {'invalid': _("This value may contain only letters, numbers and #/./+/-/_ characters.")})
password1 = forms.CharField(label=_("Password"), widget=forms.PasswordInput)
password2 = forms.CharField(label=_("Password confirmation"), widget=forms.PasswordInput,
help_text = _("Enter the same password as above, for verification."))
email = forms.EmailField(label=_("Email"), max_length=75)
class Meta:
model = User
fields = ("username", "email")
def clean_username(self):
username = self.cleaned_data["username"]
try:
User.objects.get(username=username)
except User.DoesNotExist:
return username
raise forms.ValidationError(_("A user with that username already exists."))
def clean_password2(self):
password1 = self.cleaned_data.get("password1", "")
password2 = self.cleaned_data["password2"]
if password1 != password2:
raise forms.ValidationError(_("The two password fields didn't match."))
return password2
def save(self, commit=True):
user = super(NewUserCreationForm, self).save(commit=False)
user.set_password(self.cleaned_data["password1"])
user.email = self.email
if commit:
user.save()
return user
class CustomUserAdmin(UserAdmin):
add_form = NewUserCreationForm
admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)
But this code isn't working. I can see the form rendering, but with no email field, just the Username, password1 and password2 fields.
what is it that I am missing here?
You are missing ... UserAdmin.add_fieldsets, e.g.:
class CustomUserAdmin(UserAdmin):
add_form = NewUserCreationForm
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username', 'password1', 'password2', 'email')}
),
)

Categories