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.2KB

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