Django Library Users
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

forms.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from django import forms
  2. from django.contrib.auth.models import User
  3. from django.contrib.auth.forms import AuthenticationForm, UserCreationForm, UserChangeForm, PasswordChangeForm, SetPasswordForm
  4. from django.utils.translation import gettext as _
  5. from .models import UserProfile, get_userprofile
  6. from users import emails
  7. class PasswordRecoverForm(AuthenticationForm):
  8. password = None
  9. class PasswordRecoverChangeForm(SetPasswordForm):
  10. fields = ["new_password1", "new_password2"]
  11. class UserRegistrationForm(UserCreationForm):
  12. email = forms.EmailField()
  13. class Meta:
  14. model = User
  15. fields = ['username', 'email', 'first_name', 'last_name', 'password1', 'password2']
  16. class UserProfileForm(forms.ModelForm):
  17. class Meta:
  18. model = UserProfile
  19. fields = ['timezone', 'language_code']
  20. class UserProfileFormLanguageOnly(forms.ModelForm):
  21. class Meta:
  22. model = UserProfile
  23. fields = ['language_code']
  24. class UserPasswordChangeForm(PasswordChangeForm):
  25. email = forms.EmailField()
  26. first_name = forms.CharField(max_length=150)
  27. last_name = forms.CharField(max_length=150)
  28. field_order = ["email", "first_name", "last_name", "old_password", "new_password1", "new_password2"]
  29. def __init__(self, request):
  30. self.request = request
  31. #
  32. data = request.POST or {'email': request.user.email, 'first_name': request.user.first_name, 'last_name': request.user.last_name}
  33. #
  34. super().__init__(request.user, data)
  35. self.fields['old_password'].widget.attrs.update({'autofocus': False})
  36. self.fields['old_password'].required = False
  37. def clean_old_password(self):
  38. """
  39. Validation of old_password field only, if set.
  40. """
  41. old_password = self.cleaned_data["old_password"]
  42. if old_password and not self.user.check_password(old_password):
  43. raise forms.ValidationError(
  44. self.error_messages["password_incorrect"],
  45. code="password_incorrect",
  46. )
  47. return old_password
  48. def clean(self):
  49. """
  50. Validation of new_passwords only if old_password field is set.
  51. """
  52. clean_data = forms.Form.clean(self)
  53. old_password = clean_data.get('old_password')
  54. #
  55. if old_password:
  56. self.validate_passwords("new_password1", "new_password2")
  57. self.validate_password_for_user(self.user, "new_password2")
  58. return forms.Form.clean(self)
  59. def save(self, commit=True):
  60. changed = False
  61. for key in self.fields:
  62. new = self.data.get(key)
  63. try:
  64. old = getattr(self.user, key)
  65. except AttributeError:
  66. pass # Is a password field
  67. else:
  68. if new != old:
  69. if key == 'email':
  70. emails.send_validation_mail(self.user, self.request)
  71. up = get_userprofile(self.user)
  72. up.mail_pending = new
  73. up.save()
  74. else:
  75. changed = True
  76. setattr(self.user, key, new)
  77. if self.data.get('new_password1'):
  78. changed = True
  79. self.user.set_password(self.data.get('new_password1'))
  80. #
  81. if changed:
  82. self.user.save()
  83. return changed
  84. class UserActivationForm(UserChangeForm):
  85. password = None
  86. class Meta:
  87. model = User
  88. fields = ['is_active', 'is_staff']