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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. from django.core.urlresolvers import resolve
  2. from oscar.services import import_module
  3. shipping_models = import_module('shipping.models', ['Method'])
  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. FREE_SHIPPING = '__free__'
  65. def __init__(self, request):
  66. self.request = request
  67. if self.SESSION_KEY not in self.request.session:
  68. self.request.session[self.SESSION_KEY] = {}
  69. def _check_namespace(self, namespace):
  70. if namespace not in self.request.session[self.SESSION_KEY]:
  71. self.request.session[self.SESSION_KEY][namespace] = {}
  72. def _get(self, namespace, key):
  73. u"""Return session value or None"""
  74. self._check_namespace(namespace)
  75. if key in self.request.session[self.SESSION_KEY][namespace]:
  76. return self.request.session[self.SESSION_KEY][namespace][key]
  77. return None
  78. def _set(self, namespace, key, value):
  79. u"""Set session value"""
  80. self._check_namespace(namespace)
  81. self.request.session[self.SESSION_KEY][namespace][key] = value
  82. self.request.session.modified = True
  83. def _unset(self, namespace, key):
  84. u"""Unset session value"""
  85. self._check_namespace(namespace)
  86. if key in self.request.session[self.SESSION_KEY][namespace]:
  87. del self.request.session[self.SESSION_KEY][namespace][key]
  88. self.request.session.modified = True
  89. def flush(self):
  90. u"""Delete session key"""
  91. self.request.session[self.SESSION_KEY] = {}
  92. # Shipping methods
  93. def ship_to_user_address(self, address):
  94. u"""Set existing shipping address id to session and unset address fields from session"""
  95. self._set('shipping', 'user_address_id', address.id)
  96. self._unset('shipping', 'new_address_fields')
  97. def ship_to_new_address(self, address_fields):
  98. u"""Set new shipping address details to session and unset shipping address id"""
  99. self._set('shipping', 'new_address_fields', address_fields)
  100. self._unset('shipping', 'user_address_id')
  101. def new_address_fields(self):
  102. u"""Get shipping address fields from session"""
  103. return self._get('shipping', 'new_address_fields')
  104. def user_address_id(self):
  105. u"""Get user address id from session"""
  106. return self._get('shipping', 'user_address_id')
  107. def use_free_shipping(self):
  108. u"""Set "free shipping" code to session"""
  109. self._set('shipping', 'method_code', '__free__')
  110. def use_shipping_method(self, code):
  111. u"""Set shipping method code to session"""
  112. self._set('shipping', 'method_code', code)
  113. def shipping_method(self):
  114. u"""
  115. Returns the shipping method model based on the
  116. data stored in the session.
  117. """
  118. code = self._get('shipping', 'method_code')
  119. if code == self.FREE_SHIPPING:
  120. method = shipping_models.Method(name="Standard shipping (Free)", code=self.FREE_SHIPPING)
  121. else:
  122. method = shipping_models.Method.objects.get(code=code)
  123. return method