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 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from django import forms
  2. from django.db.models import get_model
  3. from django.contrib.auth.forms import AuthenticationForm
  4. from django.utils.translation import ugettext_lazy as _
  5. from oscar.apps.address.forms import AbstractAddressForm
  6. from oscar.apps.customer.utils import normalise_email
  7. from oscar.core.compat import get_user_model
  8. User = get_user_model()
  9. Country = get_model('address', 'Country')
  10. class ShippingAddressForm(AbstractAddressForm):
  11. def __init__(self, *args, **kwargs):
  12. super(ShippingAddressForm, self).__init__(*args, **kwargs)
  13. countries = Country._default_manager.filter(
  14. is_shipping_country=True)
  15. # No need to show country dropdown if there is only one option
  16. if len(countries) == 1:
  17. del self.fields['country']
  18. self.instance.country = countries[0]
  19. else:
  20. self.fields['country'].queryset = countries
  21. self.fields['country'].empty_label = None
  22. class Meta:
  23. model = get_model('order', 'shippingaddress')
  24. exclude = ('user', 'search_text')
  25. class GatewayForm(AuthenticationForm):
  26. username = forms.EmailField(label=_("My email address is"))
  27. GUEST, NEW, EXISTING = 'anonymous', 'new', 'existing'
  28. CHOICES = (
  29. (GUEST, _('I am a new customer and want to checkout as a guest')),
  30. (NEW, _('I am a new customer and want to create an account '
  31. 'before checking out')),
  32. (EXISTING, _('I am a returning customer, and my password is')))
  33. options = forms.ChoiceField(widget=forms.widgets.RadioSelect,
  34. choices=CHOICES, initial=GUEST)
  35. def clean_username(self):
  36. return normalise_email(self.cleaned_data['username'])
  37. def clean(self):
  38. if self.is_guest_checkout() or self.is_new_account_checkout():
  39. if 'password' in self.errors:
  40. del self.errors['password']
  41. if 'username' in self.cleaned_data:
  42. email = normalise_email(self.cleaned_data['username'])
  43. if User._default_manager.filter(email=email).exists():
  44. msg = "A user with that email address already exists"
  45. self._errors["username"] = self.error_class([msg])
  46. return self.cleaned_data
  47. return super(GatewayForm, self).clean()
  48. def is_guest_checkout(self):
  49. return self.cleaned_data.get('options', None) == self.GUEST
  50. def is_new_account_checkout(self):
  51. return self.cleaned_data.get('options', None) == self.NEW
  52. # The BillingAddress form is in oscar.apps.payment.forms