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.

forms.py 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import string
  2. import random
  3. from django.contrib.auth.forms import AuthenticationForm
  4. from django.utils.translation import ugettext_lazy as _
  5. from django import forms
  6. from django.contrib.auth.models import User
  7. def generate_username():
  8. uname = ''.join([random.choice(string.letters + string.digits + '_') for i in range(30)])
  9. try:
  10. User.objects.get(username=uname)
  11. return generate_username()
  12. except User.DoesNotExist:
  13. return uname
  14. class EmailAuthenticationForm(AuthenticationForm):
  15. """
  16. Extends the standard django AuthenticationForm, to support 75 character
  17. usernames. 75 character usernames are needed to support the EmailOrUsername
  18. auth backend.
  19. """
  20. username = forms.EmailField(label=_('Email Address'))
  21. class EmailUserCreationForm(forms.ModelForm):
  22. email = forms.EmailField(label=_('Email Address'))
  23. password1 = forms.CharField(label=_('Password'), widget=forms.PasswordInput)
  24. password2 = forms.CharField(label=_('Confirm Password'), widget=forms.PasswordInput)
  25. class Meta:
  26. model = User
  27. fields = ('email',)
  28. def clean_email(self):
  29. email = self.cleaned_data['email']
  30. try:
  31. User.objects.get(email=email)
  32. except User.DoesNotExist:
  33. return email
  34. raise forms.ValidationError(_("A user with that email address already exists."))
  35. def clean_password2(self):
  36. password1 = self.cleaned_data.get('password1', '')
  37. password2 = self.cleaned_data.get('password2', '')
  38. if password1 != password2:
  39. raise forms.ValidationError(_("The two password fields didn't match."))
  40. return password2
  41. def save(self, commit=True):
  42. user = super(EmailUserCreationForm, self).save(commit=False)
  43. user.set_password(self.cleaned_data['password1'])
  44. user.username = generate_username()
  45. if commit:
  46. user.save()
  47. return user
  48. class SearchByDateRangeForm(forms.Form):
  49. date_from = forms.DateField(required=False, label="From")
  50. date_to = forms.DateField(required=False, label="To")
  51. def clean(self):
  52. if self.is_valid() and not self.cleaned_data['date_from'] and not self.cleaned_data['date_to']:
  53. raise forms.ValidationError(_("At least one date field is required."))
  54. return super(SearchByDateRangeForm, self).clean()
  55. def description(self):
  56. if not self.is_bound:
  57. return 'All orders'
  58. date_from = self.cleaned_data['date_from']
  59. date_to = self.cleaned_data['date_to']
  60. if date_from and date_to:
  61. return 'Orders placed between %s and %s' % (date_from, date_to)
  62. elif date_from and not date_to:
  63. return 'Orders placed since %s' % date_from
  64. elif not date_from and date_to:
  65. return 'Orders placed until %s' % date_to
  66. def get_filters(self):
  67. date_from = self.cleaned_data['date_from']
  68. date_to = self.cleaned_data['date_to']
  69. if date_from and date_to:
  70. return {'date_placed__range': [date_from, date_to]}
  71. elif date_from and not date_to:
  72. return {'date_placed__gt': date_from}
  73. elif not date_from and date_to:
  74. return {'date_placed__lt': date_to}
  75. return {}