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.

114 lines
3.2 KiB

10 years ago
10 years ago
10 years ago
9 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. from hc.api.models import TokenBucket
  6. class LowercaseEmailField(forms.EmailField):
  7. def clean(self, value):
  8. value = super(LowercaseEmailField, self).clean(value)
  9. return value.lower()
  10. class AvailableEmailForm(forms.Form):
  11. # Call it "identity" instead of "email"
  12. # to avoid some of the dumber bots
  13. identity = LowercaseEmailField(
  14. error_messages={"required": "Please enter your email address."}
  15. )
  16. def clean_identity(self):
  17. v = self.cleaned_data["identity"]
  18. if len(v) > 254:
  19. raise forms.ValidationError("Address is too long.")
  20. if User.objects.filter(email=v).exists():
  21. raise forms.ValidationError(
  22. "An account with this email address already exists."
  23. )
  24. return v
  25. class EmailLoginForm(forms.Form):
  26. # Call it "identity" instead of "email"
  27. # to avoid some of the dumber bots
  28. identity = LowercaseEmailField()
  29. def clean_identity(self):
  30. v = self.cleaned_data["identity"]
  31. if not TokenBucket.authorize_login_email(v):
  32. raise forms.ValidationError("Too many attempts, please try later.")
  33. try:
  34. self.user = User.objects.get(email=v)
  35. except User.DoesNotExist:
  36. raise forms.ValidationError("Incorrect email address.")
  37. return v
  38. class PasswordLoginForm(forms.Form):
  39. email = LowercaseEmailField()
  40. password = forms.CharField()
  41. def clean(self):
  42. username = self.cleaned_data.get("email")
  43. password = self.cleaned_data.get("password")
  44. if username and password:
  45. if not TokenBucket.authorize_login_password(username):
  46. raise forms.ValidationError("Too many attempts, please try later.")
  47. self.user = authenticate(username=username, password=password)
  48. if self.user is None or not self.user.is_active:
  49. raise forms.ValidationError("Incorrect email or password.")
  50. return self.cleaned_data
  51. class ReportSettingsForm(forms.Form):
  52. reports_allowed = forms.BooleanField(required=False)
  53. nag_period = forms.IntegerField(min_value=0, max_value=86400)
  54. def clean_nag_period(self):
  55. seconds = self.cleaned_data["nag_period"]
  56. if seconds not in (0, 3600, 86400):
  57. raise forms.ValidationError("Bad nag_period: %d" % seconds)
  58. return td(seconds=seconds)
  59. class SetPasswordForm(forms.Form):
  60. password = forms.CharField(min_length=8)
  61. class ChangeEmailForm(forms.Form):
  62. error_css_class = "has-error"
  63. email = LowercaseEmailField()
  64. def clean_email(self):
  65. v = self.cleaned_data["email"]
  66. if User.objects.filter(email=v).exists():
  67. raise forms.ValidationError("%s is already registered" % v)
  68. return v
  69. class InviteTeamMemberForm(forms.Form):
  70. email = LowercaseEmailField(max_length=254)
  71. rw = forms.BooleanField(required=False)
  72. class RemoveTeamMemberForm(forms.Form):
  73. email = LowercaseEmailField()
  74. class ProjectNameForm(forms.Form):
  75. name = forms.CharField(max_length=60, required=True)
  76. class TransferForm(forms.Form):
  77. email = LowercaseEmailField()