Move plugin post queryset in the plugin models

This commit is contained in:
Iacopo Spalletti 2014-12-05 15:40:24 +01:00
parent 1cd52bd389
commit 37502d73da
4 changed files with 26 additions and 9 deletions

View file

@ -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

View file

@ -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):

View file

@ -2,7 +2,7 @@
<div class="plugin plugin-blog">
<h3>{% trans "Authors" %}</h3>
<ul class="blog-authors">
{% for author in instance.get_authors %}
{% for author in authors_list %}
<li><a href="{% url 'djangocms_blog:posts-author' author.get_username %}">
{{ author.get_full_name }}
<span>(

View file

@ -1,7 +1,7 @@
{% load i18n %}{% spaceless %}
<div class="plugin plugin-blog">
<div class="blog-latest-entries">
{% for post in instance.get_posts %}
{% for post in posts_list %}
{% include "djangocms_blog/includes/blog_item.html" with post=post TRUNCWORDS_COUNT=TRUNCWORDS_COUNT %}
{% empty %}
<p class="blog-empty">{% trans "No article found." %}</p>