Permalink
Browse files

Caches the list of footnote plugins.

  • Loading branch information...
1 parent e21a833 commit 5ee6190650ea76e69d6aac1dc5ad281d9e19e251 @BertrandBordage committed Aug 29, 2012
View
7 cmsplugin_footnote/cms_plugins.py
@@ -2,6 +2,7 @@
from .models import Footnote
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
+from .utils import get_footnote_plugins
from cms.plugin_pool import plugin_pool
@@ -25,11 +26,7 @@ def render(self, context, instance, placeholder_name):
context = super(FootnotePlugin, self).render(context, instance,
placeholder_name)
page = context['request'].current_page
- placeholder = page.placeholders.get(slot=placeholder_name)
- plugins = placeholder.cmsplugin_set \
- .filter(plugin_type='FootnotePlugin') \
- .order_by('tree_id', 'rght')
- footnote_plugins = [p.get_plugin_instance()[0] for p in plugins]
+ footnote_plugins = get_footnote_plugins(page, placeholder_name)
context['counter'] = footnote_plugins.index(instance) + 1
context['placeholder_name'] = placeholder_name
return context
View
11 cmsplugin_footnote/models.py
@@ -3,6 +3,7 @@
from django.utils.translation import ugettext_lazy as _
from django.template.defaultfilters import truncatewords_html
from django.utils.html import strip_tags
+from django.core.cache import cache
class Footnote(AbstractText):
@@ -15,3 +16,13 @@ class Meta:
def __unicode__(self):
return strip_tags(truncatewords_html(self.body, 5))
+
+ def save(self, *args, **kwargs):
+ super(Footnote, self).save(*args, **kwargs)
+ placeholder, page = self.placeholder, self.placeholder.page
+ cache_key = self.get_cache_key(page, placeholder.slot)
+ cache.delete(cache_key)
+
+ @staticmethod
+ def get_cache_key(page, placeholder_name):
+ return 'footnote_plugins_%d_%s' % (page.pk, placeholder_name)
View
7 cmsplugin_footnote/templatetags/footnote.py
@@ -1,6 +1,7 @@
# coding: utf-8
from django.template import Library
+from ..utils import get_footnote_plugins
register = Library()
@@ -10,10 +11,6 @@
def footnote_list(context, placeholder_name, page=None):
if page is None:
page = context['request'].current_page
- placeholder = page.placeholders.get(slot=placeholder_name)
- plugins = placeholder.cmsplugin_set.filter(plugin_type='FootnotePlugin') \
- .order_by('tree_id', 'rght')
- footnote_plugins = (p.get_plugin_instance()[0] for p in plugins)
- context['footnote_plugins'] = footnote_plugins
+ context['footnote_plugins'] = get_footnote_plugins(page, placeholder_name)
context['placeholder_name'] = placeholder_name
return context
View
15 cmsplugin_footnote/utils.py
@@ -0,0 +1,15 @@
+from .models import Footnote
+from django.core.cache import cache
+
+
+def get_footnote_plugins(page, placeholder_name):
+ cache_key = Footnote.get_cache_key(page, placeholder_name)
+ footnote_plugins = cache.get(cache_key)
+ if footnote_plugins is None:
+ placeholder = page.placeholders.get(slot=placeholder_name)
+ plugins = placeholder.cmsplugin_set \
+ .filter(plugin_type='FootnotePlugin') \
+ .order_by('tree_id', 'rght')
+ footnote_plugins = [p.get_plugin_instance()[0] for p in plugins]
+ cache.set(cache_key, footnote_plugins)
+ return footnote_plugins

0 comments on commit 5ee6190

Please sign in to comment.