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

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