Skip to content

Commit

Permalink
ProductAttributesContainer: Prefix internal attributes with _
Browse files Browse the repository at this point in the history
  • Loading branch information
mvantellingen committed Aug 19, 2015
1 parent 87e5712 commit 8f50afc
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/oscar/apps/catalogue/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@ class ProductAttributesContainer(object):

def __setstate__(self, state):
self.__dict__ = state
self.initialised = False
self._initialised = False

def __init__(self, product):
self.product = product
self.initialised = False
self._product = product
self._product_pk = product.pk
self._initialised = False

def __getattr__(self, name):
if not name.startswith('_') and not self.initialised:
self._load_values()
return getattr(self, name)
raise AttributeError(
_("%(obj)s has no attribute named '%(attr)s'") % {
'obj': self.product.get_product_class(), 'attr': name})
'obj': self._product.get_product_class(), 'attr': name})

def validate_attributes(self):
for attribute in self.get_all_attributes():
Expand All @@ -44,13 +45,13 @@ def validate_attributes(self):
{'attr': attribute.code, 'err': e})

def get_values(self):
return self.product.attribute_values.all()
return self._product.attribute_values.all()

def get_value_by_attribute(self, attribute):
return self.get_values().get(attribute=attribute)

def get_all_attributes(self):
return self.product.get_product_class().attributes.all()
return self._product.get_product_class().attributes.all()

def get_attribute_by_code(self, code):
return self.get_all_attributes().get(code=code)
Expand Down Expand Up @@ -83,11 +84,11 @@ def save(self):
# Django fetch it again.
if value_obj:
value_obj.attribute = attribute
attribute.save_value(self.product, new_value, value_obj)
attribute.save_value(self._product, new_value, value_obj)

def _load_values(self):
values = {v.attribute_id: v for v in self.get_values()}
attrs = self.product.get_product_class().attributes.all()
attrs = self._product.get_product_class().attributes.all()

self._initial_state = {}
for attr in attrs:
Expand All @@ -96,4 +97,4 @@ def _load_values(self):
setattr(self, attr.code, value)
self._initial_state[attr.code] = value

self.initialised = True
self._initialised = True

0 comments on commit 8f50afc

Please sign in to comment.