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.

__init__.py 962B

12345678910111213141516171819202122232425262728
  1. from django.core.exceptions import ObjectDoesNotExist
  2. class Scales(object):
  3. """
  4. For calculating the weight of a product or basket
  5. """
  6. def __init__(self, attribute_code='weight', default_weight=None):
  7. self.attribute = attribute_code
  8. self.default_weight = default_weight
  9. def weigh_product(self, product):
  10. try:
  11. attr_val = product.attribute_values.get(attribute__code=self.attribute)
  12. except ObjectDoesNotExist:
  13. if self.default_weight is None:
  14. raise ValueError("No attribute %s found for product %s" % (self.attribute, product))
  15. weight = self.default_weight
  16. else:
  17. weight = attr_val.value
  18. return float(weight) if weight is not None else 0.0
  19. def weigh_basket(self, basket):
  20. weight = 0.0
  21. for line in basket.lines.all():
  22. weight += self.weigh_product(line.product) * line.quantity
  23. return weight