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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import logging
  2. from django.db.models import get_model
  3. from oscar.apps.shipping.methods import Free
  4. from oscar.core.loading import get_class
  5. OrderTotalCalculator = get_class('checkout.calculators', 'OrderTotalCalculator')
  6. CheckoutSessionData = get_class('checkout.utils', 'CheckoutSessionData')
  7. ShippingAddress = get_model('order', 'ShippingAddress')
  8. UserAddress = get_model('address', 'UserAddress')
  9. # Standard logger for checkout events
  10. logger = logging.getLogger('oscar.checkout')
  11. class CheckoutSessionMixin(object):
  12. """
  13. Mixin to provide common functionality shared between checkout views.
  14. """
  15. def dispatch(self, request, *args, **kwargs):
  16. self.checkout_session = CheckoutSessionData(request)
  17. return super(CheckoutSessionMixin, self).dispatch(request, *args, **kwargs)
  18. def get_shipping_address(self):
  19. """
  20. Return the current shipping address for this checkout session.
  21. This could either be a ShippingAddress model which has been
  22. pre-populated (not saved), or a UserAddress model which will
  23. need converting into a ShippingAddress model at submission
  24. """
  25. addr_data = self.checkout_session.new_shipping_address_fields()
  26. if addr_data:
  27. # Load address data into a blank address model
  28. return ShippingAddress(**addr_data)
  29. addr_id = self.checkout_session.user_address_id()
  30. if addr_id:
  31. try:
  32. return UserAddress._default_manager.get(pk=addr_id)
  33. except UserAddress.DoesNotExist:
  34. # This can happen if you reset all your tables and you still
  35. # have session data that refers to addresses that no longer
  36. # exist.
  37. pass
  38. return None
  39. def get_shipping_method(self, basket=None):
  40. method = self.checkout_session.shipping_method(basket)
  41. # We default to using free shipping
  42. if not method:
  43. method = Free()
  44. return method
  45. def get_order_totals(self, basket=None, shipping_method=None, **kwargs):
  46. """
  47. Returns the total for the order with and without tax (as a tuple)
  48. """
  49. calc = OrderTotalCalculator(self.request)
  50. if not basket:
  51. basket = self.request.basket
  52. if not shipping_method:
  53. shipping_method = self.get_shipping_method(basket)
  54. total_incl_tax = calc.order_total_incl_tax(basket, shipping_method, **kwargs)
  55. total_excl_tax = calc.order_total_excl_tax(basket, shipping_method, **kwargs)
  56. return total_incl_tax, total_excl_tax
  57. def get_context_data(self, **kwargs):
  58. """
  59. Assign common template variables to the context.
  60. """
  61. ctx = super(CheckoutSessionMixin, self).get_context_data(**kwargs)
  62. ctx['shipping_address'] = self.get_shipping_address()
  63. method = self.get_shipping_method()
  64. if method:
  65. ctx['shipping_method'] = method
  66. ctx['shipping_total_excl_tax'] = method.basket_charge_excl_tax()
  67. ctx['shipping_total_incl_tax'] = method.basket_charge_incl_tax()
  68. ctx['order_total_incl_tax'], ctx['order_total_excl_tax'] = self.get_order_totals()
  69. return ctx