Skip to content

Commit

Permalink
Fix FeaturedPostsPlugin get_posts method rendering all posts instead …
Browse files Browse the repository at this point in the history
…of selected ones (nephila#783)
  • Loading branch information
protoroto authored Dec 22, 2023
1 parent f2ff618 commit d330315
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions changes/781.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix FeaturedPostsPlugin get_posts method rendering all posts instead of selected ones
6 changes: 3 additions & 3 deletions djangocms_blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,9 +513,9 @@ def optimize(self, qs):
"translations", "categories", "categories__translations", "categories__app_config"
)

def post_queryset(self, request=None, published_only=True):
def post_queryset(self, request=None, published_only=True, selected_posts=None):
language = get_language()
posts = Post.objects
posts = Post.objects if not selected_posts else selected_posts
if self.app_config:
posts = posts.namespace(self.app_config.namespace)
if self.current_site:
Expand Down Expand Up @@ -611,7 +611,7 @@ def copy_relations(self, oldinstance):
self.posts.set(oldinstance.posts.all())

def get_posts(self, request, published_only=True):
posts = self.post_queryset(request, published_only)
posts = self.post_queryset(request, published_only, selected_posts=self.posts.all())
return posts


Expand Down
3 changes: 2 additions & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1364,10 +1364,11 @@ def test_plugin_featured_posts(self):
plugin = add_plugin(post1.content, "BlogFeaturedPostsPlugin", language="en", app_config=self.app_config_1)
plugin.posts.add(post1, post2)
self.assertEqual(len(plugin.get_posts(request)), 1)

post2.publish = True
post2.save()
self.assertEqual(len(plugin.get_posts(request)), 2)
plugin.posts.remove(post2)
self.assertEqual(len(plugin.get_posts(request)), 1)

def test_copy_plugin_featured_post(self):
post1 = self._get_post(self._post_data[0]["en"])
Expand Down
11 changes: 8 additions & 3 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,13 @@ def test_plugin_featured_cached(self):
plugin_nocache = add_plugin(ph, "BlogFeaturedPostsPlugin", language="en", app_config=self.app_config_1)
plugin_nocache.posts.add(posts[0])
# FIXME: Investigate the correct number of queries expected here
with self.assertNumQueries(FuzzyInt(14, 15)):
with self.assertNumQueries(FuzzyInt(15, 17)):
self.render_plugin(pages[0], "en", plugin_nocache)

with self.assertNumQueries(FuzzyInt(14, 15)):
with self.assertNumQueries(FuzzyInt(15, 17)):
self.render_plugin(pages[0], "en", plugin)

with self.assertNumQueries(FuzzyInt(14, 15)):
with self.assertNumQueries(FuzzyInt(15, 17)):
rendered = self.render_plugin(pages[0], "en", plugin)

self.assertTrue(rendered.find("<p>first line</p>") > -1)
Expand Down Expand Up @@ -181,6 +181,11 @@ def test_plugin_featured(self):
self.assertTrue(rendered.find('<article id="post-second-post"') > -1)
self.assertTrue(rendered.find(posts[0].get_absolute_url()) > -1)
self.assertTrue(rendered.find(posts[1].get_absolute_url()) > -1)
plugin.posts.remove(posts[1])

rendered = self.render_plugin(pages[0], "en", plugin, edit=True)
self.assertTrue(rendered.find(posts[0].get_absolute_url()) > -1)
self.assertFalse(rendered.find(posts[1].get_absolute_url()) > -1)

def test_plugin_tags(self):
pages = self.get_pages()
Expand Down

0 comments on commit d330315

Please sign in to comment.