def __init__(self, *args, **kwargs): checkout = kwargs.pop('checkout', None) super(PostageForm, self).__init__(*args, **kwargs) # # iterate the models checking for matches # self.choices = [] for model_path in c_settings.CCCHECKOUT_POSTAGE_MODELS: module, model_cls = import_thing(model_path) self.matches = model_cls.objects.estimate(checkout) for match in self.matches: method_ct = ContentType.objects.get_for_model(match[0]) tier_ct = ContentType.objects.get_for_model(match[1]) # make the choice value for the form value = '%s:%s|%s:%s' % ( method_ct.pk, match[0].pk, tier_ct.pk, match[1].pk) self.choices.append({ 'value': value, 'label': u'%s - £%s' % (match[0].name, match[1].price), 'price': match[1].price, 'method': match[0], 'method_ct': method_ct, 'tier': match[1], 'tier_ct': tier_ct }) input_choices = [] # TODO: order the choices by price for choice in self.choices: input_choices.append((choice['value'], choice['label'])) self.fields['available_methods'].choices = input_choices self.fields['available_methods'].initial = 1
def customer(request, *args, **kwargs): """Handles the processing of the customer model and form""" checkout = kwargs['checkout'] # if there is no form in the settings for customer redirect to postage if c_settings.CCCHECKOUT_CUSTOMER_FORM is None: return HttpResponseRedirect(reverse('cccheckout:postage')) # get the form module, form_cls = import_thing(c_settings.CCCHECKOUT_CUSTOMER_FORM) model_cls = form_cls._meta.model # make the form form = form_cls(instance=checkout.customer) # make discount form discountform = kwargs.get('discountform', None) if request.method == 'POST': form = form_cls(request.POST, instance=checkout.customer) if form.is_valid(): customer = form.save() checkout.customer = customer checkout.save() messages.success(request, 'Your details have been saved') return HttpResponseRedirect(reverse('cccheckout:postage')) messages.error(request, 'There were problems with the information' ' you\'ve entered') return render_to_response('cccheckout/customer.html', { 'form': form, 'discountform': discountform, 'checkout': checkout}, context_instance=RequestContext(request))
def _prepare_checkout(request, *args, **kwargs): # now that we have items set up the checkout # get the module.adapter from the settings adapter_path = getattr(django_settings, 'CCCHECKOUT_ADAPTER', c_settings.CCCHECKOUT_ADAPTER) # import the module to fire the monkey patching adapter try: module, adapter_func = import_thing(adapter_path) except ImportError, e: raise Exception('Configure me properly! %s' % e)
def clean_code(self): """If there is a matchingt discount for the code this will return True else it will not """ code = self.cleaned_data['code'] for model in c_settings.CCCHECKOUT_DISCOUNT_MODELS: try: module, model = import_thing(model) except (ImportError, AttributeError), e: continue try: self.discount = model.objects.get(code=code) return self.cleaned_data except model.DoesNotExist: pass
def payment(request, *args, **kwargs): """Confirms to customer and then begins the payment process""" checkout = request.session['cccheckout'] # ensure that we have a customer and postage if required if c_settings.CCCHECKOUT_CUSTOMER_FORM is not None and \ checkout.customer is None: return HttpResponseRedirect(reverse('cccheckout:customer')) # if there is postage models but checkout doesnt have one then redirect if len(c_settings.CCCHECKOUT_POSTAGE_MODELS) > 0 and \ checkout.postage is None: return HttpResponseRedirect(reverse('cccheckout:postage')) discountform = kwargs.get('discountform', None) # now import the forms forms = [] for form in c_settings.CCCHECKOUT_PAYMENT_FORMS: form_mod, form_cls = import_thing(form) form = form_cls(checkout=checkout) forms.append(form) return render_to_response('cccheckout/payment.html', { 'forms': forms, 'checkout': checkout, 'discountform': discountform}, context_instance=RequestContext(request))