Permalink
Browse files

Fixes footnotes ordering.

  • Loading branch information...
1 parent f6cb454 commit e25b87caab95be07a4621e7628290fa263756d36 @BertrandBordage committed Sep 13, 2012
@@ -4,8 +4,9 @@
from .models import Footnote
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
-from .utils import get_footnote_plugins
+from .utils import get_footnotes_for_page
from cms.plugin_pool import plugin_pool
+from .utils import delete_cache_key
class FootnotePlugin(TextPlugin):
@@ -26,11 +27,15 @@ def icon_src(self):
def render(self, context, instance, placeholder_name):
context = super(FootnotePlugin, self).render(context, instance,
placeholder_name)
- page = context['request'].current_page
- footnote_plugins = get_footnote_plugins(page, placeholder_name)
- context['counter'] = footnote_plugins.index(instance) + 1
- context['placeholder_name'] = placeholder_name
+ request = context['request']
+ page = request.current_page
+ footnotes = get_footnotes_for_page(request, page)
+ context['counter'] = footnotes.index(instance) + 1
return context
+ def save_model(self, *args, **kwargs):
+ super(FootnotePlugin, self).save_model(*args, **kwargs)
+ delete_cache_key(self.placeholder.page)
+
plugin_pool.register_plugin(FootnotePlugin)
@@ -3,8 +3,6 @@
from cms.plugins.text.models import AbstractText
from django.db.models import CharField
from django.utils.translation import ugettext_lazy as _
-from .utils import get_cache_key
-from django.core.cache import cache
class Footnote(AbstractText):
@@ -14,9 +12,3 @@ class Footnote(AbstractText):
class Meta:
verbose_name = _('Footnote')
verbose_name_plural = _('Footnotes')
-
- def save(self, *args, **kwargs):
- super(Footnote, self).save(*args, **kwargs)
- placeholder, page = self.placeholder, self.placeholder.page
- cache_key = get_cache_key(page, placeholder.slot)
- cache.delete(cache_key)
@@ -0,0 +1,7 @@
+# coding: utf-8
+
+from django.conf import settings
+
+
+CMSPLUGIN_FOOTNOTE_DEBUG = getattr(settings, 'CMSPLUGIN_FOOTNOTE_DEBUG',
+ False)
@@ -5,7 +5,7 @@
{% endaddtoblock %}
{% spaceless %}
- {% for footnote_plugin in footnote_plugins %}
+ {% for footnote_plugin in footnotes %}
{% if forloop.first %}
<div class="cms-footnote-list-hline"></div>
@@ -1,16 +1,16 @@
# coding: utf-8
from django.template import Library
-from ..utils import get_footnote_plugins
+from ..utils import get_footnotes_for_page
register = Library()
@register.inclusion_tag('cmsplugin_footnote/footnote_list.html',
takes_context=True)
-def footnote_list(context, placeholder_name, page=None):
+def footnote_list(context, page=None):
+ request = context['request']
if page is None:
- page = context['request'].current_page
- context['footnote_plugins'] = get_footnote_plugins(page, placeholder_name)
- context['placeholder_name'] = placeholder_name
+ page = request.current_page
+ context['footnotes'] = get_footnotes_for_page(request, page)
return context
@@ -1,20 +1,64 @@
# coding: utf-8
from django.core.cache import cache
+from cms.utils.moderator import get_cmsplugin_queryset
+from cms.plugins.text.utils import plugin_tags_to_id_list
+from cms.plugins.utils import downcast_plugins
+from cms.models import CMSPlugin
+from .settings import CMSPLUGIN_FOOTNOTE_DEBUG
-def get_cache_key(page, placeholder_name):
- return 'footnote_plugins_%d_%s' % (page.pk, placeholder_name)
+def get_cache_key(page, plugins):
+ return 'footnote_plugins__%d_%d' \
+ % (page.pk, plugins.filter(placeholder__page=page,
+ plugin_type='FootnotePlugin').count())
-def get_footnote_plugins(page, placeholder_name):
- cache_key = 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
+def delete_cache_key(page):
+ plugins = CMSPlugin.objects.all()
+ cache_key = get_cache_key(page, plugins)
+ cache.delete(cache_key)
+
+
+def plugin_is_footnote(plugin):
+ return plugin.plugin_type == 'FootnotePlugin'
+
+
+def plugin_iterator_from_text_plugin(text_plugin):
+ plugin_pk_list = plugin_tags_to_id_list(text_plugin.body)
+ for pk in plugin_pk_list:
+ try:
+ yield CMSPlugin.objects.get(pk=pk)
+ except CMSPlugin.DoesNotExist, e:
+ if CMSPLUGIN_FOOTNOTE_DEBUG:
+ raise e
+
+
+def get_footnotes_for_page(request, page):
+ '''
+ Gets the Footnote instances for `page`, with the correct order.
+ '''
+ plugins = get_cmsplugin_queryset(request)
+ cache_key = get_cache_key(page, plugins)
+ footnotes = cache.get(cache_key)
+ if footnotes is None:
+ print u'raté :\\'
+ root_footnote_and_text_plugins = plugins.filter(
+ placeholder__page=page,
+ plugin_type__in=('FootnotePlugin', 'TextPlugin'),
+ parent=None
+ ).order_by('position')
+ footnote_plugins = []
+ footnote_plugins__append = footnote_plugins.append
+ for p in root_footnote_and_text_plugins:
+ if plugin_is_footnote(p):
+ footnote_plugins__append(p)
+ else:
+ text = downcast_plugins((p,))[0]
+ plugin_iterator = plugin_iterator_from_text_plugin(text)
+ for plugin in plugin_iterator:
+ if plugin_is_footnote(plugin):
+ footnote_plugins__append(plugin)
+ footnotes = downcast_plugins(footnote_plugins)
+ cache.set(cache_key, footnotes)
+ return footnotes

0 comments on commit e25b87c

Please sign in to comment.