This repository was archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathforms.py
44 lines (38 loc) · 1.58 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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"]