Coverage for /home/tribaal/workspace/django-shop/shop/views/checkout : 71.26%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# -*- coding: utf-8 -*- This models the checkout process using views. """
assign_address_to_request, get_billing_address_from_request, get_shipping_address_from_request, )
""" Returns a dynamic ModelForm from the loaded AddressModel """ AddressModel, exclude=['user_shipping', 'user_billing'])
""" Provided for extensibility. """
""" Provided for extensibility. """
""" 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)) instance=shipping_address) 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
# Try to get a shipping address instance from the request (user or # session)) instance=billing_address) 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:
billing_address): """ Provided for extensibility.
Adds both addresses (shipping and billing addresses) to the Order object. """
""" Initializes and handles the form for order extra info. """ # Try to get the cached version first. # Create a dynamic Form class for the model form = form_class(self.request.POST) else:
if form.cleaned_data.get('text'): extra_info = form.save(commit=False) extra_info.order = order extra_info.save()
""" Called when view is POSTed """ shipping_form = self.get_shipping_address_form() billing_form = self.get_billing_address_form() extra_info_form = self.get_extra_info_form() if shipping_form.is_valid() and billing_form.is_valid() and extra_info_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(): # save selected billing and shipping methods self.request.session['payment_backend'] = \ billingshipping_form.cleaned_data['payment_method'] self.request.session['shipping_backend'] = \ billingshipping_form.cleaned_data['shipping_method']
# add extra info to order self.save_extra_info_to_order(order, extra_info_form)
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, 'extra_info_form': extra_info_form, })
order = get_order_from_request(self.request) order.status = Order.CONFIRMED order.save()
self.confirm_order() return super(OrderConfirmView, self).get(request, *args, **kwargs)
self.url = reverse(self.url_name) return super(OrderConfirmView, self).get_redirect_url(**kwargs)
# put the latest order in the context only if it is completed
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')) |