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.

views.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from django.shortcuts import render
  2. from oscar.checkout.views import (ShippingMethodView as CoreShippingMethodView,
  3. PaymentMethodView as CorePaymentMethodView,
  4. PaymentDetailsView as CorePaymentDetailsView,
  5. OrderPreviewView as CoreOrderPreviewView,
  6. prev_steps_must_be_complete)
  7. from oscar.payment.forms import BankcardForm, BillingAddressForm
  8. from oscar.services import import_module
  9. shipping_models = import_module('shipping.models', ['Method'])
  10. payment_models = import_module('payment.models', ['Source', 'SourceType'])
  11. class ShippingMethodView(CoreShippingMethodView):
  12. def get_available_shipping_methods(self):
  13. codes = ['royal-mail-first-class']
  14. for line in self.basket.lines.all():
  15. if line.product.stockrecord.partner_id == 2:
  16. codes.append('parcel-force')
  17. return shipping_models.Method.objects.filter(code__in=codes)
  18. class PaymentMethodView(CorePaymentMethodView):
  19. template_file = 'checkout/payment_method.html'
  20. def handle_GET(self):
  21. return render(self.request, self.template_file, self.context)
  22. def handle_POST(self):
  23. method = self.request.POST['method_code']
  24. self.co_data.pay_by(method)
  25. return self.get_success_response()
  26. class OrderPreviewView(CoreOrderPreviewView):
  27. u"""View a preview of the order before submitting."""
  28. def handle_GET(self):
  29. # Forward straight onto the payment details
  30. return self.get_success_response()
  31. class PaymentDetailsView(CorePaymentDetailsView):
  32. template_file = 'checkout/payment_details.html'
  33. def handle_GET(self):
  34. if self._is_cheque_payment():
  35. self.template_file = 'checkout/payment_details_cheque.html'
  36. else:
  37. self.context['bankcard_form'] = BankcardForm()
  38. self.context['billing_address_form'] = BillingAddressForm()
  39. return render(self.request, self.template_file, self.context)
  40. def _is_cheque_payment(self):
  41. payment_method = self.co_data.payment_method()
  42. return payment_method == 'cheque'
  43. def handle_POST(self):
  44. if self._is_cheque_payment():
  45. return self._submit()
  46. self.bankcard_form = BankcardForm(self.request.POST)
  47. self.billing_addr_form = BillingAddressForm(self.request.POST)
  48. if self.bankcard_form.is_valid() and self.billing_addr_form.is_valid():
  49. return self._submit()
  50. self.context['bankcard_form'] = bankcard_form
  51. self.context['billing_address_form'] = billing_addr_form
  52. return render(self.request, self.template_file, self.context)
  53. def _handle_payment(self, basket, order_number):
  54. if self._is_cheque_payment():
  55. type = payment_models.SourceType.objects.get(code='cheque')
  56. allocation = self._get_chargable_total(basket)
  57. source = payment_models.Source(type=type, allocation=allocation)
  58. self.payment_sources.append(source)
  59. return
  60. self._handle_bankcard_payment()
  61. def _handle_bankcard_payment(self):
  62. # Handle payment problems with an exception
  63. pass
  64. def _place_order(self, basket, order_number):
  65. order = super(PaymentDetailsView, self)._place_order(basket, order_number)
  66. if self._is_cheque_payment():
  67. # Set order status as on hold
  68. pass
  69. return order