Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0][FIX] base_custom_filter: Prevent all records from being displayed in favorites #657

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion base_custom_filter/models/ir_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# Copyright 2021 ForgeFlow S.L. (https://www.forgeflow.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import fields, models
from odoo import api, fields, models
from odoo.osv import expression


class IrFilters(models.Model):
Expand All @@ -29,3 +30,19 @@ def _selection_type(self):
ondelete="cascade",
)
group_id = fields.Many2one(comodel_name="ir.filters.group", string="Filter Group")

@api.model
def get_filters(self, model, action_id=None):
"""We need to inject a context to obtain only the records of favorite type."""
self = self.with_context(filter_type="favorite")
return super().get_filters(model, action_id=action_id)

@api.model
def search(self, args, offset=0, limit=None, order=None, count=False):
if self.env.context.get("filter_type"):
args = expression.AND(
(args, [("type", "=", self.env.context["filter_type"])])
)
return super().search(
args, offset=offset, limit=limit, order=order, count=count
)
18 changes: 16 additions & 2 deletions base_custom_filter/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,28 @@ def setUpClass(cls, chart_template_ref=None):
filters_group.groupby_field = cls.env.ref(
"base_custom_filter.field_ir_filters_group__name"
)
filters_group = filters_group.save()
cls.filters_groupby = filters_group.save()

filters_group = Form(filters_obj)
filters_group.name = "Test No filters group"
filters_group.type = "filter"
filters_group.model_id = "ir.filters.group"
filters_group.domain = '[["id","=",1]]'
filters_group = filters_group.save()
cls.filters_filter = filters_group.save()

filters_group = Form(filters_obj)
filters_group.name = "Test favorite"
filters_group.type = "favorite"
filters_group.model_id = "ir.filters.group"
filters_group.domain = '[["id","=",1]]'
cls.filters_favorite = filters_group.save()

def test_filters_favorite(self):
res = self.env["ir.filters"].get_filters("ir.filters.group")
res_ids = [item["id"] for item in res]
self.assertNotIn(self.filters_groupby.id, res_ids)
self.assertNotIn(self.filters_filter.id, res_ids)
self.assertIn(self.filters_favorite.id, res_ids)

def test_sale_order_line(self):
filters_group_obj = self.env["ir.filters.group"]
Expand Down