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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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_namespace(self, namespace):
  39. self.request.session[self.SESSION_KEY][namespace] = {}
  40. self.request.session.modified = True
  41. def flush(self):
  42. """
  43. Delete session key
  44. """
  45. self.request.session[self.SESSION_KEY] = {}
  46. # Guest checkout
  47. def set_guest_email(self, email):
  48. self._set('guest', 'email', email)
  49. def get_guest_email(self):
  50. return self._get('guest', 'email')
  51. # Shipping address
  52. # ================
  53. # Options:
  54. # 1. No shipping required (eg digital products)
  55. # 2. Ship to new address (entered in a form)
  56. # 3. Ship to an addressbook address (address chosen from list)
  57. def reset_shipping_data(self):
  58. self._flush_namespace('shipping')
  59. def ship_to_user_address(self, address):
  60. """
  61. Set existing shipping address id to session and unset address fields from session
  62. """
  63. self.reset_shipping_data()
  64. self._set('shipping', 'user_address_id', address.id)
  65. def ship_to_new_address(self, address_fields):
  66. """
  67. Set new shipping address details to session and unset shipping address id
  68. """
  69. self._unset('shipping', 'new_address_fields')
  70. self._set('shipping', 'new_address_fields', address_fields)
  71. def new_shipping_address_fields(self):
  72. """
  73. Get shipping address fields from session
  74. """
  75. return self._get('shipping', 'new_address_fields')
  76. def shipping_user_address_id(self):
  77. """
  78. Get user address id from session
  79. """
  80. return self._get('shipping', 'user_address_id')
  81. user_address_id = shipping_user_address_id
  82. def is_shipping_address_set(self):
  83. """
  84. Test whether a shipping address has been stored in the session.
  85. This can be from a new address or re-using an existing address.
  86. """
  87. new_fields = self.new_shipping_address_fields()
  88. has_new_address = new_fields is not None
  89. has_old_address = self.user_address_id() > 0
  90. return has_new_address or has_old_address
  91. # Shipping method
  92. # ===============
  93. def use_free_shipping(self):
  94. """
  95. Set "free shipping" code to session
  96. """
  97. self._set('shipping', 'method_code', '__free__')
  98. def use_shipping_method(self, code):
  99. """
  100. Set shipping method code to session
  101. """
  102. self._set('shipping', 'method_code', code)
  103. def shipping_method(self, basket):
  104. """
  105. Returns the shipping method model based on the
  106. data stored in the session.
  107. """
  108. code = self._get('shipping', 'method_code')
  109. if not code:
  110. return None
  111. return Repository().find_by_code(code, basket)
  112. def is_shipping_method_set(self, basket):
  113. """
  114. Test if a valid shipping method is stored in the session
  115. """
  116. return self.shipping_method(basket) is not None
  117. # Billing address fields
  118. # ======================
  119. #
  120. # There are 3 common options:
  121. # 1. Billing address is entered manually through a form
  122. # 2. Billing address is selected from address book
  123. # 3. Billing address is the same as the shipping address
  124. def bill_to_new_address(self, address_fields):
  125. """
  126. Store address fields for a billing address.
  127. """
  128. self._flush_namespace('billing')
  129. self._set('billing', 'new_address_fields', address_fields)
  130. def bill_to_user_address(self, address):
  131. """
  132. Set an address from a user's address book as the billing address
  133. :address: The address object
  134. """
  135. self._flush_namespace('billing')
  136. self._set('billing', 'user_address_id', address.id)
  137. def bill_to_shipping_address(self):
  138. """
  139. Record fact that the billing address is to be the same as
  140. the shipping address.
  141. """
  142. self._flush_namespace('billing')
  143. self._set('billing', 'billing_address_same_as_shipping', True)
  144. # Legacy method name
  145. billing_address_same_as_shipping = bill_to_shipping_address
  146. def is_billing_address_same_as_shipping(self):
  147. return self._get('billing', 'billing_address_same_as_shipping', False)
  148. def billing_user_address_id(self):
  149. """
  150. Return the ID of the user address being used for billing
  151. """
  152. return self._get('billing', 'user_address_id')
  153. def new_billing_address_fields(self):
  154. """
  155. Return fields for a billing address
  156. """
  157. return self._get('billing', 'new_address_fields')
  158. # Payment methods
  159. # ===============
  160. def pay_by(self, method):
  161. self._set('payment', 'method', method)
  162. def payment_method(self):
  163. return self._get('payment', 'method')
  164. # Submission methods
  165. def set_order_number(self, order_number):
  166. self._set('submission', 'order_number', order_number)
  167. def get_order_number(self):
  168. return self._get('submission', 'order_number')
  169. def set_submitted_basket(self, basket):
  170. self._set('submission', 'basket_id', basket.id)
  171. def get_submitted_basket_id(self):
  172. return self._get('submission', 'basket_id')