Skip to content

Commit

Permalink
[13.0][ADD] base_custom_filter
Browse files Browse the repository at this point in the history
  • Loading branch information
JordiMForgeFlow authored and AungKoKoLin1997 committed Apr 28, 2023
1 parent 5ffb48a commit fe7ec7c
Show file tree
Hide file tree
Showing 16 changed files with 1,007 additions and 0 deletions.
109 changes: 109 additions & 0 deletions base_custom_filter/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
=======================================
Add custom filters in filters dropdowns
=======================================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--ux-lightgray.png?logo=github
:target: https://github.com/OCA/server-ux/tree/13.0/base_search_custom_field_filter
:alt: OCA/server-ux
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/server-ux-13-0/server-ux-13-0-base_search_custom_field_filter
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/250/13.0
:alt: Try me on Runbot

|badge1| |badge2| |badge3| |badge4| |badge5|

This module allows to define custom filters to be shown under the standard
filters and group by menus of a model's search view.

**Table of contents**

.. contents::
:local:

Configuration
=============

#. Go to *Settings > Custom Filters*.
#. Create a new Custom Filter, and define following information:

* The **Model** for which you are defining the filter.
* The **Type** depending on whether you want to add a filter or a grouping by
by a field.
* The **Domain** that will be applied with the filter.
* The **Group By Field** used to perform the group by.
* The **Group** to have filters under the same separator.

#. You can reorder records from the list view with the arrow handle. This will
determine the order in which they appear in the filters/groupby menu.
#. A separator is added between each custom filter added. You can create Custom
Filter Groups to group filters that you want to have displayed under the same
separator.

Usage
=====

#. Go to the model's menu entry for which you have defined the filter.
#. On the filters and group by dropdowns, you will see the configured filters.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-ux/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/server-ux/issues/new?body=module:%20base_custom_filter%0Aversion:%2013.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* ForgeFlow

Contributors
~~~~~~~~~~~~

* `ForgeFlow S.L. <https://www.forgeflow.com>`_:

* Jordi Masvidal

Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

.. |maintainer-JordiMForgeFlow| image:: https://github.com/JordiMForgeFlow.png?size=40px
:target: https://github.com/JordiMForgeFlow
:alt: JordiMForgeFlow

Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-JordiMForgeFlow|

This module is part of the `OCA/server-ux <https://github.com/OCA/server-ux/tree/13.0/base_custom_filter>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 2 additions & 0 deletions base_custom_filter/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from . import models
18 changes: 18 additions & 0 deletions base_custom_filter/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2021 ForgeFlow S.L. (https://www.forgeflow.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
{
"name": "Add custom filters in standard filters and group by dropdowns",
"version": "13.0.1.0.0",
"category": "Usability",
"website": "https://github.com/OCA/server-ux",
"author": "ForgeFlow, Odoo Community Association (OCA)",
"data": [
"security/ir.model.access.csv",
"views/ir_filters_views.xml",
"views/ir_filters_group_views.xml",
],
"depends": ["web"],
"license": "AGPL-3",
"installable": True,
"maintainers": ["JordiMForgeFlow"],
}
4 changes: 4 additions & 0 deletions base_custom_filter/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from . import base
from . import ir_filters
from . import ir_filters_group
113 changes: 113 additions & 0 deletions base_custom_filter/models/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Copyright 2021 ForgeFlow S.L. (https://www.forgeflow.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from lxml import etree

from odoo import api, models


class Base(models.AbstractModel):
_inherit = "base"

@api.model
def _add_grouped_filters(self, res, custom_filters):
arch = etree.fromstring(res["arch"])
node = arch.xpath("//search/filter[last()]")
if node:
node[0].addnext(etree.Element("separator"))
for custom_filter in custom_filters:
node = arch.xpath("//search/separator[last()]")
if node:
elem = etree.Element(
"filter",
{
"name": "ir_custom_filter_%s" % custom_filter.id,
"string": custom_filter.name,
"domain": custom_filter.domain,
},
)
node[0].addnext(elem)
res["arch"] = etree.tostring(arch)
return res

@api.model
def _add_grouped_groupby(self, res, custom_groupbys):
arch = etree.fromstring(res["arch"])
node = arch.xpath("//group/filter[last()]")
if node:
node[0].addnext(etree.Element("separator"))
for custom_groupby in custom_groupbys:
node = arch.xpath("//group/separator[last()]")
if node:
elem = etree.Element(
"filter",
{
"name": "ir_custom_filter_%s" % custom_groupby.id,
"string": custom_groupby.name,
"context": str(
{"group_by": custom_groupby.groupby_field.name}
),
},
)
node[0].addnext(elem)
res["arch"] = etree.tostring(arch)
return res

@api.model
def fields_view_get(
self, view_id=None, view_type="form", toolbar=False, submenu=False
):
"""Add filters in search views."""
res = super().fields_view_get(
view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu
)
if view_type == "search":
filter_groups = self.env["ir.filters.group"].search(
[
("model_id", "=", res.get("model")),
("filter_ids", "!=", False),
("type", "=", "filter"),
]
)
filters_no_group = self.env["ir.filters"].search(
[
("model_id", "=", res.get("model")),
("group_id", "=", False),
("type", "=", "filter"),
],
order="sequence desc",
)
groupby_groups = self.env["ir.filters.group"].search(
[
("model_id", "=", res.get("model")),
("filter_ids", "!=", False),
("type", "=", "groupby"),
]
)
groupbys_no_group = self.env["ir.filters"].search(
[
("model_id", "=", res.get("model")),
("group_id", "=", False),
("type", "=", "groupby"),
],
order="sequence desc",
)
# Add filter type
if filter_groups:
for filter_group in filter_groups:
res = self._add_grouped_filters(
res, filter_group.filter_ids.sorted("sequence", True)
)
if filters_no_group:
for filter_no_group in filters_no_group:
res = self._add_grouped_filters(res, filter_no_group)
# Add groupby type
if groupby_groups:
for groupby_group in groupby_groups:
res = self._add_grouped_groupby(
res, groupby_group.filter_ids.sorted("sequence", True)
)
if groupbys_no_group:
for groupby_no_group in groupbys_no_group:
res = self._add_grouped_groupby(res, groupby_no_group)
return res
25 changes: 25 additions & 0 deletions base_custom_filter/models/ir_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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


class IrFilters(models.Model):
_inherit = "ir.filters"
_order = "model_id, sequence, name, id desc"

def _selection_type(self):
return [
("favorite", "Favorite"),
("filter", "Standard Filter"),
("groupby", "Standard Group By"),
]

sequence = fields.Integer()
type = fields.Selection(
selection="_selection_type", string="Type", required=True, default="favorite",
)
groupby_field = fields.Many2one(
comodel_name="ir.model.fields", string="Group By Field", ondelete="cascade",
)
group_id = fields.Many2one(comodel_name="ir.filters.group", string="Filter Group")
47 changes: 47 additions & 0 deletions base_custom_filter/models/ir_filters_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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 _, api, fields, models
from odoo.exceptions import ValidationError


class IrFiltersGroup(models.Model):
_name = "ir.filters.group"
_description = "Filters Group"
_order = "sequence, name, id"

def _selection_type(self):
return [("filter", "Filter"), ("groupby", "Group By")]

sequence = fields.Integer()
model_id = fields.Selection(
selection="_list_all_models", string="Model", required=True
)
name = fields.Char(required=True, translate=True)
type = fields.Selection(
selection="_selection_type", string="Type", required=True, default="filter",
)
filter_ids = fields.One2many(
comodel_name="ir.filters", inverse_name="group_id", string="Filters"
)

@api.constrains("model_id", "filter_ids")
def _check_filter_group_model(self):
for rec in self:
if any(rec.filter_ids.filtered(lambda f: f.model_id != rec.model_id)):
raise ValidationError(
_("The group contains filters related to different models.")
)

@api.constrains("type", "filter_ids")
def _check_filter_group_type(self):
for rec in self:
if any(rec.filter_ids.filtered(lambda f: f.type != rec.type)):
raise ValidationError(
_("The group contains filters of different types.")
)

@api.model
def _list_all_models(self):
self._cr.execute("SELECT model, name FROM ir_model ORDER BY name")
return self._cr.fetchall()
15 changes: 15 additions & 0 deletions base_custom_filter/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#. Go to *Settings > Custom Filters*.
#. Create a new Custom Filter, and define following information:

* The **Model** for which you are defining the filter.
* The **Type** depending on whether you want to add a filter or a grouping by
by a field.
* The **Domain** that will be applied with the filter.
* The **Group By Field** used to perform the group by.
* The **Group** to have filters under the same separator.

#. You can reorder records from the list view with the arrow handle. This will
determine the order in which they appear in the filters/groupby menu.
#. A separator is added between each custom filter added. You can create Custom
Filter Groups to group filters that you want to have displayed under the same
separator.
3 changes: 3 additions & 0 deletions base_custom_filter/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* `ForgeFlow S.L. <https://www.forgeflow.com>`_:

* Jordi Masvidal
2 changes: 2 additions & 0 deletions base_custom_filter/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This module allows to define custom filters to be shown under the standard
filters and group by menus of a model's search view.
2 changes: 2 additions & 0 deletions base_custom_filter/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#. Go to the model's menu entry for which you have defined the filter.
#. On the filters and group by dropdowns, you will see the configured filters.
3 changes: 3 additions & 0 deletions base_custom_filter/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_ir_filters_group_user,ir.filters.group,model_ir_filters_group,base.group_user,1,0,0,0
access_ir_filters_group_system,ir.filters.group,model_ir_filters_group,base.group_system,1,1,1,1
Binary file added base_custom_filter/static/description/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit fe7ec7c

Please sign in to comment.