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.6KB

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