From d1276d54a401c842866dd99f5741bae23fd429ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Mon, 4 Jul 2022 12:19:26 +0000 Subject: [PATCH] [FW][FIX] knowledge: fix ordering, especially with user preferences Several issues with article model ordering have been found and are fixed in this commit: * default order of model as ``favorite_count`` currently ascending while we actually want most favorite articles to be on top; * when searching sorted articles per user, we use the sequence on favorite model. When ordering on this field we therefore have to fetch all favorite articles and then order them based on user's sequence. Otherwise when having more favorite articles than asked the user's sequence is not taken into account; * specify order in various searches to make it explicit; Task-2901990 X-original-commit: odoo/odoo@bbabb2fdb6ab8b0f6c5529ec27efb2b5ccfc3a86 Part-of: odoo/odoo#95471 --- addons/knowledge/models/knowledge_article.py | 12 ++++++++---- .../tests/test_knowledge_article_business.py | 1 - 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/addons/knowledge/models/knowledge_article.py b/addons/knowledge/models/knowledge_article.py index 87fddb825c7..4d488e64993 100644 --- a/addons/knowledge/models/knowledge_article.py +++ b/addons/knowledge/models/knowledge_article.py @@ -19,7 +19,7 @@ class Article(models.Model): _name = "knowledge.article" _description = "Knowledge Article" _inherit = ['mail.thread', 'mail.activity.mixin'] - _order = "favorite_count, create_date desc, id desc" + _order = "favorite_count desc, write_date desc, id desc" _mail_post_access = 'read' active = fields.Boolean(default=True) @@ -434,7 +434,8 @@ class Article(models.Model): if (value and operator == '=') or (not value and operator == '!='): return [('favorite_ids.user_id', 'in', [self.env.uid])] - # easier than a not in on a 2many field + # easier than a not in on a 2many field (hint: use sudo because of + # complicated ACL on favorite based on user access on article) favorited = self.env['knowledge.article.favorite'].sudo().search( [('user_id', '=', self.env.uid)] ).article_id @@ -876,7 +877,7 @@ class Article(models.Model): search_domain = ["|", ("name", "ilike", search_query), ("root_article_id.name", "ilike", search_query)] articles = self.search( expression.AND([search_domain, [("is_user_favorite", "=", True)]]), - limit=limit + limit=None, ) sorted_articles = articles.sorted( key=lambda a: (-1 * a.user_favorite_sequence, @@ -890,7 +891,8 @@ class Article(models.Model): if len(sorted_articles) < limit: articles = self.search( expression.AND([search_domain, [("is_user_favorite", "=", False)]]), - limit=(limit-len(sorted_articles)) + limit=(limit-len(sorted_articles)), + order='favorite_count desc, write_date desc, id desc' ) sorted_articles += articles.sorted( key=lambda a: (a.favorite_count, @@ -898,6 +900,8 @@ class Article(models.Model): a.id), reverse=True ) + elif len(sorted_articles) > limit: + sorted_articles = sorted_articles[:limit] return sorted_articles.read(['id', 'name', 'is_user_favorite', 'favorite_count', 'root_article_id', 'icon']) diff --git a/addons/knowledge/tests/test_knowledge_article_business.py b/addons/knowledge/tests/test_knowledge_article_business.py index 5633387773d..bc7b4d78014 100644 --- a/addons/knowledge/tests/test_knowledge_article_business.py +++ b/addons/knowledge/tests/test_knowledge_article_business.py @@ -529,7 +529,6 @@ class TestKnowledgeArticleBusiness(KnowledgeCommonWData): self.assertEqual(other.favorite_count, 0) self.assertEqual(other.user_favorite_sequence, -1) - # TDE FIXME: currently failing # search also includes descendants of articles having the term in their name result = self.env['knowledge.article'].get_user_sorted_articles('laygroun', limit=4) expected = self.article_workspace + self.workspace_children[1] + self.wkspace_grandchildren[2] + self.workspace_children[0]