1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
from __future__ import unicode_literals |
3
|
|
|
""" |
4
|
|
|
Forms of django-inspectional-registration |
5
|
|
|
|
6
|
|
|
This is a modification of django-registration_ ``forms.py`` |
7
|
|
|
The original code is written by James Bennett |
8
|
|
|
|
9
|
|
|
.. _django-registration: https://bitbucket.org/ubernostrum/django-registration |
10
|
|
|
|
11
|
|
|
Original License:: |
12
|
|
|
|
13
|
|
|
Copyright (c) 2007-2011, James Bennett |
14
|
|
|
All rights reserved. |
15
|
|
|
|
16
|
|
|
Redistribution and use in source and binary forms, with or without |
17
|
|
|
modification, are permitted provided that the following conditions are |
18
|
|
|
met: |
19
|
|
|
|
20
|
|
|
* Redistributions of source code must retain the above copyright |
21
|
|
|
notice, this list of conditions and the following disclaimer. |
22
|
|
|
* Redistributions in binary form must reproduce the above |
23
|
|
|
copyright notice, this list of conditions and the following |
24
|
|
|
disclaimer in the documentation and/or other materials provided |
25
|
|
|
with the distribution. |
26
|
|
|
* Neither the name of the author nor the names of other |
27
|
|
|
contributors may be used to endorse or promote products derived |
28
|
|
|
from this software without specific prior written permission. |
29
|
|
|
|
30
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
31
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
32
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
33
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
34
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
35
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
36
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
37
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
38
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
39
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
40
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
41
|
|
|
""" |
42
|
|
|
__author__ = 'Alisue <[email protected]>' |
43
|
|
|
__all__ = ( |
44
|
|
|
'ActivationForm', 'RegistrationForm', |
45
|
|
|
'RegistrationFormNoFreeEmail', |
46
|
|
|
'RegistrationFormTermsOfService', |
47
|
|
|
'RegistrationFormUniqueEmail', |
48
|
|
|
) |
49
|
|
|
from django import forms |
50
|
|
|
from django.utils.translation import ugettext_lazy as _ |
51
|
|
|
from registration.compat import get_user_model |
52
|
|
|
|
53
|
|
|
attrs_dict = {'class': 'required'} |
54
|
|
|
|
55
|
|
|
class ActivationForm(forms.Form): |
56
|
|
|
"""Form for activating a user account. |
57
|
|
|
|
58
|
|
|
Requires the password to be entered twice to catch typos. |
59
|
|
|
|
60
|
|
|
Subclasses should feel free to add any additional validation they need, but |
61
|
|
|
should avoid defining a ``save()`` method -- the actual saving of collected |
62
|
|
|
user data is delegated to the active registration backend. |
63
|
|
|
|
64
|
|
|
""" |
65
|
|
|
password1 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, |
66
|
|
|
render_value=False), |
67
|
|
|
label=_("Password")) |
68
|
|
|
password2 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, |
69
|
|
|
render_value=False), |
70
|
|
|
label=_("Password (again)")) |
71
|
|
|
|
72
|
|
|
def clean(self): |
73
|
|
|
"""Check the passed two password are equal |
74
|
|
|
|
75
|
|
|
Verifiy that the values entered into the two password fields match. |
76
|
|
|
Note that an error here will end up in ``non_field_errors()`` because it |
77
|
|
|
doesn't apply to a single field. |
78
|
|
|
|
79
|
|
|
""" |
80
|
|
|
if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data: |
81
|
|
|
if self.cleaned_data['password1'] != self.cleaned_data['password2']: |
82
|
|
|
raise forms.ValidationError(_("The two password fields didn't match.")) |
83
|
|
|
return self.cleaned_data |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
class RegistrationForm(forms.Form): |
87
|
|
|
"""Form for registration a user account. |
88
|
|
|
|
89
|
|
|
Validates that the requested username is not already in use, and requires |
90
|
|
|
the email to be entered twice to catch typos. |
91
|
|
|
|
92
|
|
|
Subclasses should feel free to add any additional validation they need, but |
93
|
|
|
should avoid defining a ``save()`` method -- the actual saving of collected |
94
|
|
|
user data is delegated to the active registration backend. |
95
|
|
|
|
96
|
|
|
""" |
97
|
|
|
username = forms.RegexField(regex=r'^[\w.@+-]+$', |
98
|
|
|
max_length=30, |
99
|
|
|
widget=forms.TextInput(attrs=attrs_dict), |
100
|
|
|
label=_("Username"), |
101
|
|
|
error_messages={ |
102
|
|
|
'invalid': _("This value must contain " |
103
|
|
|
"only letters, numbers and " |
104
|
|
|
"underscores.") |
105
|
|
|
}) |
106
|
|
|
email1 = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict, |
107
|
|
|
maxlength=75)), |
108
|
|
|
label=_("E-mail")) |
109
|
|
|
email2 = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict, |
110
|
|
|
maxlength=75)), |
111
|
|
|
label=_("E-mail (again)")) |
112
|
|
|
|
113
|
|
|
def clean_username(self): |
114
|
|
|
""" |
115
|
|
|
Validate that the username is alphanumeric and is not already in use. |
116
|
|
|
""" |
117
|
|
|
User = get_user_model() |
118
|
|
|
try: |
119
|
|
|
User.objects.get(username__iexact=self.cleaned_data['username']) |
120
|
|
|
except User.DoesNotExist: |
121
|
|
|
return self.cleaned_data['username'] |
122
|
|
|
raise forms.ValidationError(_( |
123
|
|
|
"A user with that username already exists.")) |
124
|
|
|
|
125
|
|
|
def clean(self): |
126
|
|
|
"""Check the passed two email are equal |
127
|
|
|
|
128
|
|
|
Verifiy that the values entered into the two email fields match. |
129
|
|
|
Note that an error here will end up in ``non_field_errors()`` because |
130
|
|
|
it doesn't apply to a single field. |
131
|
|
|
|
132
|
|
|
""" |
133
|
|
|
if 'email1' in self.cleaned_data and 'email2' in self.cleaned_data: |
134
|
|
|
if self.cleaned_data['email1'] != self.cleaned_data['email2']: |
135
|
|
|
raise forms.ValidationError(_( |
136
|
|
|
"The two email fields didn't match.")) |
137
|
|
|
return self.cleaned_data |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
class RegistrationFormTermsOfService(RegistrationForm): |
141
|
|
|
""" |
142
|
|
|
Subclass of ``RegistrationForm`` which adds a required checkbox for |
143
|
|
|
agreeing to a site's Terms of Service. |
144
|
|
|
|
145
|
|
|
""" |
146
|
|
|
tos = forms.BooleanField(widget=forms.CheckboxInput(attrs=attrs_dict), |
147
|
|
|
label=_('I have read and agree to the Terms ' |
148
|
|
|
'of Service'), |
149
|
|
|
error_messages={'required': _( |
150
|
|
|
"You must agree to the terms to register")}) |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
class RegistrationFormUniqueEmail(RegistrationForm): |
154
|
|
|
""" |
155
|
|
|
Subclass of ``RegistrationForm`` which enforces uniqueness of email address |
156
|
|
|
""" |
157
|
|
|
|
158
|
|
|
def clean_email1(self): |
159
|
|
|
"""Validate that the supplied email address is unique for the site.""" |
160
|
|
|
User = get_user_model() |
161
|
|
|
if User.objects.filter(email__iexact=self.cleaned_data['email1']): |
162
|
|
|
raise forms.ValidationError(_( |
163
|
|
|
"This email address is already in use. " |
164
|
|
|
"Please supply a different email address.")) |
165
|
|
|
return self.cleaned_data['email1'] |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
class RegistrationFormNoFreeEmail(RegistrationForm): |
169
|
|
|
""" |
170
|
|
|
Subclass of ``RegistrationForm`` which disallows registration with email |
171
|
|
|
addresses from popular free webmail services; moderately useful for |
172
|
|
|
preventing automated spam registration. |
173
|
|
|
|
174
|
|
|
To change the list of banned domains, subclass this form and override the |
175
|
|
|
attribute ``bad_domains``. |
176
|
|
|
|
177
|
|
|
""" |
178
|
|
|
bad_domains = ['aim.com', 'aol.com', 'email.com', 'gmail.com', |
179
|
|
|
'googlemail.com', 'hotmail.com', 'hushmail.com', |
180
|
|
|
'msn.com', 'mail.ru', 'mailinator.com', 'live.com', |
181
|
|
|
'yahoo.com'] |
182
|
|
|
|
183
|
|
|
def clean_email1(self): |
184
|
|
|
""" |
185
|
|
|
Check the supplied email address against a list of known free webmail |
186
|
|
|
domains. |
187
|
|
|
""" |
188
|
|
|
email_domain = self.cleaned_data['email1'].split('@')[1] |
189
|
|
|
if email_domain in self.bad_domains: |
190
|
|
|
raise forms.ValidationError(_( |
191
|
|
|
"Registration using free email addresses is prohibited. " |
192
|
|
|
"Please supply a different email address.")) |
193
|
|
|
return self.cleaned_data['email1'] |
194
|
|
|
|