Skip to content

Instantly share code, notes, and snippets.

@matthiask
Created February 15, 2012 09:42
Show Gist options
  • Save matthiask/1834813 to your computer and use it in GitHub Desktop.
Save matthiask/1834813 to your computer and use it in GitHub Desktop.
Plata: Working Shop class with custom Contact model
class BaseCheckoutForm(forms.ModelForm):
"""
Needs self.request
"""
def clean(self):
data = self.cleaned_data
email = data.get('email')
create_account = data.get('create_account')
if email:
users = list(User.objects.filter(email=email))
if users:
if self.request.user not in users:
if self.request.user.is_authenticated():
self._errors['email'] = self.error_class([
_('This e-mail address belongs to a different account.')])
else:
self._errors['email'] = self.error_class([
_('This e-mail address might belong to you, but we cannot know for sure because you are not authenticated yet.')])
return data
class CustomShop(Shop):
def checkout_form(self, request, order):
class CheckoutForm(BaseCheckoutForm):
class Meta:
fields = ['email'] + ['billing_%s' % f for f in Contact.ADDRESS_FIELDS]
model = self.order_model
def __init__(self, *args, **kwargs):
contact = kwargs.pop('contact')
self.request = kwargs.pop('request')
shop = kwargs.pop('shop')
if contact:
initial = {}
for f in contact.ADDRESS_FIELDS:
initial['billing_%s' % f] = getattr(contact, f)
kwargs['initial'] = initial
super(CheckoutForm, self).__init__(*args, **kwargs)
if not contact:
self.fields['create_account'] = forms.BooleanField(
label=_('create account'),
required=False, initial=True)
return CheckoutForm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment