Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

utils.py 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. from django.core.urlresolvers import resolve
  2. from oscar.services import import_module
  3. shipping_methods = import_module('shipping.repository', ['Repository'])
  4. class ProgressChecker(object):
  5. u"""
  6. Class for testing whether the appropriate steps of the checkout
  7. have been completed.
  8. """
  9. # List of URL names that have to be completed (in this order)
  10. urls_for_steps = ['oscar-checkout-shipping-address',
  11. 'oscar-checkout-shipping-method',
  12. 'oscar-checkout-payment-method',
  13. 'oscar-checkout-preview',
  14. 'oscar-checkout-payment-details',
  15. 'oscar-checkout-submit',]
  16. def are_previous_steps_complete(self, request):
  17. u"""
  18. Checks whether the previous checkout steps have been completed.
  19. This uses the URL-name and the class-level list of required
  20. steps.
  21. """
  22. # Extract the URL name from the path
  23. complete_steps = self._get_completed_steps(request)
  24. try:
  25. url_name = self._get_url_name(request)
  26. current_step_index = self.urls_for_steps.index(url_name)
  27. last_completed_step_index = len(complete_steps) - 1
  28. return current_step_index <= last_completed_step_index + 1
  29. except ValueError:
  30. # Can't find current step index - must be manipulation
  31. return False
  32. except IndexError:
  33. # No complete steps - only allowed to be on first page
  34. return current_step_index == 0
  35. def step_complete(self, request):
  36. u"""Record a checkout step as complete."""
  37. url_name = self._get_url_name(request)
  38. complete_steps = self._get_completed_steps(request)
  39. if not url_name in complete_steps:
  40. # Only add name if this is the first time the step
  41. # has been completed.
  42. complete_steps.append(url_name)
  43. request.session['checkout_complete_steps'] = complete_steps
  44. def get_next_step(self, request):
  45. u"""Returns the next incomplete step of the checkout."""
  46. completed_steps = self._get_completed_steps(request)
  47. if len(completed_steps):
  48. return self.urls_for_steps[len(completed_steps)]
  49. else:
  50. return self.urls_for_steps[0]
  51. def all_steps_complete(self, request):
  52. u"""
  53. Order has been submitted - clear the completed steps from
  54. the session.
  55. """
  56. request.session['checkout_complete_steps'] = []
  57. def _get_url_name(self, request):
  58. return resolve(request.path).url_name
  59. def _get_completed_steps(self, request):
  60. return request.session.get('checkout_complete_steps', [])
  61. class CheckoutSessionData(object):
  62. u"""Class responsible for marshalling all the checkout session data."""
  63. SESSION_KEY = 'checkout_data'
  64. def __init__(self, request):
  65. self.request = request
  66. if self.SESSION_KEY not in self.request.session:
  67. self.request.session[self.SESSION_KEY] = {}
  68. def _check_namespace(self, namespace):
  69. if namespace not in self.request.session[self.SESSION_KEY]:
  70. self.request.session[self.SESSION_KEY][namespace] = {}
  71. def _get(self, namespace, key):
  72. self._check_namespace(namespace)
  73. if key in self.request.session[self.SESSION_KEY][namespace]:
  74. return self.request.session[self.SESSION_KEY][namespace][key]
  75. return None
  76. def _set(self, namespace, key, value):
  77. self._check_namespace(namespace)
  78. self.request.session[self.SESSION_KEY][namespace][key] = value
  79. self.request.session.modified = True
  80. def _unset(self, namespace, key):
  81. self._check_namespace(namespace)
  82. if key in self.request.session[self.SESSION_KEY][namespace]:
  83. del self.request.session[self.SESSION_KEY][namespace][key]
  84. self.request.session.modified = True
  85. def flush(self):
  86. self.request.session[self.SESSION_KEY] = {}
  87. # Shipping methods
  88. def ship_to_user_address(self, address):
  89. self._set('shipping', 'user_address_id', address.id)
  90. self._unset('shipping', 'new_address_fields')
  91. def ship_to_new_address(self, address_fields):
  92. self._set('shipping', 'new_address_fields', address_fields)
  93. self._unset('shipping', 'user_address_id')
  94. def new_address_fields(self):
  95. return self._get('shipping', 'new_address_fields')
  96. def user_address_id(self):
  97. return self._get('shipping', 'user_address_id')
  98. def use_shipping_method(self, code):
  99. self._set('shipping', 'method_code', code)
  100. def shipping_method(self):
  101. u"""
  102. Returns the shipping method model based on the
  103. data stored in the session.
  104. """
  105. code = self._get('shipping', 'method_code')
  106. if not code:
  107. return None
  108. repo = shipping_methods.Repository()
  109. return repo.find_by_code(code)