[ADD] base_sequence_default: add default values with sequences

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

@moduon MT-1506

Co-authored-by: Eduardo de Miguel <1162050+Shide@users.noreply.github.com>
pull/2765/head
Jairo Llopis 2023-03-28 11:00:07 +01:00 committed by Jairo Llopis
parent 97be5cf17c
commit 4020460798
No known key found for this signature in database
GPG Key ID: E47E3BE44B940490
13 changed files with 172 additions and 0 deletions

View File

@ -0,0 +1,35 @@
**This file is going to be generated by oca-gen-addon-readme.**
*Manual changes will be overwritten.*
Please provide content in the ``readme`` directory:
* **DESCRIPTION.rst** (required)
* INSTALL.rst (optional)
* CONFIGURE.rst (optional)
* **USAGE.rst** (optional, highly recommended)
* DEVELOP.rst (optional)
* ROADMAP.rst (optional)
* HISTORY.rst (optional, recommended)
* **CONTRIBUTORS.rst** (optional, highly recommended)
* CREDITS.rst (optional)
Content of this README will also be drawn from the addon manifest,
from keys such as name, authors, maintainers, development_status,
and license.
A good, one sentence summary in the manifest is also highly recommended.
Automatic changelog generation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
`HISTORY.rst` can be auto generated using `towncrier <https://pypi.org/project/towncrier>`_.
Just put towncrier compatible changelog fragments into `readme/newsfragments`
and the changelog file will be automatically generated and updated when a new fragment is added.
Please refer to `towncrier` documentation to know more.
NOTE: the changelog will be automatically generated when using `/ocabot merge $option`.
If you need to run it manually, refer to `OCA/maintainer-tools README <https://github.com/OCA/maintainer-tools>`_.

View File

@ -0,0 +1 @@
from . import models

View File

@ -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": "14.0.0.1.0",
"development_status": "Alpha",
"category": "Tools",
"website": "https://github.com/OCA/server-tools",
"author": "Moduon, Odoo Community Association (OCA)",
"maintainers": ["Shide"],
"license": "AGPL-3",
"application": False,
"installable": True,
"depends": [
"base",
],
"demo": [
"demo/ir_sequence_demo.xml",
],
}

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2023 Moduon Team S.L.
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0) -->
<data noupdate="1">
<record id="res_partner_fields_name" model="ir.sequence">
<field name="name">Partners default: Ref</field>
<field name="code">base_sequence_default.res.partner.fields.ref</field>
<field name="prefix">AUTOREF/</field>
<field name="padding">3</field>
<field name="company_id" eval="False" />
<field name="implementation">standard</field>
</record>
</data>

View File

@ -0,0 +1 @@
from . import base

View File

@ -0,0 +1,37 @@
# Copyright 2023 Moduon Team S.L.
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3.0)
from odoo import api, models
SEQUENCE_PREFIX = "base_sequence_default"
class Base(models.AbstractModel):
_inherit = "base"
@api.model
def default_get(self, fields_list):
"""Produce sequenced defaults if DB is configured for that."""
res = super().default_get(fields_list)
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",
)
)
sequences_by_field = {seq.code[len(prefix) :]: seq for seq in model_sequences}
for fname in fields_list:
try:
seq = sequences_by_field[fname]
except KeyError:
# No sequence? No problem
continue
res[fname] = seq.next_by_id()
return res

View File

@ -0,0 +1,13 @@
To configure this module, you need to:
#. Enable developer mode.
#. Go to the form view of the model to which you want to add the new sequential default value.
#. Hover over the field to which you want to add the sequential default value. A tooltip with more info will appear.
#. Make sure the tooltip says *Type: char*. Only those fields will work.
#. Take note of the *Object* and *Field*.
#. Go to *Settings > Technical > Sequences & Identifiers > Sequences*.
#. 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.
#. Configure the sequence at will.

View File

@ -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/>`__)

View File

@ -0,0 +1,2 @@
This module extends the defaults functionality to allow filling *Character*
type fields on creation with a predefined sequence.

View File

@ -0,0 +1,5 @@
To use this module, it is necessary to:
#. Follow the configuration instructions.
#. Create a record of the model you configured.
#. Check that the field defined in the sequence is set to the next value in the sequence.

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.2 KiB

View File

@ -0,0 +1 @@
from . import test_project_project

View File

@ -0,0 +1,39 @@
# 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."""
with Form(self.env["res.partner"]) as pp_form:
self.assertEqual(pp_form.name, "PN/001")
self.assertEqual(pp_form.mobile, "+34 000000001")