Move plugin post queryset in the plugin models
This commit is contained in:
parent
1cd52bd389
commit
37502d73da
4 changed files with 26 additions and 9 deletions
|
@ -24,6 +24,7 @@ class BlogLatestEntriesPlugin(BlogPlugin):
|
||||||
|
|
||||||
def render(self, context, instance, placeholder):
|
def render(self, context, instance, placeholder):
|
||||||
context['instance'] = instance
|
context['instance'] = instance
|
||||||
|
context['posts_list'] = instance.get_posts(context['request'])
|
||||||
context['TRUNCWORDS_COUNT'] = get_setting('POSTS_LIST_TRUNCWORDS_COUNT')
|
context['TRUNCWORDS_COUNT'] = get_setting('POSTS_LIST_TRUNCWORDS_COUNT')
|
||||||
return context
|
return context
|
||||||
|
|
||||||
|
@ -38,6 +39,7 @@ class BlogAuthorPostsPlugin(BlogPlugin):
|
||||||
|
|
||||||
def render(self, context, instance, placeholder):
|
def render(self, context, instance, placeholder):
|
||||||
context['instance'] = instance
|
context['instance'] = instance
|
||||||
|
context['authors_list'] = instance.get_authors()
|
||||||
return context
|
return context
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -219,7 +219,23 @@ class Post(ModelMeta, TranslatableModel):
|
||||||
|
|
||||||
|
|
||||||
@python_2_unicode_compatible
|
@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'),
|
latest_posts = models.IntegerField(_(u'Articles'), default=get_setting('LATEST_POSTS'),
|
||||||
help_text=_('The number of latests articles to be displayed.'))
|
help_text=_('The number of latests articles to be displayed.'))
|
||||||
|
@ -234,16 +250,15 @@ class LatestPostsPlugin(CMSPlugin):
|
||||||
def copy_relations(self, oldinstance):
|
def copy_relations(self, oldinstance):
|
||||||
self.tags = oldinstance.tags.all()
|
self.tags = oldinstance.tags.all()
|
||||||
|
|
||||||
def get_posts(self):
|
def get_posts(self, request):
|
||||||
posts = Post.objects.published()
|
posts = self.post_queryset(request)
|
||||||
tags = list(self.tags.all())
|
tags = list(self.tags.all())
|
||||||
if tags:
|
if tags:
|
||||||
posts = posts.filter(tags__in=tags)
|
posts = posts.filter(tags__in=tags)
|
||||||
return posts[:self.latest_posts]
|
return posts[:self.latest_posts]
|
||||||
|
|
||||||
|
|
||||||
@python_2_unicode_compatible
|
class AuthorEntriesPlugin(BasePostPlugin):
|
||||||
class AuthorEntriesPlugin(CMSPlugin):
|
|
||||||
authors = models.ManyToManyField(
|
authors = models.ManyToManyField(
|
||||||
dj_settings.AUTH_USER_MODEL, verbose_name=_('Authors'),
|
dj_settings.AUTH_USER_MODEL, verbose_name=_('Authors'),
|
||||||
limit_choices_to={'djangocms_blog_post_author__publish': True}
|
limit_choices_to={'djangocms_blog_post_author__publish': True}
|
||||||
|
@ -259,8 +274,8 @@ class AuthorEntriesPlugin(CMSPlugin):
|
||||||
def copy_relations(self, oldinstance):
|
def copy_relations(self, oldinstance):
|
||||||
self.authors = oldinstance.authors.all()
|
self.authors = oldinstance.authors.all()
|
||||||
|
|
||||||
def get_posts(self):
|
def get_posts(self, request):
|
||||||
posts = (Post.objects.published().filter(author__in=self.authors.all()))
|
posts = self.post_queryset(request)
|
||||||
return posts[:self.latest_posts]
|
return posts[:self.latest_posts]
|
||||||
|
|
||||||
def get_authors(self):
|
def get_authors(self):
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
<div class="plugin plugin-blog">
|
<div class="plugin plugin-blog">
|
||||||
<h3>{% trans "Authors" %}</h3>
|
<h3>{% trans "Authors" %}</h3>
|
||||||
<ul class="blog-authors">
|
<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 %}">
|
<li><a href="{% url 'djangocms_blog:posts-author' author.get_username %}">
|
||||||
{{ author.get_full_name }}
|
{{ author.get_full_name }}
|
||||||
<span>(
|
<span>(
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{% load i18n %}{% spaceless %}
|
{% load i18n %}{% spaceless %}
|
||||||
<div class="plugin plugin-blog">
|
<div class="plugin plugin-blog">
|
||||||
<div class="blog-latest-entries">
|
<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 %}
|
{% include "djangocms_blog/includes/blog_item.html" with post=post TRUNCWORDS_COUNT=TRUNCWORDS_COUNT %}
|
||||||
{% empty %}
|
{% empty %}
|
||||||
<p class="blog-empty">{% trans "No article found." %}</p>
|
<p class="blog-empty">{% trans "No article found." %}</p>
|
||||||
|
|
Loading…
Reference in a new issue