Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
snipt/utils/forms.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
44 lines (38 sloc)
1.58 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
from django.contrib.auth.models import User | |
from django.utils.translation import ugettext_lazy as _ | |
from registration.forms import RegistrationForm | |
class SniptRegistrationForm(RegistrationForm): | |
""" | |
Subclass of ``RegistrationForm`` which enforces uniqueness of | |
email addresses and further restricts usernames. | |
""" | |
def clean_username(self): | |
""" | |
Validate that the username is alphanumeric and is not already | |
in use. | |
""" | |
existing = User.objects.filter(username__iexact=self.cleaned_data["username"]) | |
if existing.exists(): | |
raise forms.ValidationError(_("A user with that username already exists.")) | |
elif "@" in self.cleaned_data["username"]: | |
raise forms.ValidationError(_("Cannot have '@' in username.")) | |
elif "." in self.cleaned_data["username"]: | |
raise forms.ValidationError(_("Cannot have '.' in username.")) | |
elif "+" in self.cleaned_data["username"]: | |
raise forms.ValidationError(_("Cannot have '+' in username.")) | |
else: | |
return self.cleaned_data["username"] | |
def clean_email(self): | |
""" | |
Validate that the supplied email address is unique for the | |
site. | |
""" | |
if User.objects.filter(email__iexact=self.cleaned_data["email"]): | |
raise forms.ValidationError( | |
_( | |
"""This email address is already in use. Please supply a | |
different email address.""" | |
) | |
) | |
return self.cleaned_data["email"] |