Commit 686fe41d8d1da60381e500af8a1fe6d95ff0ac80
1 parent
8a875cca
Exists in
master
and in
4 other branches
Deleting unused accounts forms
Signed-off-by: Matheus Faria <matheus.sousa.faria@gmail.com> Signed-off-by: TomazMartins <tomaz.r.martins@gmail.com>
Showing
1 changed file
with
1 additions
and
242 deletions
Show diff stats
colab/accounts/forms.py
1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
2 | 2 | ||
3 | -from collections import OrderedDict | ||
4 | - | ||
5 | from django import forms | 3 | from django import forms |
6 | from django.conf import settings | 4 | from django.conf import settings |
7 | -from django.contrib.auth import authenticate, get_user_model | 5 | +from django.contrib.auth import get_user_model |
8 | from django.contrib.auth.forms import ReadOnlyPasswordHashField | 6 | from django.contrib.auth.forms import ReadOnlyPasswordHashField |
9 | -from django.contrib.auth.tokens import default_token_generator | ||
10 | -from django.contrib.sites.shortcuts import get_current_site | ||
11 | from django.core.urlresolvers import reverse | 7 | from django.core.urlresolvers import reverse |
12 | -from django.template import loader | ||
13 | -from django.utils.encoding import force_bytes | ||
14 | from django.utils.functional import lazy | 8 | from django.utils.functional import lazy |
15 | -from django.utils.http import urlsafe_base64_encode | ||
16 | -from django.utils.text import capfirst | ||
17 | from django.utils.translation import ugettext_lazy as _ | 9 | from django.utils.translation import ugettext_lazy as _ |
18 | from django.utils.safestring import mark_safe | 10 | from django.utils.safestring import mark_safe |
19 | 11 | ||
@@ -285,236 +277,3 @@ class UserChangeForm(forms.ModelForm): | @@ -285,236 +277,3 @@ class UserChangeForm(forms.ModelForm): | ||
285 | # This is done here, rather than on the field, because the | 277 | # This is done here, rather than on the field, because the |
286 | # field does not have access to the initial value | 278 | # field does not have access to the initial value |
287 | return self.initial["password"] | 279 | return self.initial["password"] |
288 | - | ||
289 | - | ||
290 | -class AuthenticationForm(forms.Form): | ||
291 | - """ | ||
292 | - Base class for authenticating users. Extend this to get a form that accepts | ||
293 | - username/password logins. | ||
294 | - """ | ||
295 | - username = forms.CharField(max_length=254) | ||
296 | - password = forms.CharField(label=_("Password"), widget=forms.PasswordInput) | ||
297 | - | ||
298 | - error_messages = { | ||
299 | - 'invalid_login': _("Please enter a correct %(username)s and password. " | ||
300 | - "Note that both fields may be case-sensitive."), | ||
301 | - 'inactive': _("This account is inactive."), | ||
302 | - } | ||
303 | - | ||
304 | - def __init__(self, request=None, *args, **kwargs): | ||
305 | - """ | ||
306 | - The 'request' parameter is set for custom auth use by subclasses. | ||
307 | - The form data comes in via the standard 'data' kwarg. | ||
308 | - """ | ||
309 | - self.request = request | ||
310 | - self.user_cache = None | ||
311 | - super(AuthenticationForm, self).__init__(*args, **kwargs) | ||
312 | - | ||
313 | - # Set the label for the "username" field. | ||
314 | - UserModel = get_user_model() | ||
315 | - self.username_field = UserModel._meta.get_field( | ||
316 | - UserModel.USERNAME_FIELD) | ||
317 | - if self.fields['username'].label is None: | ||
318 | - self.fields['username'].label = capfirst( | ||
319 | - self.username_field.verbose_name) | ||
320 | - | ||
321 | - def clean(self): | ||
322 | - username = self.cleaned_data.get('username') | ||
323 | - password = self.cleaned_data.get('password') | ||
324 | - | ||
325 | - if username and password: | ||
326 | - self.user_cache = authenticate(username=username, | ||
327 | - password=password) | ||
328 | - if self.user_cache is None: | ||
329 | - raise forms.ValidationError( | ||
330 | - self.error_messages['invalid_login'], | ||
331 | - code='invalid_login', | ||
332 | - params={'username': self.username_field.verbose_name}, | ||
333 | - ) | ||
334 | - else: | ||
335 | - self.confirm_login_allowed(self.user_cache) | ||
336 | - | ||
337 | - return self.cleaned_data | ||
338 | - | ||
339 | - def confirm_login_allowed(self, user): | ||
340 | - """ | ||
341 | - Controls whether the given User may log in. This is a policy setting, | ||
342 | - independent of end-user authentication. This default behavior is to | ||
343 | - allow login by active users, and reject login by inactive users. | ||
344 | - If the given user cannot log in, this method should raise a | ||
345 | - ``forms.ValidationError``. | ||
346 | - If the given user may log in, this method should return None. | ||
347 | - """ | ||
348 | - if not user.is_active: | ||
349 | - raise forms.ValidationError( | ||
350 | - self.error_messages['inactive'], | ||
351 | - code='inactive', | ||
352 | - ) | ||
353 | - | ||
354 | - def get_user_id(self): | ||
355 | - if self.user_cache: | ||
356 | - return self.user_cache.id | ||
357 | - return None | ||
358 | - | ||
359 | - def get_user(self): | ||
360 | - return self.user_cache | ||
361 | - | ||
362 | - | ||
363 | -class PasswordResetForm(forms.Form): | ||
364 | - email = forms.EmailField(label=_("Email"), max_length=254) | ||
365 | - | ||
366 | - def save(self, domain_override=None, | ||
367 | - subject_template_name='registration/password_reset_subject.txt', | ||
368 | - email_template_name='registration/password_reset_email.html', | ||
369 | - use_https=False, token_generator=default_token_generator, | ||
370 | - from_email=None, request=None, html_email_template_name=None): | ||
371 | - """ | ||
372 | - Generates a one-use only link for resetting password and sends to the | ||
373 | - user. | ||
374 | - """ | ||
375 | - from django.core.mail import send_mail | ||
376 | - UserModel = get_user_model() | ||
377 | - email = self.cleaned_data["email"] | ||
378 | - active_users = UserModel._default_manager.filter( | ||
379 | - email__iexact=email, is_active=True) | ||
380 | - for user in active_users: | ||
381 | - # Make sure that no email is sent to a user that actually has | ||
382 | - # a password marked as unusable | ||
383 | - if not user.has_usable_password(): | ||
384 | - continue | ||
385 | - if not domain_override: | ||
386 | - current_site = get_current_site(request) | ||
387 | - site_name = current_site.name | ||
388 | - domain = current_site.domain | ||
389 | - else: | ||
390 | - site_name = domain = domain_override | ||
391 | - c = { | ||
392 | - 'email': user.email, | ||
393 | - 'domain': domain, | ||
394 | - 'site_name': site_name, | ||
395 | - 'uid': urlsafe_base64_encode(force_bytes(user.pk)), | ||
396 | - 'user': user, | ||
397 | - 'token': token_generator.make_token(user), | ||
398 | - 'protocol': 'https' if use_https else 'http', | ||
399 | - } | ||
400 | - subject = loader.render_to_string(subject_template_name, c) | ||
401 | - # Email subject *must not* contain newlines | ||
402 | - subject = ''.join(subject.splitlines()) | ||
403 | - email = loader.render_to_string(email_template_name, c) | ||
404 | - | ||
405 | - if html_email_template_name: | ||
406 | - html_email = loader.render_to_string(html_email_template_name, | ||
407 | - c) | ||
408 | - else: | ||
409 | - html_email = None | ||
410 | - send_mail(subject, email, from_email, [user.email], | ||
411 | - html_message=html_email) | ||
412 | - | ||
413 | - | ||
414 | -class SetPasswordForm(forms.Form): | ||
415 | - """ | ||
416 | - A form that lets a user change set their password without entering the old | ||
417 | - password | ||
418 | - """ | ||
419 | - error_messages = { | ||
420 | - 'password_mismatch': _("The two password fields didn't match."), | ||
421 | - } | ||
422 | - new_password1 = forms.CharField(label=_("New password"), | ||
423 | - widget=forms.PasswordInput) | ||
424 | - new_password2 = forms.CharField(label=_("New password confirmation"), | ||
425 | - widget=forms.PasswordInput) | ||
426 | - | ||
427 | - def __init__(self, user, *args, **kwargs): | ||
428 | - self.user = user | ||
429 | - super(SetPasswordForm, self).__init__(*args, **kwargs) | ||
430 | - | ||
431 | - def clean_new_password2(self): | ||
432 | - password1 = self.cleaned_data.get('new_password1') | ||
433 | - password2 = self.cleaned_data.get('new_password2') | ||
434 | - if password1 and password2: | ||
435 | - if password1 != password2: | ||
436 | - raise forms.ValidationError( | ||
437 | - self.error_messages['password_mismatch'], | ||
438 | - code='password_mismatch', | ||
439 | - ) | ||
440 | - return password2 | ||
441 | - | ||
442 | - def save(self, commit=True): | ||
443 | - self.user.set_password(self.cleaned_data['new_password1']) | ||
444 | - if commit: | ||
445 | - self.user.save() | ||
446 | - return self.user | ||
447 | - | ||
448 | - | ||
449 | -class PasswordChangeForm(SetPasswordForm): | ||
450 | - """ | ||
451 | - A form that lets a user change their password by entering their old | ||
452 | - password. | ||
453 | - """ | ||
454 | - error_messages = dict(SetPasswordForm.error_messages, **{ | ||
455 | - 'password_incorrect': _("Your old password was entered incorrectly. " | ||
456 | - "Please enter it again."), | ||
457 | - }) | ||
458 | - old_password = forms.CharField(label=_("Old password"), | ||
459 | - widget=forms.PasswordInput) | ||
460 | - | ||
461 | - def clean_old_password(self): | ||
462 | - """ | ||
463 | - Validates that the old_password field is correct. | ||
464 | - """ | ||
465 | - old_password = self.cleaned_data["old_password"] | ||
466 | - if not self.user.check_password(old_password): | ||
467 | - raise forms.ValidationError( | ||
468 | - self.error_messages['password_incorrect'], | ||
469 | - code='password_incorrect', | ||
470 | - ) | ||
471 | - return old_password | ||
472 | - | ||
473 | -PasswordChangeForm.base_fields = OrderedDict( | ||
474 | - (k, PasswordChangeForm.base_fields[k]) | ||
475 | - for k in ['old_password', 'new_password1', 'new_password2'] | ||
476 | -) | ||
477 | - | ||
478 | - | ||
479 | -class AdminPasswordChangeForm(forms.Form): | ||
480 | - """ | ||
481 | - A form used to change the password of a user in the admin interface. | ||
482 | - """ | ||
483 | - error_messages = { | ||
484 | - 'password_mismatch': _("The two password fields didn't match."), | ||
485 | - } | ||
486 | - password1 = forms.CharField(label=_("Password"), | ||
487 | - widget=forms.PasswordInput) | ||
488 | - password2 = forms.CharField(label=_("Password (again)"), | ||
489 | - widget=forms.PasswordInput) | ||
490 | - | ||
491 | - def __init__(self, user, *args, **kwargs): | ||
492 | - self.user = user | ||
493 | - super(AdminPasswordChangeForm, self).__init__(*args, **kwargs) | ||
494 | - | ||
495 | - def clean_password2(self): | ||
496 | - password1 = self.cleaned_data.get('password1') | ||
497 | - password2 = self.cleaned_data.get('password2') | ||
498 | - if password1 and password2: | ||
499 | - if password1 != password2: | ||
500 | - raise forms.ValidationError( | ||
501 | - self.error_messages['password_mismatch'], | ||
502 | - code='password_mismatch', | ||
503 | - ) | ||
504 | - return password2 | ||
505 | - | ||
506 | - def save(self, commit=True): | ||
507 | - """ | ||
508 | - Saves the new password. | ||
509 | - """ | ||
510 | - self.user.set_password(self.cleaned_data["password1"]) | ||
511 | - if commit: | ||
512 | - self.user.save() | ||
513 | - return self.user | ||
514 | - | ||
515 | - def _get_changed_data(self): | ||
516 | - data = super(AdminPasswordChangeForm, self).changed_data | ||
517 | - for name in self.fields.keys(): | ||
518 | - if name not in data: | ||
519 | - return [] | ||
520 | - return ['password'] |