git.ithinksw.org
/
philo.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Tweaked AttributeMapper._fill_cache to also store values on the Attribute instance...
[philo.git]
/
philo
/
utils
/
entities.py
diff --git
a/philo/utils/entities.py
b/philo/utils/entities.py
index
1ddff05
..
830276e
100644
(file)
--- a/
philo/utils/entities.py
+++ b/
philo/utils/entities.py
@@
-1,8
+1,11
@@
+from functools import partial
from UserDict import DictMixin
from django.db import models
from django.contrib.contenttypes.models import ContentType
from UserDict import DictMixin
from django.db import models
from django.contrib.contenttypes.models import ContentType
+from philo.utils.lazycompat import SimpleLazyObject
+
### AttributeMappers
### AttributeMappers
@@
-83,17
+86,24
@@
class AttributeMapper(object, DictMixin):
value_lookups = {}
for a in attributes:
value_lookups = {}
for a in attributes:
- value_lookups.setdefault(a.value_content_type, []).append(a.value_object_id)
+ value_lookups.setdefault(a.value_content_type
_id
, []).append(a.value_object_id)
self._attributes_cache[a.key] = a
self._attributes_cache[a.key] = a
- values_bulk = {}
+ values_bulk = dict(((ct_pk, SimpleLazyObject(partial(ContentType.objects.get_for_id(ct_pk).model_class().objects.in_bulk, pks))) for ct_pk, pks in value_lookups.items()))
+
+ cache = {}
- for ct, pks in value_lookups.items():
- values_bulk[ct] = ct.model_class().objects.in_bulk(pks)
+ for a in attributes:
+ cache[a.key] = SimpleLazyObject(partial(self._lazy_value_from_bulk, values_bulk, a))
+ a._value_cache = cache[a.key]
- self._cache.update(
dict([(a.key, getattr(values_bulk[a.value_content_type].get(a.value_object_id), 'value', None)) for a in attributes])
)
+ self._cache.update(
cache
)
self._cache_filled = True
self._cache_filled = True
+ def _lazy_value_from_bulk(self, bulk, attribute):
+ v = bulk[attribute.value_content_type_id].get(attribute.value_object_id)
+ return getattr(v, 'value', None)
+
def clear_cache(self):
"""Clears the cache."""
self._cache = {}
def clear_cache(self):
"""Clears the cache."""
self._cache = {}