From 37502d73da60160f15bfeb099be6f9d5a7e7cb5b Mon Sep 17 00:00:00 2001 From: Iacopo Spalletti Date: Fri, 5 Dec 2014 15:40:24 +0100 Subject: [PATCH] Move plugin post queryset in the plugin models --- djangocms_blog/cms_plugins.py | 2 ++ djangocms_blog/models.py | 29 ++++++++++++++----- .../djangocms_blog/plugins/authors.html | 2 +- .../plugins/latest_entries.html | 2 +- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/djangocms_blog/cms_plugins.py b/djangocms_blog/cms_plugins.py index 91ce5c5..34074c4 100644 --- a/djangocms_blog/cms_plugins.py +++ b/djangocms_blog/cms_plugins.py @@ -24,6 +24,7 @@ class BlogLatestEntriesPlugin(BlogPlugin): 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 @@ -38,6 +39,7 @@ class BlogAuthorPostsPlugin(BlogPlugin): def render(self, context, instance, placeholder): context['instance'] = instance + context['authors_list'] = instance.get_authors() return context diff --git a/djangocms_blog/models.py b/djangocms_blog/models.py index 63cd67e..78d957f 100644 --- a/djangocms_blog/models.py +++ b/djangocms_blog/models.py @@ -219,7 +219,23 @@ class Post(ModelMeta, TranslatableModel): @python_2_unicode_compatible -class LatestPostsPlugin(CMSPlugin): +class BasePostPlugin(CMSPlugin): + + class Meta: + abstract = True + + def post_queryset(self, request): + language = get_language() + posts = Post._default_manager.active_translations(language_code=language) + if not request.toolbar or not request.toolbar.edit_mode: + posts = posts.published() + return posts + + def __str__(self): + return unicode(self.latest_posts) + + +class LatestPostsPlugin(BasePostPlugin): latest_posts = models.IntegerField(_(u'Articles'), default=get_setting('LATEST_POSTS'), help_text=_('The number of latests articles to be displayed.')) @@ -234,16 +250,15 @@ class LatestPostsPlugin(CMSPlugin): def copy_relations(self, oldinstance): self.tags = oldinstance.tags.all() - def get_posts(self): - posts = Post.objects.published() + def get_posts(self, request): + posts = self.post_queryset(request) tags = list(self.tags.all()) if tags: posts = posts.filter(tags__in=tags) return posts[:self.latest_posts] -@python_2_unicode_compatible -class AuthorEntriesPlugin(CMSPlugin): +class AuthorEntriesPlugin(BasePostPlugin): authors = models.ManyToManyField( dj_settings.AUTH_USER_MODEL, verbose_name=_('Authors'), limit_choices_to={'djangocms_blog_post_author__publish': True} @@ -259,8 +274,8 @@ class AuthorEntriesPlugin(CMSPlugin): def copy_relations(self, oldinstance): self.authors = oldinstance.authors.all() - def get_posts(self): - posts = (Post.objects.published().filter(author__in=self.authors.all())) + def get_posts(self, request): + posts = self.post_queryset(request) return posts[:self.latest_posts] def get_authors(self): diff --git a/djangocms_blog/templates/djangocms_blog/plugins/authors.html b/djangocms_blog/templates/djangocms_blog/plugins/authors.html index 759f494..6c9aa66 100644 --- a/djangocms_blog/templates/djangocms_blog/plugins/authors.html +++ b/djangocms_blog/templates/djangocms_blog/plugins/authors.html @@ -2,7 +2,7 @@

{% trans "Authors" %}