forked from pennersr/django-allauth
/
forms.py
442 lines (367 loc) · 15.6 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import base64
import re
import uuid
from django import forms
from django.conf import settings
from django.core.mail import send_mail
from django.core.urlresolvers import reverse
from django.template.loader import render_to_string
from django.utils.translation import ugettext_lazy as _, ugettext
from django.utils.encoding import smart_unicode
from django.utils.hashcompat import sha_constructor
from django.utils.http import int_to_base36
from django.contrib import messages
from django.contrib.auth import authenticate
from django.contrib.auth.models import User
from django.contrib.auth.tokens import default_token_generator
from django.contrib.sites.models import Site
from emailconfirmation.models import EmailAddress
# from models import PasswordReset
from utils import user_display, perform_login, send_email_confirmation
from allauth.utils import email_address_exists
from app_settings import *
alnum_re = re.compile(r"^\w+$")
class LoginForm(forms.Form):
password = forms.CharField(
label = _("Password"),
widget = forms.PasswordInput(render_value=False)
)
remember = forms.BooleanField(
label = _("Remember Me"),
# help_text = _("If checked you will stay logged in for 3 weeks"),
required = False
)
user = None
def __init__(self, *args, **kwargs):
super(LoginForm, self).__init__(*args, **kwargs)
ordering = []
if EMAIL_AUTHENTICATION:
self.fields["email"] = forms.EmailField(
label = ugettext("E-mail"),
)
ordering.append("email")
else:
self.fields["username"] = forms.CharField(
label = ugettext("Username"),
max_length = 30,
)
ordering.append("username")
ordering.extend(["password", "remember"])
self.fields.keyOrder = ordering
def user_credentials(self):
"""
Provides the credentials required to authenticate the user for
login.
"""
credentials = {}
if EMAIL_AUTHENTICATION:
credentials["email"] = self.cleaned_data["email"]
else:
credentials["username"] = self.cleaned_data["username"]
credentials["password"] = self.cleaned_data["password"]
return credentials
def clean(self):
if self._errors:
return
user = authenticate(**self.user_credentials())
if user:
if user.is_active:
self.user = user
else:
raise forms.ValidationError(_("This account is currently inactive."))
else:
if EMAIL_AUTHENTICATION:
error = _("The e-mail address and/or password you specified are not correct.")
else:
error = _("The username and/or password you specified are not correct.")
raise forms.ValidationError(error)
return self.cleaned_data
def login(self, request):
perform_login(request, self.user)
if self.cleaned_data["remember"]:
request.session.set_expiry(60 * 60 * 24 * 7 * 3)
else:
request.session.set_expiry(0)
class BaseSignupForm(forms.Form):
username = forms.CharField(
label = _("Username"),
max_length = 30,
widget = forms.TextInput()
)
email = forms.EmailField(widget=forms.TextInput())
def __init__(self, *args, **kwargs):
super(BaseSignupForm, self).__init__(*args, **kwargs)
if EMAIL_REQUIRED or EMAIL_VERIFICATION or EMAIL_AUTHENTICATION:
self.fields["email"].label = ugettext("E-mail")
self.fields["email"].required = True
else:
self.fields["email"].label = ugettext("E-mail (optional)")
self.fields["email"].required = False
if not USERNAME_REQUIRED:
del self.fields["username"]
def random_username(self):
return base64.urlsafe_b64encode(uuid.uuid4().bytes).strip('=')
def clean_username(self):
value = self.cleaned_data["username"]
if not alnum_re.search(value):
raise forms.ValidationError(_("Usernames can only contain "
"letters, numbers and underscores."))
try:
User.objects.get(username__iexact=value)
except User.DoesNotExist:
return value
raise forms.ValidationError(_("This username is already taken. Please "
"choose another."))
def clean_email(self):
value = self.cleaned_data["email"]
if UNIQUE_EMAIL or EMAIL_AUTHENTICATION:
if value and email_address_exists(value):
raise forms.ValidationError \
(_("A user is registered with this e-mail address."))
return value
def create_user(self, commit=True):
user = User()
if USERNAME_REQUIRED:
user.username = self.cleaned_data["username"]
else:
while True:
user.username = self.random_username()
try:
User.objects.get(username=user.username)
except User.DoesNotExist:
break
user.email = self.cleaned_data["email"].strip().lower()
user.set_unusable_password()
if EMAIL_VERIFICATION:
user.is_active = False
if commit:
user.save()
return user
class SignupForm(BaseSignupForm):
password1 = forms.CharField(
label = _("Password"),
widget = forms.PasswordInput(render_value=False)
)
password2 = forms.CharField(
label = _("Password (again)"),
widget = forms.PasswordInput(render_value=False)
)
confirmation_key = forms.CharField(
max_length = 40,
required = False,
widget = forms.HiddenInput()
)
def __init__(self, *args, **kwargs):
super(SignupForm, self).__init__(*args, **kwargs)
self.fields.keyOrder = ["username",
"password1",
"password2",
"email"]
if not USERNAME_REQUIRED:
self.fields.keyOrder = ["email",
"password1",
"password2"]
if not SIGNUP_PASSWORD_VERIFICATION:
del self.fields["password2"]
def clean(self):
if SIGNUP_PASSWORD_VERIFICATION \
and "password1" in self.cleaned_data \
and "password2" in self.cleaned_data:
if self.cleaned_data["password1"] != self.cleaned_data["password2"]:
raise forms.ValidationError(_("You must type the same password each time."))
return self.cleaned_data
def create_user(self, commit=True):
user = super(SignupForm, self).create_user(commit=False)
password = self.cleaned_data.get("password1")
if password:
user.set_password(password)
if commit:
user.save()
return user
def save(self, request=None):
# don't assume a username is available. it is a common removal if
# site developer wants to use e-mail authentication.
username = self.cleaned_data.get("username")
email = self.cleaned_data["email"]
if self.cleaned_data.get("confirmation_key"):
from friends.models import JoinInvitation # @@@ temporary fix for issue 93
try:
join_invitation = JoinInvitation.objects.get(confirmation_key=self.cleaned_data["confirmation_key"])
confirmed = True
except JoinInvitation.DoesNotExist:
confirmed = False
else:
confirmed = False
# @@@ clean up some of the repetition below -- DRY!
if confirmed:
if email == join_invitation.contact.email:
new_user = self.create_user()
join_invitation.accept(new_user) # should go before creation of EmailAddress below
if request:
messages.add_message(request, messages.INFO,
ugettext(u"Your e-mail address has already been verified")
)
# already verified so can just create
EmailAddress(user=new_user, email=email, verified=True, primary=True).save()
else:
new_user = self.create_user()
join_invitation.accept(new_user) # should go before creation of EmailAddress below
if email:
if request:
messages.add_message(request, messages.INFO,
ugettext(u"Confirmation e-mail sent to %(email)s") % {
"email": email,
}
)
EmailAddress.objects.add_email(new_user, email)
else:
new_user = self.create_user()
send_email_confirmation(new_user, request=request)
self.after_signup(new_user)
return new_user
def after_signup(self, user, **kwargs):
"""
An extension point for subclasses.
"""
pass
class UserForm(forms.Form):
def __init__(self, user=None, *args, **kwargs):
self.user = user
super(UserForm, self).__init__(*args, **kwargs)
class AccountForm(UserForm):
def __init__(self, *args, **kwargs):
super(AccountForm, self).__init__(*args, **kwargs)
try:
self.account = Account.objects.get(user=self.user)
except Account.DoesNotExist:
self.account = Account(user=self.user)
class AddEmailForm(UserForm):
email = forms.EmailField(
label = _("E-mail"),
required = True,
widget = forms.TextInput(attrs={"size": "30"})
)
def clean_email(self):
value = self.cleaned_data["email"]
errors = {
"this_account": _("This e-mail address already associated with this account."),
"different_account": _("This e-mail address already associated with another account."),
}
if UNIQUE_EMAIL:
try:
email = EmailAddress.objects.get(email__iexact=value)
except EmailAddress.DoesNotExist:
return value
if email.user == self.user:
raise forms.ValidationError(errors["this_account"])
raise forms.ValidationError(errors["different_account"])
else:
try:
EmailAddress.objects.get(user=self.user, email__iexact=value)
except EmailAddress.DoesNotExist:
return value
raise forms.ValidationError(errors["this_account"])
def save(self):
return EmailAddress.objects.add_email(self.user, self.cleaned_data["email"])
class ChangePasswordForm(UserForm):
oldpassword = forms.CharField(
label = _("Current Password"),
widget = forms.PasswordInput(render_value=False)
)
password1 = forms.CharField(
label = _("New Password"),
widget = forms.PasswordInput(render_value=False)
)
password2 = forms.CharField(
label = _("New Password (again)"),
widget = forms.PasswordInput(render_value=False)
)
def clean_oldpassword(self):
if not self.user.check_password(self.cleaned_data.get("oldpassword")):
raise forms.ValidationError(_("Please type your current password."))
return self.cleaned_data["oldpassword"]
def clean_password2(self):
if "password1" in self.cleaned_data and "password2" in self.cleaned_data:
if self.cleaned_data["password1"] != self.cleaned_data["password2"]:
raise forms.ValidationError(_("You must type the same password each time."))
return self.cleaned_data["password2"]
def save(self):
self.user.set_password(self.cleaned_data["password1"])
self.user.save()
class SetPasswordForm(UserForm):
password1 = forms.CharField(
label = _("Password"),
widget = forms.PasswordInput(render_value=False)
)
password2 = forms.CharField(
label = _("Password (again)"),
widget = forms.PasswordInput(render_value=False)
)
def clean_password2(self):
if "password1" in self.cleaned_data and "password2" in self.cleaned_data:
if self.cleaned_data["password1"] != self.cleaned_data["password2"]:
raise forms.ValidationError(_("You must type the same password each time."))
return self.cleaned_data["password2"]
def save(self):
self.user.set_password(self.cleaned_data["password1"])
self.user.save()
class ResetPasswordForm(forms.Form):
email = forms.EmailField(
label = _("E-mail"),
required = True,
widget = forms.TextInput(attrs={"size":"30"})
)
def clean_email(self):
if EmailAddress.objects.filter(email__iexact=self.cleaned_data["email"], verified=True).count() == 0:
raise forms.ValidationError(_("E-mail address not verified for any user account"))
return self.cleaned_data["email"]
def save(self, **kwargs):
email = self.cleaned_data["email"]
token_generator = kwargs.get("token_generator", default_token_generator)
for user in User.objects.filter(email__iexact=email):
temp_key = token_generator.make_token(user)
# save it to the password reset model
# password_reset = PasswordReset(user=user, temp_key=temp_key)
# password_reset.save()
current_site = Site.objects.get_current()
domain = unicode(current_site.domain)
# send the password reset email
subject = _("Password Reset E-mail")
path = reverse("account_reset_password_from_key",
kwargs=dict(uidb36=int_to_base36(user.id),
key=temp_key))
url = 'http://%s%s' % (current_site.domain,
path)
message = render_to_string \
("account/password_reset_key_message.txt",
{ "site": current_site,
"user": user,
"password_reset_url": url })
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [user.email])
return self.cleaned_data["email"]
class ResetPasswordKeyForm(forms.Form):
password1 = forms.CharField(
label = _("New Password"),
widget = forms.PasswordInput(render_value=False)
)
password2 = forms.CharField(
label = _("New Password (again)"),
widget = forms.PasswordInput(render_value=False)
)
def __init__(self, *args, **kwargs):
self.user = kwargs.pop("user", None)
self.temp_key = kwargs.pop("temp_key", None)
super(ResetPasswordKeyForm, self).__init__(*args, **kwargs)
# FIXME: Inspecting other fields -> should be put in def clean(self) ?
def clean_password2(self):
if "password1" in self.cleaned_data and "password2" in self.cleaned_data:
if self.cleaned_data["password1"] != self.cleaned_data["password2"]:
raise forms.ValidationError(_("You must type the same password each time."))
return self.cleaned_data["password2"]
def save(self):
# set the new user password
user = self.user
user.set_password(self.cleaned_data["password1"])
user.save()
# mark password reset object as reset
# PasswordReset.objects.filter(temp_key=self.temp_key).update(reset=True)