[FIX] docuseal_signing: keep partner field mappings on fields fetch
``action_fetch_fields`` wiped the whole ``field_ids`` mapping on every fetch, dropping the res.partner fields configured by the administrator. Reconcile instead: keep existing mappings untouched, add fields new to the Docuseal template and drop the ones removed from it. Covered by unit tests running against a mocked Docuseal API. Assisted-By: GLM-5.3-Flash
This commit is contained in:
@@ -51,14 +51,23 @@ class DocusealTemplate(models.Model):
|
||||
if data.get("submitters") and not self.submitter_role:
|
||||
self.submitter_role = data["submitters"][0].get("name", "")
|
||||
|
||||
self.field_ids.unlink()
|
||||
fields_data = [
|
||||
{"template_id": self.id, "docuseal_field_name": f["name"]}
|
||||
for f in data.get("fields", [])
|
||||
if f.get("name")
|
||||
# Reconcile the field mapping with the Docuseal template instead of
|
||||
# wiping it: existing mappings keep their res.partner field.
|
||||
existing = {
|
||||
field.docuseal_field_name: field for field in self.field_ids
|
||||
}
|
||||
docuseal_names = [
|
||||
f["name"] for f in data.get("fields", []) if f.get("name")
|
||||
]
|
||||
if fields_data:
|
||||
self.env["docuseal.template.field"].create(fields_data)
|
||||
for name in docuseal_names:
|
||||
if name not in existing:
|
||||
self.env["docuseal.template.field"].create({
|
||||
"template_id": self.id,
|
||||
"docuseal_field_name": name,
|
||||
})
|
||||
self.field_ids.filtered(
|
||||
lambda field: field.docuseal_field_name not in docuseal_names
|
||||
).unlink()
|
||||
|
||||
def action_create_server_action(self):
|
||||
self.ensure_one()
|
||||
|
||||
1
docuseal_signing/tests/__init__.py
Normal file
1
docuseal_signing/tests/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from . import test_fetch_fields
|
||||
89
docuseal_signing/tests/test_fetch_fields.py
Normal file
89
docuseal_signing/tests/test_fetch_fields.py
Normal file
@@ -0,0 +1,89 @@
|
||||
import json
|
||||
from unittest import mock
|
||||
|
||||
from odoo.addons.docuseal_signing.models import (
|
||||
docuseal_template as docuseal_template_module,
|
||||
)
|
||||
from odoo.tests import TransactionCase
|
||||
|
||||
|
||||
class TestFetchFields(TransactionCase):
|
||||
"""Reconciliation of the field mapping on action_fetch_fields."""
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super().setUpClass()
|
||||
cls.phone_field = cls.env["ir.model.fields"].search(
|
||||
[("model", "=", "res.partner"), ("name", "=", "phone")], limit=1
|
||||
)
|
||||
cls.template = cls.env["docuseal.template"].create({
|
||||
"name": "Old name",
|
||||
"template_id": 999,
|
||||
"submitter_role": "Signer",
|
||||
})
|
||||
cls.env["docuseal.template.field"].create({
|
||||
"template_id": cls.template.id,
|
||||
"docuseal_field_name": "phone",
|
||||
"partner_field_id": cls.phone_field.id,
|
||||
})
|
||||
cls.env["docuseal.template.field"].create({
|
||||
"template_id": cls.template.id,
|
||||
"docuseal_field_name": "obsolete",
|
||||
})
|
||||
|
||||
def _mock_urlopen(self, payload):
|
||||
resp = mock.MagicMock()
|
||||
resp.read.return_value = json.dumps(payload).encode()
|
||||
context_manager = mock.MagicMock()
|
||||
context_manager.__enter__.return_value = resp
|
||||
context_manager.__exit__.return_value = False
|
||||
return context_manager
|
||||
|
||||
def test_fetch_fields_reconciles_mapping(self):
|
||||
"""Existing mappings keep their partner field; removed Docuseal
|
||||
fields are dropped and new ones are added."""
|
||||
payload = {
|
||||
"name": "Contract",
|
||||
"submitters": [{"name": "Signer"}],
|
||||
"fields": [{"name": "phone"}, {"name": "extra"}, {"name": ""}],
|
||||
}
|
||||
with mock.patch.object(
|
||||
docuseal_template_module.urllib.request,
|
||||
"urlopen",
|
||||
return_value=self._mock_urlopen(payload),
|
||||
):
|
||||
self.template.action_fetch_fields()
|
||||
|
||||
names = {
|
||||
field.docuseal_field_name for field in self.template.field_ids
|
||||
}
|
||||
self.assertEqual(names, {"phone", "extra"})
|
||||
|
||||
phone_mapping = self.template.field_ids.filtered(
|
||||
lambda field: field.docuseal_field_name == "phone"
|
||||
)
|
||||
self.assertEqual(phone_mapping.partner_field_id, self.phone_field)
|
||||
|
||||
# Name and submitter role are only filled when still empty.
|
||||
self.assertEqual(self.template.name, "Old name")
|
||||
self.assertEqual(self.template.submitter_role, "Signer")
|
||||
|
||||
def test_fetch_fields_fills_empty_name_and_role(self):
|
||||
"""Name and submitter role are set from the API when missing."""
|
||||
self.template.name = False
|
||||
self.template.submitter_role = False
|
||||
payload = {
|
||||
"name": "Contract",
|
||||
"submitters": [{"name": "Counterparty"}],
|
||||
"fields": [],
|
||||
}
|
||||
with mock.patch.object(
|
||||
docuseal_template_module.urllib.request,
|
||||
"urlopen",
|
||||
return_value=self._mock_urlopen(payload),
|
||||
):
|
||||
self.template.action_fetch_fields()
|
||||
|
||||
self.assertEqual(self.template.name, "Contract")
|
||||
self.assertEqual(self.template.submitter_role, "Counterparty")
|
||||
self.assertFalse(self.template.field_ids)
|
||||
Reference in New Issue
Block a user