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

abstract_models.py 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from decimal import Decimal
  2. from django.db import models
  3. from django.utils.translation import ugettext_lazy as _
  4. from django.template.defaultfilters import slugify
  5. from django.conf import settings
  6. from oscar.apps.shipping.methods import ShippingMethod
  7. class AbstractOrderAndItemLevelChargeMethod(models.Model, ShippingMethod):
  8. u"""
  9. Standard shipping method
  10. This method has two components:
  11. * a charge per order
  12. * a charge per item
  13. Many sites use shipping logic which fits into this system. However, for more
  14. complex shipping logic, a custom shipping method object will need to be provided
  15. that subclasses ShippingMethod.
  16. """
  17. code = models.CharField(max_length=128, unique=True)
  18. name = models.CharField(_("Name"), max_length=128)
  19. description = models.TextField(_("Description"), blank=True)
  20. price_currency = models.CharField(max_length=12, default=settings.OSCAR_DEFAULT_CURRENCY)
  21. price_per_order = models.DecimalField(decimal_places=2, max_digits=12, default=Decimal('0.00'))
  22. price_per_item = models.DecimalField(decimal_places=2, max_digits=12, default=Decimal('0.00'))
  23. # If basket value is above this threshold, then shipping is free
  24. free_shipping_threshold = models.DecimalField(decimal_places=2, max_digits=12, null=True)
  25. _basket = None
  26. def save(self, *args, **kwargs):
  27. if not self.code:
  28. self.code = slugify(self.name)
  29. super(AbstractOrderAndItemLevelChargeMethod, self).save(*args, **kwargs)
  30. class Meta:
  31. abstract = True
  32. def __unicode__(self):
  33. return self.name
  34. def set_basket(self, basket):
  35. self._basket = basket
  36. def basket_charge_incl_tax(self):
  37. u"""
  38. Return basket total including tax
  39. """
  40. if self.free_shipping_threshold != None and self._basket.total_incl_tax >= self.free_shipping_threshold:
  41. return Decimal('0.00')
  42. charge = self.price_per_order
  43. for line in self._basket.lines.all():
  44. charge += line.quantity * self.price_per_item
  45. return charge
  46. def basket_charge_excl_tax(self):
  47. u"""
  48. Return basket total excluding tax.
  49. Default implementation assumes shipping is tax free.
  50. """
  51. return self.basket_charge_incl_tax()