forked from Gluejar/regluit
-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathforms.py
120 lines (106 loc) · 4.41 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import logging
from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import AuthenticationForm, PasswordResetForm
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from registration.forms import RegistrationForm
from .emailcheck import is_disposable
from .models import Library
logger = logging.getLogger(__name__)
class UserData(forms.Form):
username = forms.RegexField(
label=_("New Username"),
max_length=30,
regex=r'^[\w.@+-]+$',
help_text = _("30 characters or fewer."),
error_messages = {
'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")
}
)
oldusername = None
allow_same = False
def clean_username(self):
username = self.data["username"]
if username != self.oldusername:
users = User.objects.filter(username__iexact=username)
for user in users:
raise forms.ValidationError(_("Another user with that username already exists."))
return username
if not self.allow_same:
raise forms.ValidationError(_("Your username is already "+username))
class UserNamePass(UserData):
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."))
allow_same = True
def clean_password2(self):
password1 = self.cleaned_data.get("password1", "")
password2 = self.cleaned_data["password2"]
if password1 != password2:
raise forms.ValidationError(_("The two passwords don't match."))
return password2
class RegistrationFormNoDisposableEmail(RegistrationForm):
def clean_email(self):
"""
Check the supplied email address against a list of known disposable
webmail domains.
"""
logger.info('cleaning email')
if is_disposable(self.cleaned_data['email']):
raise forms.ValidationError(_("Please supply a permanent email address."))
return self.cleaned_data['email']
class AuthForm(AuthenticationForm):
def __init__(self, request=None, *args, **kwargs):
if request and request.method == 'GET':
saved_un= request.COOKIES.get('un', None)
super(AuthForm, self).__init__(initial={"username":saved_un},*args, **kwargs)
else:
super(AuthForm, self).__init__(*args, **kwargs)
class SocialAwarePasswordResetForm(PasswordResetForm):
def get_users(self, email):
"""
Send the reset form even if the user password is not usable
"""
active_users = get_user_model()._default_manager.filter(
email__iexact=email, is_active=True)
return active_users
def clean_email(self):
email = self.cleaned_data['email']
if not get_user_model().objects.filter(email__iexact=email, is_active=True).exists():
raise forms.ValidationError("There aren't ungluers with that email address!")
return email
class NewLibraryForm(forms.ModelForm):
username = forms.RegexField(
label=_("Library Username"),
max_length=30,
regex=r'^[\w.@+-]+$',
help_text = _("30 characters or fewer."),
error_messages = {
'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")
},
initial = '',
)
email = forms.EmailField(
label=_("notification email address for library"),
max_length=100,
error_messages={'required': 'Please enter an email address for the library.'},
)
def clean_username(self):
username= self.cleaned_data['username']
try:
user = User.objects.get(username=username)
raise forms.ValidationError(_("That username is already in use, please choose another."))
except User.DoesNotExist:
self.instance.user = User(username=username)
return username
class Meta:
model = Library
fields = 'name', 'backend', 'email', 'username'
widgets = {'name':forms.TextInput(attrs={'size':'40'})}
class LibraryForm(forms.ModelForm):
class Meta:
model = Library
fields = 'name', 'backend',