diff --git a/base_search_custom_field_filter/README.rst b/base_search_custom_field_filter/README.rst new file mode 100644 index 0000000..78f973b --- /dev/null +++ b/base_search_custom_field_filter/README.rst @@ -0,0 +1,125 @@ +==================================== +Add custom filters for fields via UI +==================================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! 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/12.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-12-0/server-ux-12-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/12.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module allows to define custom filters in the search views for an specific +field belonging to the document or any other related document. + +This nature makes the definition quite technical, but once done, it adds the +element in the UI for regular user use. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +#. Go to *Settings > Technical > User Interface > Custom Field Filters*. +#. Create a new record, and define following information: + + * The **Model** for which you are defining the filter. It will appear in all + the search views of this model. + * The label you want to see on the search line on the **Name** field. This + field allows translations for proper UI in different languages. + * The **Expression**, which is the field chain string with dot notation. + Examples: `product_id`, `product_id.seller_ids.name`, `partner_id.lang`. + * Optionally, you can fill **Position After** for indicating after which + existing field (technical name) the filter will appear. If empty or not + found, the filter will be added at the end. +#. You can reorder records for determining sorting for multiple filters for the + same model with the arrow handle in the left part. + +Usage +===== + +#. Go to the menu entry for which you have defined the custom field filter. +#. On the search bar, type anything. +#. In the filter list, you will see the line for the element you have defined. + +As demo data, a custom field filter is included for sample purposes: + +.. image:: https://raw.githubusercontent.com/OCA/server-ux/12.0/base_search_custom_field_filter/static/src/img/ir_ui_custom_field_filter.png + +Steps for trying this sample: + +#. Install `contacts` module. +#. Go to *Contacts*. +#. Type "english" and you'll find the filter "Language" at the end: + +.. image:: https://raw.githubusercontent.com/OCA/server-ux/12.0/base_search_custom_field_filter/static/src/img/contact_search.png + +Bug Tracker +=========== + +Bugs are tracked on `GitHub 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 `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Tecnativa + +Contributors +~~~~~~~~~~~~ + +* `Tecnativa `_: + + * Carlos Dauden + * Pedro M. Baeza + +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-pedrobaeza| image:: https://github.com/pedrobaeza.png?size=40px + :target: https://github.com/pedrobaeza + :alt: pedrobaeza + +Current `maintainer `__: + +|maintainer-pedrobaeza| + +This module is part of the `OCA/server-ux `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/base_search_custom_field_filter/__init__.py b/base_search_custom_field_filter/__init__.py new file mode 100644 index 0000000..c32fd62 --- /dev/null +++ b/base_search_custom_field_filter/__init__.py @@ -0,0 +1,2 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from . import models diff --git a/base_search_custom_field_filter/__manifest__.py b/base_search_custom_field_filter/__manifest__.py new file mode 100644 index 0000000..c2abad4 --- /dev/null +++ b/base_search_custom_field_filter/__manifest__.py @@ -0,0 +1,22 @@ +# Copyright 2020 Tecnativa - Carlos Dauden +# Copyright 2020 Tecnativa - Pedro M. Baeza +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +{ + "name": "Add custom filters for fields via UI", + "version": "12.0.1.0.1", + "category": "Usability", + "website": "https://github.com/OCA/server-ux", + "author": "Tecnativa, Odoo Community Association (OCA)", + "demo": [ + "demo/demo_ir_ui_custom_field_filter.xml", + ], + "data": [ + "security/ir.model.access.csv", + "templates/assets.xml", + "views/ir_ui_custom_field_filter_views.xml", + ], + "depends": ["web"], + "license": "AGPL-3", + "installable": True, + "maintainers": ["pedrobaeza"], +} diff --git a/base_search_custom_field_filter/demo/demo_ir_ui_custom_field_filter.xml b/base_search_custom_field_filter/demo/demo_ir_ui_custom_field_filter.xml new file mode 100644 index 0000000..9940e29 --- /dev/null +++ b/base_search_custom_field_filter/demo/demo_ir_ui_custom_field_filter.xml @@ -0,0 +1,9 @@ + + + + + Language + lang + + + diff --git a/base_search_custom_field_filter/models/__init__.py b/base_search_custom_field_filter/models/__init__.py new file mode 100644 index 0000000..99769e9 --- /dev/null +++ b/base_search_custom_field_filter/models/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from . import base +from . import ir_ui_custom_field_filter diff --git a/base_search_custom_field_filter/models/base.py b/base_search_custom_field_filter/models/base.py new file mode 100644 index 0000000..24737eb --- /dev/null +++ b/base_search_custom_field_filter/models/base.py @@ -0,0 +1,64 @@ +# Copyright 2020-2021 Tecnativa - Carlos Dauden +# Copyright 2020 Tecnativa - Pedro M. Baeza +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from odoo import api, models +from lxml import etree + + +class Base(models.AbstractModel): + _inherit = 'base' + + @api.model + def _add_custom_filters(self, res, custom_filters): + arch = etree.fromstring(res['arch']) + for custom_filter in custom_filters: + node = False + if custom_filter.position_after: + node = arch.xpath( + "//field[@name='%s']" % custom_filter.position_after) + if not node: + node = (arch.xpath("//field[last()]") or + arch.xpath("//filter[last()]") or + arch.xpath("//group[last()]")) + if node: + elem = etree.Element('field', { + 'name': 'ir_ui_custom_filter_%s' % custom_filter.id, + 'string': custom_filter.name, + 'custom_field_filter': custom_filter.expression, + }) + 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): + """Inject fields field in search views.""" + res = super().fields_view_get( + view_id=view_id, view_type=view_type, toolbar=toolbar, + submenu=submenu) + if view_type == 'search': + custom_filters = self.env['ir.ui.custom.field.filter'].search( + [("model_name", "=", res.get("model"))]) + if custom_filters: + res = self._add_custom_filters(res, custom_filters) + return res + + @api.model + def load_views(self, views, options=None): + """Inject fake field definition for having custom filters available.""" + res = super(Base, self.with_context( + custom_field_filter=True, + )).load_views(views, options=options) + custom_filters = self.env['ir.ui.custom.field.filter'].search( + [("model_name", "=", self._name)]) + for custom_filter in custom_filters: + field = custom_filter._get_related_field() + field_name = 'ir_ui_custom_filter_%s' % custom_filter.id + res['fields'][field_name] = field.get_description(self.env) + # force this for avoiding to appear on the rest of the UI + res['fields'][field_name]['selectable'] = False + res['fields'][field_name]['sortable'] = False + res['fields'][field_name]['store'] = False + return res diff --git a/base_search_custom_field_filter/models/ir_ui_custom_field_filter.py b/base_search_custom_field_filter/models/ir_ui_custom_field_filter.py new file mode 100644 index 0000000..e6172c1 --- /dev/null +++ b/base_search_custom_field_filter/models/ir_ui_custom_field_filter.py @@ -0,0 +1,42 @@ +# Copyright 2020 Tecnativa - Carlos Dauden +# Copyright 2020 Tecnativa - Pedro M. Baeza +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from odoo import _, api, exceptions, fields, models + + +class IrUiCustomFilter(models.Model): + _name = "ir.ui.custom.field.filter" + _description = "Custom UI field filter" + _order = "model_id, sequence, id" + + sequence = fields.Integer() + model_id = fields.Many2one(comodel_name="ir.model", required=True) + model_name = fields.Char( + related="model_id.model", store=True, readonly=True, index=True, + string="Model name", + ) + name = fields.Char(required=True, translate=True) + expression = fields.Char(required=True) + position_after = fields.Char( + help="Optional field name for putting the filter after that one. " + "If empty or not found, it will be put at the end.", + ) + + def _get_related_field(self): + """Determine the chain of fields.""" + self.ensure_one() + related = self.expression.split('.') + target = self.env[self.model_name] + for name in related: + field = target._fields[name] + target = target[name] + return field + + @api.constrains('model_id', 'expression') + def _check_expression(self): + for record in self: + try: + record._get_related_field() + except KeyError: + raise exceptions.ValidationError(_("Incorrect expression.")) diff --git a/base_search_custom_field_filter/readme/CONFIGURE.rst b/base_search_custom_field_filter/readme/CONFIGURE.rst new file mode 100644 index 0000000..c6867c4 --- /dev/null +++ b/base_search_custom_field_filter/readme/CONFIGURE.rst @@ -0,0 +1,14 @@ +#. Go to *Settings > Technical > User Interface > Custom Field Filters*. +#. Create a new record, and define following information: + + * The **Model** for which you are defining the filter. It will appear in all + the search views of this model. + * The label you want to see on the search line on the **Name** field. This + field allows translations for proper UI in different languages. + * The **Expression**, which is the field chain string with dot notation. + Examples: `product_id`, `product_id.seller_ids.name`, `partner_id.lang`. + * Optionally, you can fill **Position After** for indicating after which + existing field (technical name) the filter will appear. If empty or not + found, the filter will be added at the end. +#. You can reorder records for determining sorting for multiple filters for the + same model with the arrow handle in the left part. diff --git a/base_search_custom_field_filter/readme/CONTRIBUTORS.rst b/base_search_custom_field_filter/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000..e88bd67 --- /dev/null +++ b/base_search_custom_field_filter/readme/CONTRIBUTORS.rst @@ -0,0 +1,4 @@ +* `Tecnativa `_: + + * Carlos Dauden + * Pedro M. Baeza diff --git a/base_search_custom_field_filter/readme/DESCRIPTION.rst b/base_search_custom_field_filter/readme/DESCRIPTION.rst new file mode 100644 index 0000000..5763749 --- /dev/null +++ b/base_search_custom_field_filter/readme/DESCRIPTION.rst @@ -0,0 +1,5 @@ +This module allows to define custom filters in the search views for an specific +field belonging to the document or any other related document. + +This nature makes the definition quite technical, but once done, it adds the +element in the UI for regular user use. diff --git a/base_search_custom_field_filter/readme/USAGE.rst b/base_search_custom_field_filter/readme/USAGE.rst new file mode 100644 index 0000000..37ef9b9 --- /dev/null +++ b/base_search_custom_field_filter/readme/USAGE.rst @@ -0,0 +1,15 @@ +#. Go to the menu entry for which you have defined the custom field filter. +#. On the search bar, type anything. +#. In the filter list, you will see the line for the element you have defined. + +As demo data, a custom field filter is included for sample purposes: + +.. image:: ../static/src/img/ir_ui_custom_field_filter.png + +Steps for trying this sample: + +#. Install `contacts` module. +#. Go to *Contacts*. +#. Type "english" and you'll find the filter "Language" at the end: + +.. image:: ../static/src/img/contact_search.png diff --git a/base_search_custom_field_filter/security/ir.model.access.csv b/base_search_custom_field_filter/security/ir.model.access.csv new file mode 100644 index 0000000..ba1e3b5 --- /dev/null +++ b/base_search_custom_field_filter/security/ir.model.access.csv @@ -0,0 +1,3 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_ir_ui_custom_field_filter_user,ir.ui.custom.field.filter,model_ir_ui_custom_field_filter,base.group_user,1,0,0,0 +access_ir_ui_custom_field_filter_system,ir.ui.custom.field.filter,model_ir_ui_custom_field_filter,base.group_system,1,1,1,1 diff --git a/base_search_custom_field_filter/static/description/icon.png b/base_search_custom_field_filter/static/description/icon.png new file mode 100644 index 0000000..3a0328b Binary files /dev/null and b/base_search_custom_field_filter/static/description/icon.png differ diff --git a/base_search_custom_field_filter/static/description/index.html b/base_search_custom_field_filter/static/description/index.html new file mode 100644 index 0000000..fe5fcbb --- /dev/null +++ b/base_search_custom_field_filter/static/description/index.html @@ -0,0 +1,467 @@ + + + + + + +Add custom filters for fields via UI + + + +
+

Add custom filters for fields via UI

+ + +

Beta License: AGPL-3 OCA/server-ux Translate me on Weblate Try me on Runbot

+

This module allows to define custom filters in the search views for an specific +field belonging to the document or any other related document.

+

This nature makes the definition quite technical, but once done, it adds the +element in the UI for regular user use.

+

Table of contents

+ +
+

Configuration

+
    +
  1. Go to Settings > Technical > User Interface > Custom Field Filters.
  2. +
  3. Create a new record, and define following information:
      +
    • The Model for which you are defining the filter. It will appear in all +the search views of this model.
    • +
    • The label you want to see on the search line on the Name field. This +field allows translations for proper UI in different languages.
    • +
    • The Expression, which is the field chain string with dot notation. +Examples: product_id, product_id.seller_ids.name, partner_id.lang.
    • +
    • Optionally, you can fill Position After for indicating after which +existing field (technical name) the filter will appear. If empty or not +found, the filter will be added at the end.
    • +
    +
  4. +
  5. You can reorder records for determining sorting for multiple filters for the +same model with the arrow handle in the left part.
  6. +
+
+
+

Usage

+
    +
  1. Go to the menu entry for which you have defined the custom field filter.
  2. +
  3. On the search bar, type anything.
  4. +
  5. In the filter list, you will see the line for the element you have defined.
  6. +
+

As demo data, a custom field filter is included for sample purposes:

+https://raw.githubusercontent.com/OCA/server-ux/12.0/base_search_custom_field_filter/static/src/img/ir_ui_custom_field_filter.png +

Steps for trying this sample:

+
    +
  1. Install contacts module.
  2. +
  3. Go to Contacts.
  4. +
  5. Type “english” and you’ll find the filter “Language” at the end:
  6. +
+https://raw.githubusercontent.com/OCA/server-ux/12.0/base_search_custom_field_filter/static/src/img/contact_search.png +
+
+

Bug Tracker

+

Bugs are tracked on GitHub 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.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Tecnativa
  • +
+
+
+

Contributors

+
    +
  • Tecnativa:
      +
    • Carlos Dauden
    • +
    • Pedro M. Baeza
    • +
    +
  • +
+
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

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.

+

Current maintainer:

+

pedrobaeza

+

This module is part of the OCA/server-ux project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/base_search_custom_field_filter/static/src/img/contact_search.png b/base_search_custom_field_filter/static/src/img/contact_search.png new file mode 100644 index 0000000..ba421d4 Binary files /dev/null and b/base_search_custom_field_filter/static/src/img/contact_search.png differ diff --git a/base_search_custom_field_filter/static/src/img/ir_ui_custom_field_filter.png b/base_search_custom_field_filter/static/src/img/ir_ui_custom_field_filter.png new file mode 100644 index 0000000..c4dee48 Binary files /dev/null and b/base_search_custom_field_filter/static/src/img/ir_ui_custom_field_filter.png differ diff --git a/base_search_custom_field_filter/static/src/js/search_input.js b/base_search_custom_field_filter/static/src/js/search_input.js new file mode 100644 index 0000000..9619cf2 --- /dev/null +++ b/base_search_custom_field_filter/static/src/js/search_input.js @@ -0,0 +1,22 @@ +odoo.define('base_search_custom_field_filter.search_inputs_related_field', function (require) { + "use strict"; + + var core = require('web.core'); + var search_inputs = require('web.search_inputs'); + var ManyToOneField = core.search_widgets_registry.map.many2one; + + search_inputs.Field.include({ + make_domain: function (name, operator, facetValue) { + var name_n = this.attrs.custom_field_filter || name; + return this._super(name_n, operator, facetValue); + }, + }); + + ManyToOneField.include({ + make_domain: function (name, operator, facetValue) { + var name_n = this.attrs.custom_field_filter || name; + return this._super(name_n, operator, facetValue); + }, + }); + +}); diff --git a/base_search_custom_field_filter/templates/assets.xml b/base_search_custom_field_filter/templates/assets.xml new file mode 100644 index 0000000..00f9747 --- /dev/null +++ b/base_search_custom_field_filter/templates/assets.xml @@ -0,0 +1,8 @@ + + + + diff --git a/base_search_custom_field_filter/views/ir_ui_custom_field_filter_views.xml b/base_search_custom_field_filter/views/ir_ui_custom_field_filter_views.xml new file mode 100644 index 0000000..65c89db --- /dev/null +++ b/base_search_custom_field_filter/views/ir_ui_custom_field_filter_views.xml @@ -0,0 +1,32 @@ + + + + + + ir.ui.custom.field.filter + + + + + + + + + + + + + + + +