diff --git a/base_sequence_default/README.rst b/base_sequence_default/README.rst new file mode 100644 index 000000000..9fcc85a3c --- /dev/null +++ b/base_sequence_default/README.rst @@ -0,0 +1,130 @@ +============================ +Default Fields with Sequence +============================ + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:c7159d99fc7227ae6aba6066894c512a23b5beeece75077d5779b7c41467a52e + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Alpha-red.png + :target: https://odoo-community.org/page/development-status + :alt: Alpha +.. |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--tools-lightgray.png?logo=github + :target: https://github.com/OCA/server-tools/tree/16.0/base_sequence_default + :alt: OCA/server-tools +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/server-tools-16-0/server-tools-16-0-base_sequence_default + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/server-tools&target_branch=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module extends the defaults functionality to allow filling +*Character* type fields on creation with a predefined sequence. + +.. IMPORTANT:: + This is an alpha version, the data model and design can change at any time without warning. + Only for development or testing purpose, do not use in production. + `More details on development status `_ + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +To configure this module, you need to: + +1. Enable developer mode. +2. Go to the form view of the model to which you want to add the new + sequential default value. +3. Hover over the field to which you want to add the sequential default + value. A tooltip with more info will appear. +4. Make sure the tooltip says *Type: char*. Only those fields will work. +5. Take note of the *Object* and *Field*. +6. Go to *Settings > Technical > Sequences & Identifiers > Sequences*. +7. Create one sequence with code named after this pattern: + ``base_sequence_default.{object}.fields.{field}``. E.g.: + ``base_sequence_default.res.partner.fields.name`` to add a default + sequenced name for new partners. +8. Configure the sequence at will. + +Usage +===== + +To use this module, it is necessary to: + +1. Follow the configuration instructions. +2. Create a record of the model you configured. +3. Leave empty the fields where you configured a sequence. If they are + required fields, set value to "-" to trigger the automatic sequence + on creation. +4. Check that the field defined in the sequence is set to the next value + in the sequence. + +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 to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* Moduon + +Contributors +------------ + +- Rafael Blasco (`Moduon `__) +- Eduardo de Miguel (`Moduon `__) +- Jairo Llopis (`Moduon `__) + +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-Shide| image:: https://github.com/Shide.png?size=40px + :target: https://github.com/Shide + :alt: Shide +.. |maintainer-yajo| image:: https://github.com/yajo.png?size=40px + :target: https://github.com/yajo + :alt: yajo +.. |maintainer-rafaelbn| image:: https://github.com/rafaelbn.png?size=40px + :target: https://github.com/rafaelbn + :alt: rafaelbn + +Current `maintainers `__: + +|maintainer-Shide| |maintainer-yajo| |maintainer-rafaelbn| + +This module is part of the `OCA/server-tools `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/base_sequence_default/__init__.py b/base_sequence_default/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/base_sequence_default/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/base_sequence_default/__manifest__.py b/base_sequence_default/__manifest__.py new file mode 100644 index 000000000..a0fee4091 --- /dev/null +++ b/base_sequence_default/__manifest__.py @@ -0,0 +1,22 @@ +# Copyright 2023 Moduon Team S.L. +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) + +{ + "name": "Default Fields with Sequence", + "summary": "Use sequences for default values of fields when creating a new record", + "version": "16.0.1.0.0", + "development_status": "Alpha", + "category": "Tools", + "website": "https://github.com/OCA/server-tools", + "author": "Moduon, Odoo Community Association (OCA)", + "maintainers": ["Shide", "yajo", "rafaelbn"], + "license": "AGPL-3", + "application": False, + "installable": True, + "depends": [ + "base", + ], + "demo": [ + "demo/ir_sequence_demo.xml", + ], +} diff --git a/base_sequence_default/demo/ir_sequence_demo.xml b/base_sequence_default/demo/ir_sequence_demo.xml new file mode 100644 index 000000000..aef8fa700 --- /dev/null +++ b/base_sequence_default/demo/ir_sequence_demo.xml @@ -0,0 +1,13 @@ + + + + + Partners default: Ref + base_sequence_default.res.partner.fields.ref + AUTOREF/ + 3 + + standard + + diff --git a/base_sequence_default/i18n/base_sequence_default.pot b/base_sequence_default/i18n/base_sequence_default.pot new file mode 100644 index 000000000..a1f970dfc --- /dev/null +++ b/base_sequence_default/i18n/base_sequence_default.pot @@ -0,0 +1,19 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * base_sequence_default +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 14.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: base_sequence_default +#: model:ir.model,name:base_sequence_default.model_base +msgid "Base" +msgstr "" diff --git a/base_sequence_default/models/__init__.py b/base_sequence_default/models/__init__.py new file mode 100644 index 000000000..0e4444933 --- /dev/null +++ b/base_sequence_default/models/__init__.py @@ -0,0 +1 @@ +from . import base diff --git a/base_sequence_default/models/base.py b/base_sequence_default/models/base.py new file mode 100644 index 000000000..b585dc8eb --- /dev/null +++ b/base_sequence_default/models/base.py @@ -0,0 +1,45 @@ +# Copyright 2023 Moduon Team S.L. +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) + +import logging + +from odoo import api, models + +SEQUENCE_PREFIX = "base_sequence_default" +_logger = logging.getLogger(__name__) + + +class Base(models.AbstractModel): + _inherit = "base" + + @api.model_create_multi + def create(self, vals_list): + """Produce sequenced defaults if DB is configured for that.""" + result = super().create(vals_list) + # Get sequences for fields in this model + prefix = f"{SEQUENCE_PREFIX}.{self._name}.fields." + model_sequences = ( + self.env["ir.sequence"] + .sudo() + .search( + [ + ("code", "=like", f"{prefix}%"), + ("company_id", "in", [self.env.company.id, False]), + ], + order="company_id", + ) + ) + for record in result: + for seq in model_sequences: + fname = seq.code[len(prefix) :] + if fname not in record._fields: + _logger.warning( + "Ignoring sequence %s; missing field %s in model %s", + seq.code, + fname, + self._name, + ) + continue + if record[fname] in {False, "-"}: + record[fname] = seq.next_by_id() + return result diff --git a/base_sequence_default/readme/CONFIGURE.md b/base_sequence_default/readme/CONFIGURE.md new file mode 100644 index 000000000..185244586 --- /dev/null +++ b/base_sequence_default/readme/CONFIGURE.md @@ -0,0 +1,17 @@ +To configure this module, you need to: + +1. Enable developer mode. +2. Go to the form view of the model to which you want to add the new + sequential default value. +3. Hover over the field to which you want to add the sequential default + value. A tooltip with more info will appear. +4. Make sure the tooltip says *Type: char*. Only those fields will + work. +5. Take note of the *Object* and *Field*. +6. Go to *Settings \> Technical \> Sequences & Identifiers \> + Sequences*. +7. Create one sequence with code named after this pattern: + `base_sequence_default.{object}.fields.{field}`. E.g.: + `base_sequence_default.res.partner.fields.name` to add a default + sequenced name for new partners. +8. Configure the sequence at will. diff --git a/base_sequence_default/readme/CONTRIBUTORS.md b/base_sequence_default/readme/CONTRIBUTORS.md new file mode 100644 index 000000000..b82adb987 --- /dev/null +++ b/base_sequence_default/readme/CONTRIBUTORS.md @@ -0,0 +1,3 @@ +- Rafael Blasco ([Moduon](https://www.moduon.team/)) +- Eduardo de Miguel ([Moduon](https://www.moduon.team/)) +- Jairo Llopis ([Moduon](https://www.moduon.team/)) diff --git a/base_sequence_default/readme/DESCRIPTION.md b/base_sequence_default/readme/DESCRIPTION.md new file mode 100644 index 000000000..1ebf38259 --- /dev/null +++ b/base_sequence_default/readme/DESCRIPTION.md @@ -0,0 +1,2 @@ +This module extends the defaults functionality to allow filling +*Character* type fields on creation with a predefined sequence. diff --git a/base_sequence_default/readme/USAGE.md b/base_sequence_default/readme/USAGE.md new file mode 100644 index 000000000..c349a6ab0 --- /dev/null +++ b/base_sequence_default/readme/USAGE.md @@ -0,0 +1,9 @@ +To use this module, it is necessary to: + +1. Follow the configuration instructions. +2. Create a record of the model you configured. +3. Leave empty the fields where you configured a sequence. If they are + required fields, set value to "-" to trigger the automatic sequence + on creation. +4. Check that the field defined in the sequence is set to the next + value in the sequence. diff --git a/base_sequence_default/static/description/icon.png b/base_sequence_default/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/base_sequence_default/static/description/icon.png differ diff --git a/base_sequence_default/static/description/index.html b/base_sequence_default/static/description/index.html new file mode 100644 index 000000000..0713ae57d --- /dev/null +++ b/base_sequence_default/static/description/index.html @@ -0,0 +1,466 @@ + + + + + + +Default Fields with Sequence + + + +
+

Default Fields with Sequence

+ + +

Alpha License: AGPL-3 OCA/server-tools Translate me on Weblate Try me on Runboat

+

This module extends the defaults functionality to allow filling +Character type fields on creation with a predefined sequence.

+
+

Important

+

This is an alpha version, the data model and design can change at any time without warning. +Only for development or testing purpose, do not use in production. +More details on development status

+
+

Table of contents

+ +
+

Configuration

+

To configure this module, you need to:

+
    +
  1. Enable developer mode.
  2. +
  3. Go to the form view of the model to which you want to add the new +sequential default value.
  4. +
  5. Hover over the field to which you want to add the sequential default +value. A tooltip with more info will appear.
  6. +
  7. Make sure the tooltip says Type: char. Only those fields will work.
  8. +
  9. Take note of the Object and Field.
  10. +
  11. Go to Settings > Technical > Sequences & Identifiers > Sequences.
  12. +
  13. Create one sequence with code named after this pattern: +base_sequence_default.{object}.fields.{field}. E.g.: +base_sequence_default.res.partner.fields.name to add a default +sequenced name for new partners.
  14. +
  15. Configure the sequence at will.
  16. +
+
+
+

Usage

+

To use this module, it is necessary to:

+
    +
  1. Follow the configuration instructions.
  2. +
  3. Create a record of the model you configured.
  4. +
  5. Leave empty the fields where you configured a sequence. If they are +required fields, set value to “-” to trigger the automatic sequence +on creation.
  6. +
  7. Check that the field defined in the sequence is set to the next value +in the sequence.
  8. +
+
+
+

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 to smash it by providing a detailed and welcomed +feedback.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Moduon
  • +
+
+
+

Contributors

+ +
+
+

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 maintainers:

+

Shide yajo rafaelbn

+

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

+

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

+
+
+
+ + diff --git a/base_sequence_default/tests/__init__.py b/base_sequence_default/tests/__init__.py new file mode 100644 index 000000000..14a0b196d --- /dev/null +++ b/base_sequence_default/tests/__init__.py @@ -0,0 +1 @@ +from . import test_project_project diff --git a/base_sequence_default/tests/test_project_project.py b/base_sequence_default/tests/test_project_project.py new file mode 100644 index 000000000..9aa3d100f --- /dev/null +++ b/base_sequence_default/tests/test_project_project.py @@ -0,0 +1,43 @@ +# Copyright 2023 Moduon Team S.L. +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) + + +from odoo.tests.common import Form, TransactionCase + +from ..models.base import SEQUENCE_PREFIX + + +class BaseSequenceDefaultCase(TransactionCase): + def setUp(self): + super().setUp() + is_model = self.env["ir.sequence"] + self.partner_seqs = is_model.create( + [ + { + "name": "Partner name", + "code": f"{SEQUENCE_PREFIX}.res.partner.fields.name", + "implementation": "standard", + "prefix": "PN/", + "padding": 3, + "number_increment": 1, + }, + { + "name": "Partner mobile... let's spam all Spaniards", + "code": f"{SEQUENCE_PREFIX}.res.partner.fields.mobile", + "implementation": "standard", + "prefix": "+34 ", + "padding": 9, + "number_increment": 1, + }, + ] + ) + + def test_partner_default_field(self): + """Test that new created partner has the correct default field values.""" + partner_f = Form(self.env["res.partner"]) + self.assertEqual(partner_f.name, False) + self.assertEqual(partner_f.mobile, False) + partner_f.name = "-" + partner = partner_f.save() + self.assertEqual(partner.name, "PN/001") + self.assertEqual(partner.mobile, "+34 000000001") diff --git a/setup/base_sequence_default/odoo/addons/base_sequence_default b/setup/base_sequence_default/odoo/addons/base_sequence_default new file mode 120000 index 000000000..2766299aa --- /dev/null +++ b/setup/base_sequence_default/odoo/addons/base_sequence_default @@ -0,0 +1 @@ +../../../../base_sequence_default \ No newline at end of file diff --git a/setup/base_sequence_default/setup.py b/setup/base_sequence_default/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/base_sequence_default/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)