|
| 1 | +=========================== |
| 2 | +Data Autocomplete Templates |
| 3 | +=========================== |
| 4 | + |
| 5 | +.. |
| 6 | + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 7 | + !! This file is generated by oca-gen-addon-readme !! |
| 8 | + !! changes will be overwritten. !! |
| 9 | + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 10 | + !! source digest: sha256:36224729669f87589f45a941303e973d1a2404ebb481dda00f67b66dc568aad6 |
| 11 | + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 12 | +
|
| 13 | +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png |
| 14 | + :target: https://odoo-community.org/page/development-status |
| 15 | + :alt: Beta |
| 16 | +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png |
| 17 | + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html |
| 18 | + :alt: License: AGPL-3 |
| 19 | +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github |
| 20 | + :target: https://github.com/OCA/server-tools/tree/17.0/base_filter_template |
| 21 | + :alt: OCA/server-tools |
| 22 | +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png |
| 23 | + :target: https://translation.odoo-community.org/projects/server-tools-17-0/server-tools-17-0-base_filter_template |
| 24 | + :alt: Translate me on Weblate |
| 25 | +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png |
| 26 | + :target: https://runboat.odoo-community.org/builds?repo=OCA/server-tools&target_branch=17.0 |
| 27 | + :alt: Try me on Runboat |
| 28 | + |
| 29 | +|badge1| |badge2| |badge3| |badge4| |badge5| |
| 30 | + |
| 31 | +This module provides a **generic**, reusable mechanism to **save** and |
| 32 | +**apply** named templates that autocomplete any Odoo wizard/form using a |
| 33 | +**JSON payload** stored in a template record. |
| 34 | + |
| 35 | +It is designed to be **framework-like**: other modules can inherit the |
| 36 | +mixin and add a few buttons in XML. |
| 37 | + |
| 38 | +-------------- |
| 39 | + |
| 40 | +What you get |
| 41 | +------------ |
| 42 | + |
| 43 | +1) Template model |
| 44 | +~~~~~~~~~~~~~~~~~ |
| 45 | + |
| 46 | +``data.autocomplete.template`` |
| 47 | + |
| 48 | +Each template stores: |
| 49 | + |
| 50 | +- ``name``: template name |
| 51 | +- ``model_id``: the target Odoo model (``ir.model``) the template |
| 52 | + applies to |
| 53 | +- ``company_id``: company scoping (global or per company) |
| 54 | +- ``user_id``: optional owner (personal templates) |
| 55 | +- ``values_json``: a JSON dictionary holding serialized default values |
| 56 | + |
| 57 | +2) Mixin |
| 58 | +~~~~~~~~ |
| 59 | + |
| 60 | +``data.template.mixin`` |
| 61 | + |
| 62 | +Add it to any wizard/model to get: |
| 63 | + |
| 64 | +- ``template_id`` field (select a template) |
| 65 | +- actions (for buttons): |
| 66 | + |
| 67 | + - ``action_open_create_template_wizard()`` |
| 68 | + - ``action_update_current_template()`` |
| 69 | + - ``action_apply_template(overwrite=True)`` |
| 70 | + |
| 71 | +3) Create Template Wizard |
| 72 | +~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 73 | + |
| 74 | +``data.wizard.template.create`` |
| 75 | + |
| 76 | +This popup asks for a **name** (and personal/global scope) and creates |
| 77 | +the template using the current wizard/model values. |
| 78 | + |
| 79 | +JSON serialization |
| 80 | +~~~~~~~~~~~~~~~~~~ |
| 81 | + |
| 82 | +The mixin serializes current values into a JSON-safe dict: |
| 83 | + |
| 84 | +- ``many2one`` -> the record ``id`` (or ``false``) |
| 85 | +- ``many2many`` -> a list of record ids |
| 86 | +- basic fields |
| 87 | + (char/int/float/bool/date/datetime/selection/text/monetary) -> stored |
| 88 | + as-is |
| 89 | +- ``one2many`` is ignored |
| 90 | +- computed fields without inverse and readonly fields are ignored (by |
| 91 | + default) |
| 92 | + |
| 93 | +Applying templates safely with onchanges |
| 94 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 95 | + |
| 96 | +Applying a template uses a **phased write**: |
| 97 | + |
| 98 | +1. **Drivers**: fields returned by ``_template_driver_fields()`` are |
| 99 | + written first |
| 100 | +2. **Rest**: everything else (excluding protected fields) |
| 101 | +3. **Protected**: fields returned by ``_template_protected_fields()`` |
| 102 | + are written last |
| 103 | + |
| 104 | +This solves common wizard issues where changing a driver field triggers |
| 105 | +an onchange that clears other fields (e.g. changing ``plan_id`` clears |
| 106 | +``account_ids``). |
| 107 | + |
| 108 | +While applying, ``apply_template=True`` is present in the context so you |
| 109 | +can skip destructive onchanges in your own code. |
| 110 | + |
| 111 | +**Table of contents** |
| 112 | + |
| 113 | +.. contents:: |
| 114 | + :local: |
| 115 | + |
| 116 | +Usage |
| 117 | +===== |
| 118 | + |
| 119 | +1) Inherit the mixin in Python |
| 120 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 121 | + |
| 122 | +.. code:: python |
| 123 | +
|
| 124 | + from odoo import models, api |
| 125 | +
|
| 126 | + class MyWizard(models.TransientModel): |
| 127 | + _inherit = ["my.wizard.model", "data.template.mixin"] |
| 128 | +
|
| 129 | + # Optional: define drivers/protected fields to survive onchanges |
| 130 | + def _template_driver_fields(self): |
| 131 | + return {"plan_id"} |
| 132 | +
|
| 133 | + def _template_protected_fields(self): |
| 134 | + return {"account_ids", "show_months"} |
| 135 | +
|
| 136 | + @api.onchange("plan_id") |
| 137 | + def _onchange_plan_id(self): |
| 138 | + # Skip destructive behavior when applying templates |
| 139 | + if self.env.context.get("apply_template"): |
| 140 | + return |
| 141 | + return super()._onchange_plan_id() |
| 142 | +
|
| 143 | +.. |
| 144 | +
|
| 145 | + Note: This module only provides the base. |
| 146 | + |
| 147 | +2) Add UI in XML (template selector + buttons) |
| 148 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 149 | + |
| 150 | +Insert this into your wizard form view (adapt ``inherit_id`` and the |
| 151 | +``xpath``): |
| 152 | + |
| 153 | +.. code:: xml |
| 154 | +
|
| 155 | + <separator string="Templates"/> |
| 156 | + <group col="4"> |
| 157 | + <field name="template_id" options="{'no_create': True}"/> |
| 158 | +
|
| 159 | + <button name="action_apply_template" |
| 160 | + type="object" |
| 161 | + string="Apply" |
| 162 | + class="btn btn-secondary" |
| 163 | + icon="fa-check" |
| 164 | + invisible="not template_id"/> |
| 165 | +
|
| 166 | + <button name="action_open_create_template_wizard" |
| 167 | + type="object" |
| 168 | + string="Save as template" |
| 169 | + class="btn btn-secondary" |
| 170 | + icon="fa-save"/> |
| 171 | +
|
| 172 | + <button name="action_update_current_template" |
| 173 | + type="object" |
| 174 | + string="Update template" |
| 175 | + class="btn btn-secondary" |
| 176 | + icon="fa-refresh" |
| 177 | + invisible="not template_id"/> |
| 178 | + </group> |
| 179 | +
|
| 180 | +3) Filtering templates further (optional) |
| 181 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 182 | + |
| 183 | +If the same wizard model can represent different "types" of reports |
| 184 | +(e.g. different ``report_id``), you can further restrict the templates |
| 185 | +shown by overriding: |
| 186 | + |
| 187 | +.. code:: python |
| 188 | +
|
| 189 | + def _template_domain_extra(self): |
| 190 | + return [("some_field", "=", self.some_field.id)] |
| 191 | +
|
| 192 | +-------------- |
| 193 | + |
| 194 | +Notes / Tips |
| 195 | +~~~~~~~~~~~~ |
| 196 | + |
| 197 | +- If you want templates shared across companies, allow |
| 198 | + ``company_id = False`` templates (already supported by the domain). |
| 199 | +- If you want only personal templates, set ``user_id`` on every |
| 200 | + template and adjust the domain. |
| 201 | +- If some fields should never be stored (tokens, volatile flags, etc.), |
| 202 | + override ``_template_skip_fields()``. |
| 203 | + |
| 204 | +-------------- |
| 205 | + |
| 206 | +Bug Tracker |
| 207 | +=========== |
| 208 | + |
| 209 | +Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-tools/issues>`_. |
| 210 | +In case of trouble, please check there if your issue has already been reported. |
| 211 | +If you spotted it first, help us to smash it by providing a detailed and welcomed |
| 212 | +`feedback <https://github.com/OCA/server-tools/issues/new?body=module:%20base_filter_template%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_. |
| 213 | + |
| 214 | +Do not contact contributors directly about support or help with technical issues. |
| 215 | + |
| 216 | +Credits |
| 217 | +======= |
| 218 | + |
| 219 | +Authors |
| 220 | +------- |
| 221 | + |
| 222 | +* APSL/Nagarro |
| 223 | + |
| 224 | +Contributors |
| 225 | +------------ |
| 226 | + |
| 227 | +- `APSL-Nagarro <https://apsl.tech>`__: |
| 228 | + |
| 229 | + - Bernat Obrador <bobrador@apsl.net> |
| 230 | + |
| 231 | +Maintainers |
| 232 | +----------- |
| 233 | + |
| 234 | +This module is maintained by the OCA. |
| 235 | + |
| 236 | +.. image:: https://odoo-community.org/logo.png |
| 237 | + :alt: Odoo Community Association |
| 238 | + :target: https://odoo-community.org |
| 239 | + |
| 240 | +OCA, or the Odoo Community Association, is a nonprofit organization whose |
| 241 | +mission is to support the collaborative development of Odoo features and |
| 242 | +promote its widespread use. |
| 243 | + |
| 244 | +.. |maintainer-BernatObrador| image:: https://github.com/BernatObrador.png?size=40px |
| 245 | + :target: https://github.com/BernatObrador |
| 246 | + :alt: BernatObrador |
| 247 | + |
| 248 | +Current `maintainer <https://odoo-community.org/page/maintainer-role>`__: |
| 249 | + |
| 250 | +|maintainer-BernatObrador| |
| 251 | + |
| 252 | +This module is part of the `OCA/server-tools <https://github.com/OCA/server-tools/tree/17.0/base_filter_template>`_ project on GitHub. |
| 253 | + |
| 254 | +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. |
0 commit comments