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.

utils.py 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. from oscar.core.loading import get_class
  2. Repository = get_class('shipping.repository', 'Repository')
  3. class CheckoutSessionData(object):
  4. """
  5. Class responsible for marshalling all the checkout session data
  6. """
  7. SESSION_KEY = 'checkout_data'
  8. def __init__(self, request):
  9. self.request = request
  10. if self.SESSION_KEY not in self.request.session:
  11. self.request.session[self.SESSION_KEY] = {}
  12. def _check_namespace(self, namespace):
  13. if namespace not in self.request.session[self.SESSION_KEY]:
  14. self.request.session[self.SESSION_KEY][namespace] = {}
  15. def _get(self, namespace, key, default=None):
  16. """
  17. Return session value or None
  18. """
  19. self._check_namespace(namespace)
  20. if key in self.request.session[self.SESSION_KEY][namespace]:
  21. return self.request.session[self.SESSION_KEY][namespace][key]
  22. return default
  23. def _set(self, namespace, key, value):
  24. """
  25. Set session value
  26. """
  27. self._check_namespace(namespace)
  28. self.request.session[self.SESSION_KEY][namespace][key] = value
  29. self.request.session.modified = True
  30. def _unset(self, namespace, key):
  31. """
  32. Unset session value
  33. """
  34. self._check_namespace(namespace)
  35. if key in self.request.session[self.SESSION_KEY][namespace]:
  36. del self.request.session[self.SESSION_KEY][namespace][key]
  37. self.request.session.modified = True
  38. def flush(self):
  39. """
  40. Delete session key
  41. """
  42. self.request.session[self.SESSION_KEY] = {}
  43. # Guest checkout
  44. def set_guest_email(self, email):
  45. self._set('guest', 'email', email)
  46. def get_guest_email(self):
  47. return self._get('guest', 'email')
  48. # Shipping addresses
  49. def ship_to_user_address(self, address):
  50. """
  51. Set existing shipping address id to session and unset address fields from session
  52. """
  53. self._set('shipping', 'user_address_id', address.id)
  54. self._unset('shipping', 'new_address_fields')
  55. def ship_to_new_address(self, address_fields):
  56. """
  57. Set new shipping address details to session and unset shipping address id
  58. """
  59. self._set('shipping', 'new_address_fields', address_fields)
  60. self._unset('shipping', 'user_address_id')
  61. def new_shipping_address_fields(self):
  62. """
  63. Get shipping address fields from session
  64. """
  65. return self._get('shipping', 'new_address_fields')
  66. def user_address_id(self):
  67. """
  68. Get user address id from session
  69. """
  70. return self._get('shipping', 'user_address_id')
  71. def is_shipping_address_set(self):
  72. new_fields = self.new_shipping_address_fields()
  73. has_new_address = new_fields is not None
  74. has_old_address = self.user_address_id() > 0
  75. return has_new_address or has_old_address
  76. # Shipping methods
  77. def use_free_shipping(self):
  78. """
  79. Set "free shipping" code to session
  80. """
  81. self._set('shipping', 'method_code', '__free__')
  82. def use_shipping_method(self, code):
  83. """
  84. Set shipping method code to session
  85. """
  86. self._set('shipping', 'method_code', code)
  87. def shipping_method(self):
  88. """
  89. Returns the shipping method model based on the
  90. data stored in the session.
  91. """
  92. code = self._get('shipping', 'method_code')
  93. if not code:
  94. return None
  95. return Repository().find_by_code(code)
  96. def is_shipping_method_set(self):
  97. return bool(self._get('shipping', 'method_code'))
  98. # Billing address fields
  99. def bill_to_new_address(self, address_fields):
  100. """
  101. Store address fields for a billing address.
  102. """
  103. self._set('billing', 'new_address_fields', address_fields)
  104. def new_billing_address_fields(self):
  105. """
  106. Return fields for a billing address
  107. """
  108. return self._get('billing', 'new_address_fields')
  109. def billing_address_same_as_shipping(self):
  110. """
  111. Record fact that the billing address is to be the same as
  112. the shipping address.
  113. """
  114. self._set('payment', 'billing_address_same_as_shipping', True)
  115. def is_billing_address_same_as_shipping(self):
  116. return self._get('payment', 'billing_address_same_as_shipping', False)
  117. # Payment methods
  118. def pay_by(self, method):
  119. self._set('payment', 'method', method)
  120. def payment_method(self):
  121. return self._get('payment', 'method')
  122. # Submission methods
  123. def set_order_number(self, order_number):
  124. self._set('submission', 'order_number', order_number)
  125. def get_order_number(self):
  126. return self._get('submission', 'order_number')
  127. def set_submitted_basket(self, basket):
  128. self._set('submission', 'basket_id', basket.id)
  129. def get_submitted_basket_id(self):
  130. return self._get('submission', 'basket_id')