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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. # Shipping addresses
  45. def ship_to_user_address(self, address):
  46. """
  47. Set existing shipping address id to session and unset address fields from session
  48. """
  49. self._set('shipping', 'user_address_id', address.id)
  50. self._unset('shipping', 'new_address_fields')
  51. def ship_to_new_address(self, address_fields):
  52. """
  53. Set new shipping address details to session and unset shipping address id
  54. """
  55. self._set('shipping', 'new_address_fields', address_fields)
  56. self._unset('shipping', 'user_address_id')
  57. def new_shipping_address_fields(self):
  58. """
  59. Get shipping address fields from session
  60. """
  61. return self._get('shipping', 'new_address_fields')
  62. def user_address_id(self):
  63. """
  64. Get user address id from session
  65. """
  66. return self._get('shipping', 'user_address_id')
  67. def is_shipping_address_set(self):
  68. new_fields = self.new_shipping_address_fields()
  69. has_new_address = new_fields and len(new_fields) > 0
  70. has_old_address = self.user_address_id() > 0
  71. return has_new_address or has_old_address
  72. # Shipping methods
  73. def use_free_shipping(self):
  74. """
  75. Set "free shipping" code to session
  76. """
  77. self._set('shipping', 'method_code', '__free__')
  78. def use_shipping_method(self, code):
  79. """
  80. Set shipping method code to session
  81. """
  82. self._set('shipping', 'method_code', code)
  83. def shipping_method(self):
  84. """
  85. Returns the shipping method model based on the
  86. data stored in the session.
  87. """
  88. code = self._get('shipping', 'method_code')
  89. if not code:
  90. return None
  91. return Repository().find_by_code(code)
  92. def is_shipping_method_set(self):
  93. return bool(self._get('shipping', 'method_code'))
  94. # Billing address fields
  95. def bill_to_new_address(self, address_fields):
  96. """
  97. Store address fields for a billing address.
  98. """
  99. self._set('billing', 'new_address_fields', address_fields)
  100. def new_billing_address_fields(self):
  101. """
  102. Return fields for a billing address
  103. """
  104. return self._get('billing', 'new_address_fields')
  105. def billing_address_same_as_shipping(self):
  106. """
  107. Record fact that the billing address is to be the same as
  108. the shipping address.
  109. """
  110. self._set('payment', 'billing_address_same_as_shipping', True)
  111. def is_billing_address_same_as_shipping(self):
  112. return self._get('payment', 'billing_address_same_as_shipping', False)
  113. # Payment methods
  114. def pay_by(self, method):
  115. self._set('payment', 'method', method)
  116. def payment_method(self):
  117. return self._get('payment', 'method')
  118. # Submission methods
  119. def set_order_number(self, order_number):
  120. self._set('submission', 'order_number', order_number)
  121. def get_order_number(self):
  122. return self._get('submission', 'order_number')
  123. def set_submitted_basket(self, basket):
  124. self._set('submission', 'basket_id', basket.id)
  125. def get_submitted_basket_id(self):
  126. return self._get('submission', 'basket_id')