You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.7 KiB

10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
  1. from datetime import timedelta as td
  2. from django import forms
  3. from django.contrib.auth import authenticate
  4. from django.contrib.auth.models import User
  5. class LowercaseEmailField(forms.EmailField):
  6. def clean(self, value):
  7. value = super(LowercaseEmailField, self).clean(value)
  8. return value.lower()
  9. class AvailableEmailForm(forms.Form):
  10. # Call it "identity" instead of "email"
  11. # to avoid some of the dumber bots
  12. identity = LowercaseEmailField(error_messages={'required': 'Please enter your email address.'})
  13. def clean_identity(self):
  14. v = self.cleaned_data["identity"]
  15. if User.objects.filter(email=v).exists():
  16. raise forms.ValidationError("An account with this email address already exists.")
  17. return v
  18. class ExistingEmailForm(forms.Form):
  19. # Call it "identity" instead of "email"
  20. # to avoid some of the dumber bots
  21. identity = LowercaseEmailField()
  22. def clean_identity(self):
  23. v = self.cleaned_data["identity"]
  24. try:
  25. self.user = User.objects.get(email=v)
  26. except User.DoesNotExist:
  27. raise forms.ValidationError("Incorrect email address.")
  28. return v
  29. class EmailPasswordForm(forms.Form):
  30. email = LowercaseEmailField()
  31. password = forms.CharField()
  32. def clean(self):
  33. username = self.cleaned_data.get('email')
  34. password = self.cleaned_data.get('password')
  35. if username and password:
  36. self.user = authenticate(username=username, password=password)
  37. if self.user is None:
  38. raise forms.ValidationError("Incorrect email or password")
  39. if not self.user.is_active:
  40. raise forms.ValidationError("Account is inactive")
  41. return self.cleaned_data
  42. class ReportSettingsForm(forms.Form):
  43. reports_allowed = forms.BooleanField(required=False)
  44. nag_period = forms.IntegerField(min_value=0, max_value=86400)
  45. def clean_nag_period(self):
  46. seconds = self.cleaned_data["nag_period"]
  47. if seconds not in (0, 3600, 86400):
  48. raise forms.ValidationError("Bad nag_period: %d" % seconds)
  49. return td(seconds=seconds)
  50. class SetPasswordForm(forms.Form):
  51. password = forms.CharField()
  52. class ChangeEmailForm(forms.Form):
  53. error_css_class = "has-error"
  54. email = LowercaseEmailField()
  55. def clean_email(self):
  56. v = self.cleaned_data["email"]
  57. if User.objects.filter(email=v).exists():
  58. raise forms.ValidationError("%s is already registered" % v)
  59. return v
  60. class InviteTeamMemberForm(forms.Form):
  61. email = LowercaseEmailField()
  62. class RemoveTeamMemberForm(forms.Form):
  63. email = LowercaseEmailField()
  64. class ProjectNameForm(forms.Form):
  65. name = forms.CharField(max_length=200, required=True)