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.

session.py 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 have
  35. # session data that refers to addresses that no longer exist
  36. pass
  37. return None
  38. def get_shipping_method(self, basket=None):
  39. method = self.checkout_session.shipping_method(basket)
  40. # We default to using free shipping
  41. if not method:
  42. method = Free()
  43. return method
  44. def get_order_totals(self, basket=None, shipping_method=None, **kwargs):
  45. """
  46. Returns the total for the order with and without tax (as a tuple)
  47. """
  48. calc = OrderTotalCalculator(self.request)
  49. if not basket:
  50. basket = self.request.basket
  51. if not shipping_method:
  52. shipping_method = self.get_shipping_method(basket)
  53. total_incl_tax = calc.order_total_incl_tax(basket, shipping_method, **kwargs)
  54. total_excl_tax = calc.order_total_excl_tax(basket, shipping_method, **kwargs)
  55. return total_incl_tax, total_excl_tax
  56. def get_context_data(self, **kwargs):
  57. """
  58. Assign common template variables to the context.
  59. """
  60. ctx = super(CheckoutSessionMixin, self).get_context_data(**kwargs)
  61. ctx['shipping_address'] = self.get_shipping_address()
  62. method = self.get_shipping_method()
  63. if method:
  64. ctx['shipping_method'] = method
  65. ctx['shipping_total_excl_tax'] = method.basket_charge_excl_tax()
  66. ctx['shipping_total_incl_tax'] = method.basket_charge_incl_tax()
  67. ctx['order_total_incl_tax'], ctx['order_total_excl_tax'] = self.get_order_totals()
  68. return ctx