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.

views.py 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. import logging
  2. from django.http import Http404, HttpResponseRedirect, HttpResponseBadRequest
  3. from django.core.urlresolvers import reverse, reverse_lazy
  4. from django.contrib import messages
  5. from django.contrib.auth import login
  6. from django.db.models import get_model
  7. from django.utils.translation import ugettext as _
  8. from django.views.generic import DetailView, TemplateView, FormView, \
  9. DeleteView, UpdateView, CreateView
  10. from oscar.apps.customer.utils import normalise_email
  11. from oscar.apps.shipping.methods import NoShippingRequired
  12. from oscar.core.loading import get_class, get_classes
  13. ShippingAddressForm, GatewayForm = get_classes('checkout.forms', ['ShippingAddressForm', 'GatewayForm'])
  14. OrderTotalCalculator = get_class('checkout.calculators', 'OrderTotalCalculator')
  15. CheckoutSessionData = get_class('checkout.utils', 'CheckoutSessionData')
  16. pre_payment, post_payment = get_classes('checkout.signals', ['pre_payment', 'post_payment'])
  17. OrderNumberGenerator, OrderCreator = get_classes('order.utils', ['OrderNumberGenerator', 'OrderCreator'])
  18. UserAddressForm = get_class('address.forms', 'UserAddressForm')
  19. Repository = get_class('shipping.repository', 'Repository')
  20. AccountAuthView = get_class('customer.views', 'AccountAuthView')
  21. RedirectRequired, UnableToTakePayment, PaymentError = get_classes(
  22. 'payment.exceptions', ['RedirectRequired', 'UnableToTakePayment', 'PaymentError'])
  23. UnableToPlaceOrder = get_class('order.exceptions', 'UnableToPlaceOrder')
  24. OrderPlacementMixin = get_class('checkout.mixins', 'OrderPlacementMixin')
  25. CheckoutSessionMixin = get_class('checkout.session', 'CheckoutSessionMixin')
  26. Order = get_model('order', 'Order')
  27. ShippingAddress = get_model('order', 'ShippingAddress')
  28. CommunicationEvent = get_model('order', 'CommunicationEvent')
  29. PaymentEventType = get_model('order', 'PaymentEventType')
  30. PaymentEvent = get_model('order', 'PaymentEvent')
  31. UserAddress = get_model('address', 'UserAddress')
  32. Basket = get_model('basket', 'Basket')
  33. Email = get_model('customer', 'Email')
  34. CommunicationEventType = get_model('customer', 'CommunicationEventType')
  35. # Standard logger for checkout events
  36. logger = logging.getLogger('oscar.checkout')
  37. class IndexView(CheckoutSessionMixin, FormView):
  38. """
  39. First page of the checkout. We prompt user to either sign in, or
  40. to proceed as a guest (where we still collect their email address).
  41. """
  42. template_name = 'checkout/gateway.html'
  43. form_class = GatewayForm
  44. success_url = reverse_lazy('checkout:shipping-address')
  45. def get(self, request, *args, **kwargs):
  46. # We redirect immediately to shipping address stage if the user is
  47. # signed in
  48. if request.user.is_authenticated():
  49. return self.get_success_response()
  50. return super(IndexView, self).get(request, *args, **kwargs)
  51. def get_form_kwargs(self):
  52. kwargs = super(IndexView, self).get_form_kwargs()
  53. email = self.checkout_session.get_guest_email()
  54. if email:
  55. kwargs['initial'] = {
  56. 'username': email,
  57. }
  58. return kwargs
  59. def form_valid(self, form):
  60. if form.is_guest_checkout() or form.is_new_account_checkout():
  61. email = form.cleaned_data['username']
  62. self.checkout_session.set_guest_email(email)
  63. if form.is_new_account_checkout():
  64. messages.info(
  65. self.request,
  66. _("Create your account and then you will be redirected "
  67. "back to the checkout process"))
  68. self.success_url = "%s?next=%s&email=%s" % (
  69. reverse('customer:register'),
  70. reverse('checkout:shipping-address'),
  71. email
  72. )
  73. else:
  74. user = form.get_user()
  75. login(self.request, user)
  76. return self.get_success_response()
  77. def get_success_response(self):
  78. return HttpResponseRedirect(self.get_success_url())
  79. def get_success_url(self):
  80. return self.success_url
  81. # ================
  82. # SHIPPING ADDRESS
  83. # ================
  84. class ShippingAddressView(CheckoutSessionMixin, FormView):
  85. """
  86. Determine the shipping address for the order.
  87. The default behaviour is to display a list of addresses from the users's
  88. address book, from which the user can choose one to be their shipping address.
  89. They can add/edit/delete these USER addresses. This address will be
  90. automatically converted into a SHIPPING address when the user checks out.
  91. Alternatively, the user can enter a SHIPPING address directly which will be
  92. saved in the session and later saved as ShippingAddress model when the order
  93. is sucessfully submitted.
  94. """
  95. template_name = 'checkout/shipping_address.html'
  96. form_class = ShippingAddressForm
  97. def get(self, request, *args, **kwargs):
  98. # Check that the user's basket is not empty
  99. if request.basket.is_empty:
  100. messages.error(request, _("You need to add some items to your basket to checkout"))
  101. return HttpResponseRedirect(reverse('basket:summary'))
  102. # Check that guests have entered an email address
  103. if not request.user.is_authenticated() and not self.checkout_session.get_guest_email():
  104. messages.error(request, _("Please either sign in or enter your email address"))
  105. return HttpResponseRedirect(reverse('checkout:index'))
  106. # Check to see that a shipping address is actually required. It may not be if
  107. # the basket is purely downloads
  108. if not request.basket.is_shipping_required():
  109. messages.info(request, _("Your basket does not require a shipping address to be submitted"))
  110. return HttpResponseRedirect(self.get_success_url())
  111. return super(ShippingAddressView, self).get(request, *args, **kwargs)
  112. def get_initial(self):
  113. return self.checkout_session.new_shipping_address_fields()
  114. def get_context_data(self, **kwargs):
  115. kwargs = super(ShippingAddressView, self).get_context_data(**kwargs)
  116. if self.request.user.is_authenticated():
  117. # Look up address book data
  118. kwargs['addresses'] = self.get_available_addresses()
  119. return kwargs
  120. def get_available_addresses(self):
  121. return UserAddress._default_manager.filter(user=self.request.user).order_by('-is_default_for_shipping')
  122. def post(self, request, *args, **kwargs):
  123. # Check if a shipping address was selected directly (eg no form was filled in)
  124. if self.request.user.is_authenticated() and 'address_id' in self.request.POST:
  125. address = UserAddress._default_manager.get(pk=self.request.POST['address_id'],
  126. user=self.request.user)
  127. action = self.request.POST.get('action', None)
  128. if action == 'ship_to':
  129. # User has selected a previous address to ship to
  130. self.checkout_session.ship_to_user_address(address)
  131. return HttpResponseRedirect(self.get_success_url())
  132. elif action == 'delete':
  133. # Delete the selected address
  134. address.delete()
  135. messages.info(self.request, _("Address deleted from your address book"))
  136. return HttpResponseRedirect(reverse('checkout:shipping-method'))
  137. else:
  138. return HttpResponseBadRequest()
  139. else:
  140. return super(ShippingAddressView, self).post(request, *args, **kwargs)
  141. def form_valid(self, form):
  142. # Store the address details in the session and redirect to next step
  143. self.checkout_session.ship_to_new_address(form.clean())
  144. return super(ShippingAddressView, self).form_valid(form)
  145. def get_success_url(self):
  146. return reverse('checkout:shipping-method')
  147. class UserAddressCreateView(CheckoutSessionMixin, CreateView):
  148. """
  149. Add a USER address to the user's addressbook.
  150. This is not the same as creating a SHIPPING Address, although if used for
  151. the order, it will be converted into a shipping address at submission-time.
  152. """
  153. template_name = 'checkout/user_address_form.html'
  154. form_class = UserAddressForm
  155. def get_form_kwargs(self):
  156. kwargs = super(UserAddressCreateView, self).get_form_kwargs()
  157. kwargs['user'] = self.request.user
  158. return kwargs
  159. def get_success_response(self):
  160. messages.info(self.request, _("Address saved"))
  161. # We redirect back to the shipping address page
  162. return HttpResponseRedirect(reverse('checkout:shipping-address'))
  163. class UserAddressUpdateView(CheckoutSessionMixin, UpdateView):
  164. """
  165. Update a user address
  166. """
  167. template_name = 'checkout/user_address_form.html'
  168. form_class = UserAddressForm
  169. def get_queryset(self):
  170. return self.request.user.addresses.all()
  171. def get_form_kwargs(self):
  172. kwargs = super(UserAddressUpdateView, self).get_form_kwargs()
  173. kwargs['user'] = self.request.user
  174. return kwargs
  175. def get_success_url(self):
  176. messages.info(self.request, _("Address saved"))
  177. return reverse('checkout:shipping-address')
  178. class UserAddressDeleteView(CheckoutSessionMixin, DeleteView):
  179. """
  180. Delete an address from a user's addressbook.
  181. """
  182. template_name = 'checkout/user_address_delete.html'
  183. def get_queryset(self):
  184. return UserAddress._default_manager.filter(user=self.request.user)
  185. def get_success_url(self):
  186. messages.info(self.request, _("Address deleted"))
  187. return reverse('checkout:shipping-address')
  188. # ===============
  189. # Shipping method
  190. # ===============
  191. class ShippingMethodView(CheckoutSessionMixin, TemplateView):
  192. """
  193. View for allowing a user to choose a shipping method.
  194. Shipping methods are largely domain-specific and so this view
  195. will commonly need to be subclassed and customised.
  196. The default behaviour is to load all the available shipping methods
  197. using the shipping Repository. If there is only 1, then it is
  198. automatically selected. Otherwise, a page is rendered where
  199. the user can choose the appropriate one.
  200. """
  201. template_name = 'checkout/shipping_methods.html'
  202. def get(self, request, *args, **kwargs):
  203. # Check that the user's basket is not empty
  204. if request.basket.is_empty:
  205. messages.error(request, _("You need to add some items to your basket to checkout"))
  206. return HttpResponseRedirect(reverse('basket:summary'))
  207. # Check that shipping is required at all
  208. if not request.basket.is_shipping_required():
  209. self.checkout_session.use_shipping_method(NoShippingRequired().code)
  210. return self.get_success_response()
  211. # Check that shipping address has been completed
  212. if not self.checkout_session.is_shipping_address_set():
  213. messages.error(request, _("Please choose a shipping address"))
  214. return HttpResponseRedirect(reverse('checkout:shipping-address'))
  215. # Save shipping methods as instance var as we need them both here
  216. # and when setting the context vars.
  217. self._methods = self.get_available_shipping_methods()
  218. if len(self._methods) == 0:
  219. # No shipping methods available for given address
  220. messages.warning(request, _("Shipping is not available for your chosen address - please choose another"))
  221. return HttpResponseRedirect(reverse('checkout:shipping-address'))
  222. elif len(self._methods) == 1:
  223. # Only one shipping method - set this and redirect onto the next step
  224. self.checkout_session.use_shipping_method(self._methods[0].code)
  225. return self.get_success_response()
  226. # Must be more than one available shipping method, we present them to
  227. # the user to make a choice.
  228. return super(ShippingMethodView, self).get(request, *args, **kwargs)
  229. def get_context_data(self, **kwargs):
  230. kwargs = super(ShippingMethodView, self).get_context_data(**kwargs)
  231. kwargs['methods'] = self._methods
  232. return kwargs
  233. def get_available_shipping_methods(self):
  234. """
  235. Returns all applicable shipping method objects
  236. for a given basket.
  237. """
  238. # Shipping methods can depend on the user, the contents of the basket
  239. # and the shipping address. I haven't come across a scenario that doesn't
  240. # fit this system.
  241. return Repository().get_shipping_methods(self.request.user, self.request.basket,
  242. self.get_shipping_address())
  243. def post(self, request, *args, **kwargs):
  244. # Need to check that this code is valid for this user
  245. method_code = request.POST.get('method_code', None)
  246. is_valid = False
  247. for method in self.get_available_shipping_methods():
  248. if method.code == method_code:
  249. is_valid = True
  250. if not is_valid:
  251. messages.error(request, _("Your submitted shipping method is not permitted"))
  252. return HttpResponseRedirect(reverse('checkout:shipping-method'))
  253. # Save the code for the chosen shipping method in the session
  254. # and continue to the next step.
  255. self.checkout_session.use_shipping_method(method_code)
  256. return self.get_success_response()
  257. def get_success_response(self):
  258. return HttpResponseRedirect(reverse('checkout:payment-method'))
  259. # ==============
  260. # Payment method
  261. # ==============
  262. class PaymentMethodView(CheckoutSessionMixin, TemplateView):
  263. """
  264. View for a user to choose which payment method(s) they want to use.
  265. This would include setting allocations if payment is to be split
  266. between multiple sources.
  267. """
  268. def get(self, request, *args, **kwargs):
  269. # Check that the user's basket is not empty
  270. if request.basket.is_empty:
  271. messages.error(request, _("You need to add some items to your basket to checkout"))
  272. return HttpResponseRedirect(reverse('basket:summary'))
  273. shipping_required = request.basket.is_shipping_required()
  274. # Check that shipping address has been completed
  275. if shipping_required and not self.checkout_session.is_shipping_address_set():
  276. messages.error(request, _("Please choose a shipping address"))
  277. return HttpResponseRedirect(reverse('checkout:shipping-address'))
  278. # Check that shipping method has been set
  279. if shipping_required and not self.checkout_session.is_shipping_method_set():
  280. messages.error(request, _("Please choose a shipping method"))
  281. return HttpResponseRedirect(reverse('checkout:shipping-method'))
  282. return self.get_success_response()
  283. def get_success_response(self):
  284. return HttpResponseRedirect(reverse('checkout:payment-details'))
  285. # ================
  286. # Order submission
  287. # ================
  288. class PaymentDetailsView(OrderPlacementMixin, TemplateView):
  289. """
  290. For taking the details of payment and creating the order
  291. The class is deliberately split into fine-grained methods, responsible for only one
  292. thing. This is to make it easier to subclass and override just one component of
  293. functionality.
  294. All projects will need to subclass and customise this class.
  295. """
  296. template_name = 'checkout/payment_details.html'
  297. template_name_preview = 'checkout/preview.html'
  298. preview = False
  299. def get_template_names(self):
  300. return [self.template_name_preview] if self.preview else [self.template_name]
  301. def get_error_response(self):
  302. # Check that the user's basket is not empty
  303. if self.request.basket.is_empty:
  304. messages.error(self.request, _("You need to add some items to your basket to checkout"))
  305. return HttpResponseRedirect(reverse('basket:summary'))
  306. shipping_required = self.request.basket.is_shipping_required()
  307. # Check that shipping address has been completed
  308. if shipping_required and not self.checkout_session.is_shipping_address_set():
  309. messages.error(self.request, _("Please choose a shipping address"))
  310. return HttpResponseRedirect(reverse('checkout:shipping-address'))
  311. # Check that shipping method has been set
  312. if shipping_required and not self.checkout_session.is_shipping_method_set():
  313. messages.error(self.request, _("Please choose a shipping method"))
  314. return HttpResponseRedirect(reverse('checkout:shipping-method'))
  315. def get(self, request, *args, **kwargs):
  316. error_response = self.get_error_response()
  317. if error_response:
  318. return error_response
  319. return super(PaymentDetailsView, self).get(request, *args, **kwargs)
  320. def post(self, request, *args, **kwargs):
  321. """
  322. This method is designed to be overridden by subclasses which will
  323. validate the forms from the payment details page. If the forms are valid
  324. then the method can call submit()
  325. """
  326. error_response = self.get_error_response()
  327. if error_response:
  328. return error_response
  329. if self.preview:
  330. # We use a custom parameter to indicate if this is an attempt to place an order.
  331. # Without this, we assume a payment form is being submitted from the
  332. # payment-details page
  333. if request.POST.get('action', '') == 'place_order':
  334. return self.submit(request.basket)
  335. return self.render_preview(request)
  336. # Posting to payment-details isn't the right thing to do
  337. return self.get(request, *args, **kwargs)
  338. def render_preview(self, request, **kwargs):
  339. """
  340. Show a preview of the order.
  341. If sensitive data was submitted on the payment details page, you will
  342. need to pass it back to the view here so it can be stored in hidden form
  343. inputs. This avoids ever writing the sensitive data to disk.
  344. """
  345. ctx = self.get_context_data()
  346. ctx.update(kwargs)
  347. return self.render_to_response(ctx)
  348. def can_basket_be_submitted(self, basket):
  349. for line in basket.lines.all():
  350. is_permitted, reason = line.product.is_purchase_permitted(self.request.user, line.quantity)
  351. if not is_permitted:
  352. return False, reason, reverse('basket:summary')
  353. return True, None, None
  354. def get_default_billing_address(self):
  355. """
  356. Return default billing address for user
  357. This is useful when the payment details view includes a billing address
  358. form - you can use this helper method to prepopulate the form.
  359. Note, this isn't used in core oscar as there is no billing address form
  360. by default.
  361. """
  362. if not self.request.user.is_authenticated():
  363. return None
  364. try:
  365. return self.request.user.addresses.get(is_default_for_billing=True)
  366. except UserAddress.DoesNotExist:
  367. return None
  368. def submit(self, basket, payment_kwargs=None, order_kwargs=None):
  369. """
  370. Submit a basket for order placement.
  371. The process runs as follows:
  372. * Generate an order number
  373. * Freeze the basket so it cannot be modified any more (important when
  374. redirecting the user to another site for payment as it prevents the
  375. basket being manipulated during the payment process).
  376. * Attempt to take payment for the order
  377. - If payment is successful, place the order
  378. - If a redirect is required (eg PayPal, 3DSecure), redirect
  379. - If payment is unsuccessful, show an appropriate error message
  380. :basket: The basket to submit.
  381. :payment_kwargs: Additional kwargs to pass to the handle_payment method.
  382. :order_kwargs: Additional kwargs to pass to the place_order method.
  383. """
  384. if payment_kwargs is None:
  385. payment_kwargs = {}
  386. if order_kwargs is None:
  387. order_kwargs = {}
  388. # Next, check that basket isn't empty
  389. if basket.is_empty:
  390. messages.error(self.request, _("This order cannot be submitted as the basket is empty"))
  391. url = self.request.META.get('HTTP_REFERER', reverse('basket:summary'))
  392. return HttpResponseRedirect(url)
  393. # Domain-specific checks on the basket
  394. is_valid, reason, url = self.can_basket_be_submitted(basket)
  395. if not is_valid:
  396. messages.error(self.request, reason)
  397. return HttpResponseRedirect(url)
  398. # We generate the order number first as this will be used
  399. # in payment requests (ie before the order model has been
  400. # created). We also save it in the session for multi-stage
  401. # checkouts (eg where we redirect to a 3rd party site and place
  402. # the order on a different request).
  403. order_number = self.generate_order_number(basket)
  404. logger.info("Order #%s: beginning submission process for basket #%d", order_number, basket.id)
  405. # Freeze the basket so it cannot be manipulated while the customer is
  406. # completing payment on a 3rd party site. Also, store a reference to
  407. # the basket in the session so that we know which basket to thaw if we
  408. # get an unsuccessful payment response when redirecting to a 3rd party
  409. # site.
  410. self.freeze_basket(basket)
  411. self.checkout_session.set_submitted_basket(basket)
  412. # Handle payment. Any payment problems should be handled by the
  413. # handle_payment method raise an exception, which should be caught
  414. # within handle_POST and the appropriate forms redisplayed.
  415. error_msg = _("A problem occurred while processing payment for this "
  416. "order - no payment has been taken. Please "
  417. "contact customer services if this problem persists")
  418. pre_payment.send_robust(sender=self, view=self)
  419. total_incl_tax, total_excl_tax = self.get_order_totals(basket)
  420. try:
  421. self.handle_payment(order_number, total_incl_tax, **payment_kwargs)
  422. except RedirectRequired, e:
  423. # Redirect required (eg PayPal, 3DS)
  424. logger.info("Order #%s: redirecting to %s", order_number, e.url)
  425. return HttpResponseRedirect(e.url)
  426. except UnableToTakePayment, e:
  427. # Something went wrong with payment but in an anticipated way. Eg
  428. # their bankcard has expired, wrong card number - that kind of
  429. # thing. This type of exception is supposed to set a friendly error
  430. # message that makes sense to the customer.
  431. msg = unicode(e)
  432. logger.warning("Order #%s: unable to take payment (%s) - restoring basket", order_number, msg)
  433. self.restore_frozen_basket()
  434. # We re-render the payment details view
  435. self.preview = False
  436. return self.render_to_response(self.get_context_data(error=msg))
  437. except PaymentError, e:
  438. # A general payment error - Something went wrong which wasn't
  439. # anticipated. Eg, the payment gateway is down (it happens), your
  440. # credentials are wrong - that king of thing.
  441. # It makes sense to configure the checkout logger to
  442. # mail admins on an error as this issue warrants some further
  443. # investigation.
  444. msg = unicode(e)
  445. logger.error("Order #%s: payment error (%s)", order_number, msg)
  446. self.restore_frozen_basket()
  447. self.preview = False
  448. return self.render_to_response(self.get_context_data(error=error_msg))
  449. except Exception, e:
  450. # Unhandled exception - hopefully, you will only ever see this in
  451. # development.
  452. logger.error("Order #%s: unhandled exception while taking payment (%s)", order_number, e)
  453. logger.exception(e)
  454. self.restore_frozen_basket()
  455. self.preview = False
  456. return self.render_to_response(self.get_context_data(error=error_msg))
  457. post_payment.send_robust(sender=self, view=self)
  458. # If all is ok with payment, try and place order
  459. logger.info("Order #%s: payment successful, placing order", order_number)
  460. try:
  461. return self.handle_order_placement(
  462. order_number, basket, total_incl_tax, total_excl_tax,
  463. **order_kwargs)
  464. except UnableToPlaceOrder, e:
  465. # It's possible that something will go wrong while trying to
  466. # actually place an order. Not a good situation to be in as a
  467. # payment transaction may already have taken place, but needs
  468. # to be handled gracefully.
  469. logger.error("Order #%s: unable to place order - %s",
  470. order_number, e)
  471. logger.exception(e)
  472. msg = unicode(e)
  473. self.restore_frozen_basket()
  474. return self.render_to_response(self.get_context_data(error=msg))
  475. def generate_order_number(self, basket):
  476. generator = OrderNumberGenerator()
  477. order_number = generator.order_number(basket)
  478. self.checkout_session.set_order_number(order_number)
  479. return order_number
  480. def freeze_basket(self, basket):
  481. # We freeze the basket to prevent it being modified once the payment
  482. # process has started. If your payment fails, then the basket will
  483. # need to be "unfrozen". We also store the basket ID in the session
  484. # so the it can be retrieved by multistage checkout processes.
  485. basket.freeze()
  486. def handle_payment(self, order_number, total, **kwargs):
  487. """
  488. Handle any payment processing and record payment sources and events.
  489. This method is designed to be overridden within your project. The
  490. default is to do nothing as payment is domain-specific.
  491. This method is responsible for handling payment and recording the
  492. payment sources (using the add_payment_source method) and payment
  493. events (using add_payment_event) so they can be
  494. linked to the order when it is saved later on.
  495. """
  496. pass
  497. def get_context_data(self, **kwargs):
  498. # Return kwargs directly instead of using 'params' as in django's TemplateView
  499. ctx = super(PaymentDetailsView, self).get_context_data(**kwargs)
  500. ctx.update(kwargs)
  501. return ctx
  502. # =========
  503. # Thank you
  504. # =========
  505. class ThankYouView(DetailView):
  506. """
  507. Displays the 'thank you' page which summarises the order just submitted.
  508. """
  509. template_name = 'checkout/thank_you.html'
  510. context_object_name = 'order'
  511. def get_object(self):
  512. # We allow superusers to force an order thankyou page for testing
  513. order = None
  514. if self.request.user.is_superuser:
  515. if 'order_number' in self.request.GET:
  516. order = Order._default_manager.get(number=self.request.GET['order_number'])
  517. elif 'order_id' in self.request.GET:
  518. order = Order._default_manager.get(id=self.request.GET['orderid'])
  519. if not order:
  520. if 'checkout_order_id' in self.request.session:
  521. order = Order._default_manager.get(pk=self.request.session['checkout_order_id'])
  522. else:
  523. raise Http404(_("No order found"))
  524. return order