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.

utils.py 4.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. from django.contrib.sites.models import Site
  2. from oscar.services import import_module
  3. order_models = import_module('order.models', ['ShippingAddress', 'Order', 'Batch', 'BatchLine',
  4. 'BatchLinePrice', 'BatchLineAttribute'])
  5. class OrderCreator(object):
  6. def __init__(self, order_total_calculator):
  7. self.order_total_calculator = order_total_calculator
  8. def place_order(self, user, basket, shipping_address, shipping_method):
  9. u"""
  10. Placing an order involves creating all the relevant models based on the
  11. basket and session data.
  12. """
  13. order = self._create_order_model(user, basket, shipping_address, shipping_method)
  14. for line in basket.lines.all():
  15. batch = self._get_or_create_batch_for_line(order, line)
  16. self._create_line_models(order, batch, line)
  17. basket.set_as_submitted()
  18. return order
  19. def _create_order_model(self, user, basket, shipping_address, shipping_method):
  20. u"""Creates an order model."""
  21. calc = self.order_total_calculator
  22. order_data = {'basket': basket,
  23. 'site': Site.objects.get_current(),
  24. 'total_incl_tax': calc.order_total_incl_tax(basket, shipping_method),
  25. 'total_excl_tax': calc.order_total_excl_tax(basket, shipping_method),
  26. 'shipping_address': shipping_address,
  27. 'shipping_incl_tax': shipping_method.basket_charge_incl_tax(),
  28. 'shipping_excl_tax': shipping_method.basket_charge_excl_tax(),
  29. 'shipping_method': shipping_method.name}
  30. if user.is_authenticated():
  31. order_data['user_id'] = user.id
  32. order = order_models.Order(**order_data)
  33. order.save()
  34. return order
  35. def _get_or_create_batch_for_line(self, order, line):
  36. u"""Returns the batch for a given line, creating it if appropriate."""
  37. partner = self._get_partner_for_product(line.product)
  38. batch,_ = order_models.Batch.objects.get_or_create(order=order, partner=partner)
  39. return batch
  40. def _get_partner_for_product(self, product):
  41. u"""Returns the partner for a product"""
  42. if product.has_stockrecord:
  43. return product.stockrecord.partner
  44. raise AttributeError("No partner found for product '%s'" % product)
  45. def _create_line_models(self, order, batch, basket_line):
  46. u"""Creates the batch line model."""
  47. batch_line = order_models.BatchLine.objects.create(order=order,
  48. batch=batch,
  49. product=basket_line.product,
  50. quantity=basket_line.quantity,
  51. line_price_excl_tax=basket_line.line_price_excl_tax,
  52. line_price_incl_tax=basket_line.line_price_incl_tax)
  53. self._create_line_price_models(order, batch_line, basket_line)
  54. self._create_line_attributes(order, batch_line, basket_line)
  55. def _create_line_price_models(self, order, batch_line, basket_line):
  56. u"""Creates the batch line price models"""
  57. order_models.BatchLinePrice.objects.create(order=order,
  58. line=batch_line,
  59. quantity=batch_line.quantity,
  60. price_incl_tax=basket_line.unit_price_incl_tax,
  61. price_excl_tax=basket_line.unit_price_excl_tax)
  62. def _create_line_attributes(self, order, batch_line, basket_line):
  63. u"""Creates the batch line attributes."""
  64. for attr in basket_line.attributes.all():
  65. order_models.BatchLineAttribute.objects.create(line=batch_line, type=attr.option.code,
  66. value=attr.value)