Merge branch 'optimization' into develop
authorStephen Burrows <stephen.r.burrows@gmail.com>
Fri, 26 Aug 2011 01:10:43 +0000 (18:10 -0700)
committerStephen Burrows <stephen.r.burrows@gmail.com>
Fri, 26 Aug 2011 01:10:43 +0000 (18:10 -0700)
philo/contrib/winer/models.py
philo/models/nodes.py
philo/utils/entities.py

index 09014eb..4acf5d1 100644 (file)
@@ -135,7 +135,6 @@ class FeedView(MultiView):
                
                """
                get_items = get_items_attr if callable(get_items_attr) else getattr(self, get_items_attr)
-               page = page_attr if isinstance(page_attr, Page) else getattr(self, page_attr)
                
                def inner(request, extra_context=None, *args, **kwargs):
                        obj = self.get_object(request, *args, **kwargs)
@@ -146,6 +145,7 @@ class FeedView(MultiView):
                        context.update(extra_context or {})
                        context.update(item_context or {})
                        
+                       page = page_attr if isinstance(page_attr, Page) else getattr(self, page_attr)
                        return page.render_to_response(request, extra_context=context)
                return inner
        
index 58d1b96..647ba81 100644 (file)
@@ -55,7 +55,7 @@ class Node(SlugTreeEntity):
                """This is a shortcut method for :meth:`View.render_to_response`"""
                if self.view_object_id and self.view_content_type_id:
                        view_model = ContentType.objects.get_for_id(self.view_content_type_id).model_class()
-                       self.view = view_model._default_manager.select_related(depth=1).get(pk=self.view_object_id)
+                       self.view = view_model._default_manager.get(pk=self.view_object_id)
                        return self.view.render_to_response(request, extra_context)
                raise Http404
        
index 754a5dc..830276e 100644 (file)
@@ -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 philo.utils.lazycompat import SimpleLazyObject
+
 
 ### AttributeMappers
 
@@ -86,14 +89,21 @@ class AttributeMapper(object, DictMixin):
                        value_lookups.setdefault(a.value_content_type_id, []).append(a.value_object_id)
                        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_pk, pks in value_lookups.items():
-                       values_bulk[ct_pk] = ContentType.objects.get_for_id(ct_pk).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_id].get(a.value_object_id), 'value', None)) for a in attributes]))
+               self._cache.update(cache)
                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 = {}