From d33031533a41e1df02048ff7591759fade7b5b84 Mon Sep 17 00:00:00 2001 From: Leonardo Cavallucci Date: Fri, 22 Dec 2023 14:30:22 +0100 Subject: [PATCH] Fix FeaturedPostsPlugin get_posts method rendering all posts instead of selected ones (#783) --- changes/781.bugfix | 1 + djangocms_blog/models.py | 6 +++--- tests/test_models.py | 3 ++- tests/test_plugins.py | 11 ++++++++--- 4 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 changes/781.bugfix diff --git a/changes/781.bugfix b/changes/781.bugfix new file mode 100644 index 00000000..95be8424 --- /dev/null +++ b/changes/781.bugfix @@ -0,0 +1 @@ +Fix FeaturedPostsPlugin get_posts method rendering all posts instead of selected ones diff --git a/djangocms_blog/models.py b/djangocms_blog/models.py index 66a39c0c..7237421b 100644 --- a/djangocms_blog/models.py +++ b/djangocms_blog/models.py @@ -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: @@ -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 diff --git a/tests/test_models.py b/tests/test_models.py index 8c270f3c..f589e6df 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -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"]) diff --git a/tests/test_plugins.py b/tests/test_plugins.py index 9123c795..440eb92b 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -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("

first line

") > -1) @@ -181,6 +181,11 @@ def test_plugin_featured(self): self.assertTrue(rendered.find('
-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()