From a1740b262d2778b89cfdb3af2b11a6fac21bd6a0 Mon Sep 17 00:00:00 2001 From: Akim Juillerat Date: Tue, 10 Dec 2024 18:32:18 +0100 Subject: [PATCH 1/3] pos_receipt_replace_user_by_trigram: Drop compute function The compute function does not allow any customization on how the trigram had to be generated, and there a multiple ways to define how trigram must be generated. Moreover, the compute did generate duplicates and there was no way to fix manually since the field was not stored. Also, making it stored but still computed did risk unwanted updates in case name was updated. Having a basic stored field allows each company to customize how trigram can be generated using a server action. The logic previously provided is then moved to a cron that can be customized to suit trigram generation for each company. --- .../__manifest__.py | 2 +- .../data/ir_cron.xml | 30 +++++++++++++++++++ .../models/res_partner.py | 11 ++----- .../readme/CONFIGURE.rst | 2 ++ pos_receipt_replace_user_by_trigram/utils.py | 11 ------- 5 files changed, 35 insertions(+), 21 deletions(-) create mode 100644 pos_receipt_replace_user_by_trigram/data/ir_cron.xml delete mode 100644 pos_receipt_replace_user_by_trigram/utils.py diff --git a/pos_receipt_replace_user_by_trigram/__manifest__.py b/pos_receipt_replace_user_by_trigram/__manifest__.py index d636b55c5b..1f736c2100 100644 --- a/pos_receipt_replace_user_by_trigram/__manifest__.py +++ b/pos_receipt_replace_user_by_trigram/__manifest__.py @@ -12,7 +12,6 @@ "version": "16.0.1.0.0", "depends": [ "point_of_sale", - "partner_firstname", ], "assets": { "point_of_sale.assets": [ @@ -22,6 +21,7 @@ ], }, "data": [ + "data/ir_cron.xml", "views/res_config_settings.xml", "views/res_users.xml", ], diff --git a/pos_receipt_replace_user_by_trigram/data/ir_cron.xml b/pos_receipt_replace_user_by_trigram/data/ir_cron.xml new file mode 100644 index 0000000000..a063ee2f83 --- /dev/null +++ b/pos_receipt_replace_user_by_trigram/data/ir_cron.xml @@ -0,0 +1,30 @@ + + + + POS: generate user trigram + + code + + + 1 + hours + 1 + +def get_trigram(*args): + valid = [x.strip() for x in args if x and x.strip()] + if valid: + if len(valid) > 1: + trigram = valid[0][:1] + valid[1][:2] + else: + trigram = valid[0][:3] + else: + trigram = "" + return trigram + +partners = model.search([('pos_trigram', '=', ''), ('user_ids', '=', True)]) +for partner in partners: + firstname, lastname = partner.split(' ', 1) + partner.write({'pos_trigram': get_trigram(firstname, lastname)}) + + + diff --git a/pos_receipt_replace_user_by_trigram/models/res_partner.py b/pos_receipt_replace_user_by_trigram/models/res_partner.py index 0dbf51dbf2..4e1744bb7a 100644 --- a/pos_receipt_replace_user_by_trigram/models/res_partner.py +++ b/pos_receipt_replace_user_by_trigram/models/res_partner.py @@ -1,17 +1,10 @@ # Copyright 2024 Camptocamp SA # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from odoo import api, fields, models - -from .. import utils +from odoo import fields, models class ResPartner(models.Model): _inherit = "res.partner" - pos_trigram = fields.Char(compute="_compute_pos_trigram") - - @api.depends("firstname", "lastname") - def _compute_pos_trigram(self): - for partner in self: - partner.pos_trigram = utils.get_trigram(partner.firstname, partner.lastname) + pos_trigram = fields.Char() diff --git a/pos_receipt_replace_user_by_trigram/readme/CONFIGURE.rst b/pos_receipt_replace_user_by_trigram/readme/CONFIGURE.rst index 35d761e5d7..2d1843aa76 100644 --- a/pos_receipt_replace_user_by_trigram/readme/CONFIGURE.rst +++ b/pos_receipt_replace_user_by_trigram/readme/CONFIGURE.rst @@ -2,3 +2,5 @@ By default, username on receipts is replaced by his Trigram. In "Point of Sale" configuration "Bills & Receipts" section, you can deactivate "Replace User by trigram in POS receipt" feature: .. image:: ../static/img/pos_config.png + +Generation of trigram is done through scheduled action 'POS: generate user trigram' that can be customized and executed manually. diff --git a/pos_receipt_replace_user_by_trigram/utils.py b/pos_receipt_replace_user_by_trigram/utils.py deleted file mode 100644 index c623f137b6..0000000000 --- a/pos_receipt_replace_user_by_trigram/utils.py +++ /dev/null @@ -1,11 +0,0 @@ -def get_trigram(*args): - valid = [x.strip() for x in args if x and x.strip()] - if valid: - if len(valid) > 1: - trigram = valid[0][:1] + valid[1][:2] - else: - trigram = valid[0][:3] - else: - trigram = "" - - return trigram From 528bda1df9e9e182daa02b4a4e0840bafbe7db92 Mon Sep 17 00:00:00 2001 From: Akim Juillerat Date: Tue, 10 Dec 2024 21:09:03 +0100 Subject: [PATCH 2/3] fixup! pos_receipt_replace_user_by_trigram: Drop compute function --- pos_receipt_replace_user_by_trigram/models/res_partner.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pos_receipt_replace_user_by_trigram/models/res_partner.py b/pos_receipt_replace_user_by_trigram/models/res_partner.py index 4e1744bb7a..b6d7468fa1 100644 --- a/pos_receipt_replace_user_by_trigram/models/res_partner.py +++ b/pos_receipt_replace_user_by_trigram/models/res_partner.py @@ -7,4 +7,4 @@ class ResPartner(models.Model): _inherit = "res.partner" - pos_trigram = fields.Char() + pos_trigram = fields.Char(tracking=True) From 37801848cae3b17a79a76ecc2b0ea4cc3cc2acd7 Mon Sep 17 00:00:00 2001 From: Akim Juillerat Date: Thu, 12 Dec 2024 13:46:45 +0100 Subject: [PATCH 3/3] fixup! fixup! pos_receipt_replace_user_by_trigram: Drop compute function --- .../tests/__init__.py | 1 - .../tests/test_get_trigram.py | 27 ------------------- 2 files changed, 28 deletions(-) delete mode 100644 pos_receipt_replace_user_by_trigram/tests/__init__.py delete mode 100644 pos_receipt_replace_user_by_trigram/tests/test_get_trigram.py diff --git a/pos_receipt_replace_user_by_trigram/tests/__init__.py b/pos_receipt_replace_user_by_trigram/tests/__init__.py deleted file mode 100644 index 97355089d7..0000000000 --- a/pos_receipt_replace_user_by_trigram/tests/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from . import test_get_trigram diff --git a/pos_receipt_replace_user_by_trigram/tests/test_get_trigram.py b/pos_receipt_replace_user_by_trigram/tests/test_get_trigram.py deleted file mode 100644 index 34e1a80978..0000000000 --- a/pos_receipt_replace_user_by_trigram/tests/test_get_trigram.py +++ /dev/null @@ -1,27 +0,0 @@ -from odoo.tests.common import TransactionCase - -from .. import utils - - -class TestUtils(TransactionCase): - def test_get_trigram(self): - """ - extract 1 letter from firstname and 2 letters from lastname - - If one of the fields is empty, extract 3 letters from the other field - - If both fields are empty, return an empty string - """ - firstname = "Tri" - lastname = "Doan" - self.assertEqual(utils.get_trigram(firstname, lastname), "TDo") - - firstname = "" - lastname = "Doan" - self.assertEqual(utils.get_trigram(firstname, lastname), "Doa") - - firstname = "Tri" - lastname = "" - self.assertEqual(utils.get_trigram(firstname, lastname), "Tri") - - firstname = "" - lastname = "" - self.assertEqual(utils.get_trigram(firstname, lastname), "")