From 4e56b39b7c323162a742a394dedbd289355a25be Mon Sep 17 00:00:00 2001 From: Leonardo Cavallucci Date: Fri, 22 Dec 2023 13:03:51 +0100 Subject: [PATCH 1/3] Initial commit to make tests fail --- changes/781.bugfix | 1 + djangocms_blog/models.py | 8 ++++++-- tests/test_models.py | 3 ++- tests/test_plugins.py | 11 ++++++++--- 4 files changed, 17 insertions(+), 6 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..531d6a02 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,6 +611,10 @@ def copy_relations(self, oldinstance): self.posts.set(oldinstance.posts.all()) def get_posts(self, request, published_only=True): + # if self.posts.exists(): + # posts = self.post_queryset(request, published_only, selected_posts=self.posts.all()) + # else: + # posts = self.post_queryset(request, published_only) posts = self.post_queryset(request, published_only) 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..4f4f057d 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(16, 17)): self.render_plugin(pages[0], "en", plugin_nocache) - with self.assertNumQueries(FuzzyInt(14, 15)): + with self.assertNumQueries(FuzzyInt(16, 17)): self.render_plugin(pages[0], "en", plugin) - with self.assertNumQueries(FuzzyInt(14, 15)): + with self.assertNumQueries(FuzzyInt(16, 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() From a727f3847cb4440aa228c26d7aaa848978bd048e Mon Sep 17 00:00:00 2001 From: Leonardo Cavallucci Date: Fri, 22 Dec 2023 13:05:34 +0100 Subject: [PATCH 2/3] Fix FeaturedPostsPlugin get_posts method rendering all posts instead of selected ones --- djangocms_blog/models.py | 6 +----- tests/test_plugins.py | 6 +++--- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/djangocms_blog/models.py b/djangocms_blog/models.py index 531d6a02..7237421b 100644 --- a/djangocms_blog/models.py +++ b/djangocms_blog/models.py @@ -611,11 +611,7 @@ def copy_relations(self, oldinstance): self.posts.set(oldinstance.posts.all()) def get_posts(self, request, published_only=True): - # if self.posts.exists(): - # posts = self.post_queryset(request, published_only, selected_posts=self.posts.all()) - # else: - # posts = self.post_queryset(request, published_only) - 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_plugins.py b/tests/test_plugins.py index 4f4f057d..36611be2 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(16, 17)): + with self.assertNumQueries(FuzzyInt(15, 17)): self.render_plugin(pages[0], "en", plugin_nocache) - with self.assertNumQueries(FuzzyInt(16, 17)): + with self.assertNumQueries(FuzzyInt(15, 17)): self.render_plugin(pages[0], "en", plugin) - with self.assertNumQueries(FuzzyInt(16, 17)): + with self.assertNumQueries(FuzzyInt(15, 17)): rendered = self.render_plugin(pages[0], "en", plugin) self.assertTrue(rendered.find("

first line

") > -1) From f8de632030ff724f96f73fbdbe6faaddbdb4c65f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 22 Dec 2023 12:16:24 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_plugins.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_plugins.py b/tests/test_plugins.py index 36611be2..440eb92b 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -182,7 +182,7 @@ def test_plugin_featured(self): 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)