/web/profiles/forms.py

https://gitlab.com/fbi/arguman.org · Python · 93 lines · 73 code · 17 blank · 3 comment · 8 complexity · 7237cddb447176be52a48d51f100deb2 MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. import re
  3. from django import forms
  4. from django.utils.translation import ugettext_lazy as _
  5. from django.contrib.auth.forms import (UserCreationForm,
  6. AuthenticationForm as BaseAuthenticationForm)
  7. from profiles.models import Profile
  8. from django.utils.translation import ugettext_lazy as _
  9. class AuthenticationForm(BaseAuthenticationForm):
  10. def __init__(self, *args, **kwargs):
  11. kwargs.setdefault('label_suffix', '')
  12. super(AuthenticationForm, self).__init__(*args, **kwargs)
  13. class RegistrationForm(UserCreationForm):
  14. username = forms.RegexField(
  15. label=_("Username"),
  16. max_length=30, regex=re.compile(r'^[\w\s-]+$', re.LOCALE),
  17. help_text=_('Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.'),
  18. error_messages={
  19. 'invalid': _("Invalid characters")
  20. }
  21. )
  22. class Meta(UserCreationForm.Meta):
  23. fields = ("username", "email")
  24. model = Profile
  25. def __init__(self, *args, **kwargs):
  26. kwargs.setdefault('label_suffix', '')
  27. super(RegistrationForm, self).__init__(*args, **kwargs)
  28. def clean_username(self):
  29. # Since User.username is unique, this check is redundant,
  30. # but it sets a nicer error message than the ORM. See #13147.
  31. username = self.cleaned_data["username"]
  32. try:
  33. Profile._default_manager.get(username__iexact=username)
  34. except Profile.DoesNotExist:
  35. return username
  36. raise forms.ValidationError(
  37. self.error_messages['duplicate_username'],
  38. code='duplicate_username',
  39. )
  40. class ProfileUpdateForm(forms.ModelForm):
  41. error_messages = {
  42. 'password_mismatch': _("The two password fields didn't match."),
  43. }
  44. class Meta(UserCreationForm.Meta):
  45. fields = ("first_name", "last_name", "email", "twitter_username",
  46. "notification_email")
  47. model = Profile
  48. new_password1 = forms.CharField(label=_("New password"),
  49. required=False,
  50. widget=forms.PasswordInput)
  51. new_password2 = forms.CharField(required=False,
  52. label=_("New password confirmation"),
  53. widget=forms.PasswordInput)
  54. def clean(self):
  55. password1 = self.cleaned_data.get('new_password1')
  56. password2 = self.cleaned_data.get('new_password2')
  57. if password1 or password2:
  58. if password1 != password2:
  59. raise forms.ValidationError(
  60. self.error_messages['password_mismatch'],
  61. code='password_mismatch',
  62. )
  63. return self.cleaned_data
  64. def save(self, commit=True):
  65. if self.cleaned_data.get("new_password1"):
  66. self.instance.set_password(self.cleaned_data['new_password1'])
  67. return super(ProfileUpdateForm, self).save(commit)
  68. def clean_username(self):
  69. username = self.cleaned_data["username"]
  70. try:
  71. Profile._default_manager.exclude(id=self.instance.id).get(
  72. username__iexact=username)
  73. except Profile.DoesNotExist:
  74. return username
  75. raise forms.ValidationError(
  76. 'Bu isimde bir kullanıcı zaten mevcuttur.')