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