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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. class ShippingAddressForm(AbstractAddressForm):
  10. def __init__(self, *args, **kwargs):
  11. super(ShippingAddressForm, self).__init__(*args, **kwargs)
  12. self.set_country_queryset()
  13. self.fields['country'].empty_label = None
  14. def set_country_queryset(self):
  15. self.fields['country'].queryset = get_model(
  16. 'address', 'country')._default_manager.filter(
  17. is_shipping_country=True)
  18. class Meta:
  19. model = get_model('order', 'shippingaddress')
  20. exclude = ('user', 'search_text')
  21. class GatewayForm(AuthenticationForm):
  22. username = forms.EmailField(label=_("My email address is"))
  23. GUEST, NEW, EXISTING = 'anonymous', 'new', 'existing'
  24. CHOICES = (
  25. (GUEST, _('I am a new customer and want to checkout as a guest')),
  26. (NEW, _('I am a new customer and want to create an account '
  27. 'before checking out')),
  28. (EXISTING, _('I am a returning customer, and my password is')))
  29. options = forms.ChoiceField(widget=forms.widgets.RadioSelect,
  30. choices=CHOICES, initial=GUEST)
  31. def clean_username(self):
  32. return normalise_email(self.cleaned_data['username'])
  33. def clean(self):
  34. if self.is_guest_checkout() or self.is_new_account_checkout():
  35. if 'password' in self.errors:
  36. del self.errors['password']
  37. if 'username' in self.cleaned_data:
  38. email = normalise_email(self.cleaned_data['username'])
  39. if User._default_manager.filter(email=email).exists():
  40. msg = "A user with that email address already exists"
  41. self._errors["username"] = self.error_class([msg])
  42. return self.cleaned_data
  43. return super(GatewayForm, self).clean()
  44. def is_guest_checkout(self):
  45. return self.cleaned_data.get('options', None) == self.GUEST
  46. def is_new_account_checkout(self):
  47. return self.cleaned_data.get('options', None) == self.NEW
  48. # The BillingAddress form is in oscar.apps.payment.forms