From 43cb802097e645149cd93d57ae49aaefc7e46caf Mon Sep 17 00:00:00 2001 From: Iacopo Spalletti Date: Sun, 28 Dec 2014 16:03:44 +0100 Subject: [PATCH] If using the request to filter posts, plugin cannot be cached --- djangocms_blog/cms_plugins.py | 23 ++++++++++++++++++++++- djangocms_blog/models.py | 4 ++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/djangocms_blog/cms_plugins.py b/djangocms_blog/cms_plugins.py index 34074c4..b2d00da 100644 --- a/djangocms_blog/cms_plugins.py +++ b/djangocms_blog/cms_plugins.py @@ -15,7 +15,28 @@ class BlogPlugin(CMSPluginBase): class BlogLatestEntriesPlugin(BlogPlugin): + """ + Non cached plugin which returns the latest posts taking into account the + user / toolbar state + """ + render_template = 'djangocms_blog/plugins/latest_entries.html' + name = _('Latest Blog Articles') + model = LatestPostsPlugin + form = LatestEntriesForm + filter_horizontal = ('categories',) + cache = False + def render(self, context, instance, placeholder): + context['instance'] = instance + context['posts_list'] = instance.get_posts(context['request']) + context['TRUNCWORDS_COUNT'] = get_setting('POSTS_LIST_TRUNCWORDS_COUNT') + return context + + +class BlogLatestEntriesPluginCached(BlogPlugin): + """ + Cached plugin which returns the latest published posts + """ render_template = 'djangocms_blog/plugins/latest_entries.html' name = _('Latest Blog Articles') model = LatestPostsPlugin @@ -24,7 +45,7 @@ class BlogLatestEntriesPlugin(BlogPlugin): def render(self, context, instance, placeholder): context['instance'] = instance - context['posts_list'] = instance.get_posts(context['request']) + context['posts_list'] = instance.get_posts() context['TRUNCWORDS_COUNT'] = get_setting('POSTS_LIST_TRUNCWORDS_COUNT') return context diff --git a/djangocms_blog/models.py b/djangocms_blog/models.py index 0874ba3..62e787b 100644 --- a/djangocms_blog/models.py +++ b/djangocms_blog/models.py @@ -224,10 +224,10 @@ class BasePostPlugin(CMSPlugin): class Meta: abstract = True - def post_queryset(self, request): + def post_queryset(self, request=None): language = get_language() posts = Post._default_manager.active_translations(language_code=language) - if not getattr(request, 'toolbar', False) or not request.toolbar.edit_mode: + if not request or not getattr(request, 'toolbar', False) or not request.toolbar.edit_mode: posts = posts.published() return posts