[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@bbabb2fdb6
Part-of: odoo/odoo#95471
This commit is contained in:
Thibault Delavallée
2022-07-07 17:31:01 +02:00
parent 647f7a90a4
commit d1276d54a4
2 changed files with 8 additions and 5 deletions
+8 -4
View File
@@ -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'])
@@ -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]