# -*- coding: utf-8 -*- This models the checkout process using views. """ get_billing_address_from_request, assign_address_to_request
""" Returns a dynamic ModelForm from the loaded AddressModel """ exclude=['user_shipping', 'user_billing'])
""" Provided for extensibility. """
""" Provided for extensibility. """ return self._get_dynamic_form_class_from_factory()
""" This will create an Order object form the current cart, and will pass a reference to the Order on either the User object or the session. """
""" Initializes and handles the form for the shipping address. AddressModel is a model of the type defined in settings.SHOP_ADDRESS_MODEL.
The trick here is that we generate a ModelForm for whatever model was passed to us by the SHOP_ADDRESS_MODEL setting, and us this, prefixed, as the shipping address form. So this can be as complex or as simple as one wants.
Subclasses of this view can obviously override this method and return any other form instead. """ # Try to get the cached version first. # Create a dynamic Form class for the model specified as the address model
# Try to get a shipping address instance from the request (user or session)) else: # We should either have an instance, or None # The user or guest doesn't already have a favorite address. # Instanciate a blank one, and use this as the default value for # the form.
# Instanciate the form
""" Initializes and handles the form for the shipping address. AddressModel is a model of the type defined in settings.SHOP_ADDRESS_MODEL """ # Try to get the cached version first. # Create a dynamic Form class for the model specified as the address model exclude=['user_shipping', 'user_billing'])
# Try to get a shipping address instance from the request (user or session)) else: # We should either have an instance, or None # The user or guest doesn't already have a favorite address. # Instansiate a blank one, and use this as the default value for # the form.
#Instanciate the form
""" Get (and cache) the BillingShippingForm instance """ else:
""" Provided for extensibility. Adds both addresses (shipping and billing addresses to the Order object. """
""" Called when view is POSTed """ shipping_form = self.get_shipping_address_form() billing_form = self.get_billing_address_form() if shipping_form.is_valid() and billing_form.is_valid():
# Add the address to the order shipping_address = shipping_form.save() billing_address = billing_form.save() order = self.create_order_object_from_cart()
self.save_addresses_to_order(order, shipping_address, billing_address)
# The following marks addresses as being default addresses for shipping # and billing. For more options (amazon style), we should remove this assign_address_to_request(self.request, shipping_address, shipping=True) assign_address_to_request(self.request, billing_address, shipping=False)
billingshipping_form = self.get_billing_and_shipping_selection_form() if billingshipping_form.is_valid(): self.request.session['payment_backend'] = billingshipping_form.cleaned_data['payment_method'] self.request.session['shipping_backend'] = billingshipping_form.cleaned_data['shipping_method'] return HttpResponseRedirect(reverse('checkout_shipping'))
return self.get(self, *args, **kwargs)
""" This overrides the context from the normal template view """
'shipping_address':shipping_address_form, 'billing_address':billing_address_form, 'billing_shipping_form':billingshipping_form, })
ctx = super(ShopTemplateView, self).get_context_data(**kwargs)
# Set the order status: order = get_order_from_request(self.request) if order: order.status = Order.COMPLETED order.save() completed.send(sender=self, order=order) else: order = Order.objects.get_latest_for_user(self.request.user) ctx.update({'order': order,})
# Empty the customers basket, to reflect that the purchase was completed cart_object = get_or_create_cart(self.request) cart_object.empty()
return ctx
try: backend_namespace = self.request.session.pop('shipping_backend') return HttpResponseRedirect(reverse(backend_namespace)) except KeyError: return HttpResponseRedirect(reverse('cart'))
try: backend_namespace = self.request.session.pop('payment_backend') return HttpResponseRedirect(reverse(backend_namespace)) except KeyError: return HttpResponseRedirect(reverse('cart')) |