Skip to content

Commit

Permalink
[FIX] base_custom_filter: Prevent all records from being displayed in…
Browse files Browse the repository at this point in the history
… favorites

TT43350
  • Loading branch information
victoralmau authored and AungKoKoLin1997 committed May 17, 2023
1 parent 285a0a0 commit 9f941b9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
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 @@ -28,3 +29,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

0 comments on commit 9f941b9

Please sign in to comment.