-class EntityAttributeMapper(object, DictMixin):
- def __init__(self, entity):
- self.entity = entity
-
- def get_attributes(self):
- return self.entity.attribute_set.all()
-
- def make_cache(self):
- attributes = self.get_attributes()
- value_lookups = {}
-
- for a in attributes:
- value_lookups.setdefault(a.value_content_type, []).append(a.value_object_id)
-
- values_bulk = {}
-
- for ct, pks in value_lookups.items():
- values_bulk[ct] = ct.model_class().objects.in_bulk(pks)
-
- self._cache = dict([(a.key, getattr(values_bulk[a.value_content_type].get(a.value_object_id), 'value', None)) for a in attributes])
-
- def __getitem__(self, key):
- if not hasattr(self, '_cache'):
- self.make_cache()
- return self._cache[key]
-
- def keys(self):
- if not hasattr(self, '_cache'):
- self.make_cache()
- return self._cache.keys()
-
- def items(self):
- if not hasattr(self, '_cache'):
- self.make_cache()
- return self._cache.items()
-
- def values(self):
- if not hasattr(self, '_cache'):
- self.make_cache()
- return self._cache.values()
-
-