Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

session.py 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. from django.db.models import get_model
  2. from oscar.core.loading import get_class
  3. OrderTotalCalculator = get_class(
  4. 'checkout.calculators', 'OrderTotalCalculator')
  5. CheckoutSessionData = get_class(
  6. 'checkout.utils', 'CheckoutSessionData')
  7. ShippingAddress = get_model('order', 'ShippingAddress')
  8. UserAddress = get_model('address', 'UserAddress')
  9. class CheckoutSessionMixin(object):
  10. """
  11. Mixin to provide common functionality shared between checkout views.
  12. """
  13. def dispatch(self, request, *args, **kwargs):
  14. self.checkout_session = CheckoutSessionData(request)
  15. return super(CheckoutSessionMixin, self).dispatch(
  16. request, *args, **kwargs)
  17. def get_context_data(self, **kwargs):
  18. """
  19. Assign common template variables to the context.
  20. """
  21. ctx = super(CheckoutSessionMixin, self).get_context_data(**kwargs)
  22. basket = self.request.basket
  23. shipping_address = self.get_shipping_address(basket)
  24. shipping_method = self.get_shipping_method(
  25. basket, shipping_address)
  26. ctx['shipping_address'] = shipping_address
  27. ctx['shipping_method'] = shipping_method
  28. if basket and shipping_method:
  29. ctx['order_total'] = self.get_order_totals(basket, shipping_method)
  30. return ctx
  31. def get_shipping_address(self, basket):
  32. """
  33. Return the (unsaved) shipping address for this checkout session.
  34. If the shipping address was entered manually, then we instanciate a
  35. ShippingAddress model with the appropriate form data.
  36. If the shipping address was selected from the user's address book,
  37. then we convert the UserAddress to a ShippingAddress.
  38. The ShippingAddress instance is not saved as sometimes you need a
  39. shipping address instance before the order is placed. For example, if
  40. you are submitting fraud information as part of a payment request.
  41. The create_shipping_address method is responsible for saving a shipping
  42. address when an order is placed.
  43. """
  44. if not basket.is_shipping_required():
  45. return None
  46. addr_data = self.checkout_session.new_shipping_address_fields()
  47. if addr_data:
  48. # Load address data into a blank address model
  49. return ShippingAddress(**addr_data)
  50. addr_id = self.checkout_session.user_address_id()
  51. if addr_id:
  52. try:
  53. address = UserAddress._default_manager.get(pk=addr_id)
  54. except UserAddress.DoesNotExist:
  55. # This can happen if you reset all your tables and you still
  56. # have session data that refers to addresses that no longer
  57. # exist.
  58. pass
  59. else:
  60. shipping_addr = ShippingAddress()
  61. address.populate_alternative_model(shipping_addr)
  62. return shipping_addr
  63. return None
  64. def get_shipping_method(self, basket, shipping_address=None, **kwargs):
  65. """
  66. Return the selected shipping method instance from this checkout session
  67. The shipping address is passed as this is sometimes needed to determine
  68. the tax applicable on a shipping method.
  69. """
  70. return self.checkout_session.shipping_method(basket)
  71. def get_order_totals(self, basket, shipping_method, **kwargs):
  72. """
  73. Returns the total for the order with and without tax (as a tuple)
  74. """
  75. return OrderTotalCalculator(self.request).calculate(
  76. basket, shipping_method, **kwargs)