您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

utils.py 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. from django.core.urlresolvers import resolve
  2. from oscar.core.loading 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. def are_previous_steps_complete(self, request):
  16. u"""
  17. Checks whether the previous checkout steps have been completed.
  18. This uses the URL-name and the class-level list of required
  19. steps.
  20. """
  21. # Extract the URL name from the path
  22. complete_steps = self._get_completed_steps(request)
  23. try:
  24. url_name = self._get_url_name(request)
  25. current_step_index = self.urls_for_steps.index(url_name)
  26. last_completed_step_index = len(complete_steps) - 1
  27. return current_step_index <= last_completed_step_index + 1
  28. except ValueError:
  29. # Can't find current step index - must be manipulation
  30. return False
  31. except IndexError:
  32. # No complete steps - only allowed to be on first page
  33. return current_step_index == 0
  34. def step_complete(self, request):
  35. u"""Record a checkout step as complete."""
  36. url_name = self._get_url_name(request)
  37. complete_steps = self._get_completed_steps(request)
  38. if not url_name in complete_steps:
  39. # Only add name if this is the first time the step
  40. # has been completed.
  41. complete_steps.append(url_name)
  42. request.session['checkout_complete_steps'] = complete_steps
  43. def get_next_step(self, request):
  44. u"""Returns the next incomplete step of the checkout."""
  45. url_name = self._get_url_name(request)
  46. current_step_index = self.urls_for_steps.index(url_name)
  47. return self.urls_for_steps[current_step_index+1]
  48. def all_steps_complete(self, request):
  49. u"""
  50. Order has been submitted - clear the completed steps from
  51. the session.
  52. """
  53. request.session['checkout_complete_steps'] = []
  54. def _get_url_name(self, request):
  55. return resolve(request.path).url_name
  56. def _get_completed_steps(self, request):
  57. return request.session.get('checkout_complete_steps', [])
  58. class CheckoutSessionData(object):
  59. u"""Class responsible for marshalling all the checkout session data."""
  60. SESSION_KEY = 'checkout_data'
  61. def __init__(self, request):
  62. self.request = request
  63. if self.SESSION_KEY not in self.request.session:
  64. self.request.session[self.SESSION_KEY] = {}
  65. def _check_namespace(self, namespace):
  66. if namespace not in self.request.session[self.SESSION_KEY]:
  67. self.request.session[self.SESSION_KEY][namespace] = {}
  68. def _get(self, namespace, key):
  69. u"""Return session value or None"""
  70. self._check_namespace(namespace)
  71. if key in self.request.session[self.SESSION_KEY][namespace]:
  72. return self.request.session[self.SESSION_KEY][namespace][key]
  73. return None
  74. def _set(self, namespace, key, value):
  75. u"""Set session value"""
  76. self._check_namespace(namespace)
  77. self.request.session[self.SESSION_KEY][namespace][key] = value
  78. self.request.session.modified = True
  79. def _unset(self, namespace, key):
  80. u"""Unset session value"""
  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. u"""Delete session key"""
  87. self.request.session[self.SESSION_KEY] = {}
  88. # Shipping methods
  89. def ship_to_user_address(self, address):
  90. u"""Set existing shipping address id to session and unset address fields from session"""
  91. self._set('shipping', 'user_address_id', address.id)
  92. self._unset('shipping', 'new_address_fields')
  93. def ship_to_new_address(self, address_fields):
  94. u"""Set new shipping address details to session and unset shipping address id"""
  95. self._set('shipping', 'new_address_fields', address_fields)
  96. self._unset('shipping', 'user_address_id')
  97. def new_address_fields(self):
  98. u"""Get shipping address fields from session"""
  99. return self._get('shipping', 'new_address_fields')
  100. def user_address_id(self):
  101. u"""Get user address id from session"""
  102. return self._get('shipping', 'user_address_id')
  103. def use_free_shipping(self):
  104. u"""Set "free shipping" code to session"""
  105. self._set('shipping', 'method_code', '__free__')
  106. def use_shipping_method(self, code):
  107. u"""Set shipping method code to session"""
  108. self._set('shipping', 'method_code', code)
  109. def shipping_method(self):
  110. u"""
  111. Returns the shipping method model based on the
  112. data stored in the session.
  113. """
  114. code = self._get('shipping', 'method_code')
  115. if not code:
  116. return None
  117. repo = shipping_methods.Repository()
  118. return repo.find_by_code(code)
  119. # Payment methods
  120. def pay_by(self, method):
  121. self._set('payment', 'method', method)
  122. def payment_method(self):
  123. return self._get('payment', 'method')