Django Library Users
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

forms.py 3.3KB

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