details:   https://code.tryton.org/tryton/commit/f5eaf861e4bc
branch:    8.0
user:      Cédric Krier <[email protected]>
date:      Wed Sep 30 18:30:11 2026 +0200
description:
        Validate stdnum code before compacting and formatting it

        Closes #15017
        (grafted from 7d7d0ccfb5ab052e3a83b8d33c581d41dcf9eb38)
diffstat:

 modules/party/party.py |  71 ++++++++++++++++++++++++++++---------------------
 1 files changed, 40 insertions(+), 31 deletions(-)

diffs (105 lines):

diff -r 5373473672a2 -r f5eaf861e4bc modules/party/party.py
--- a/modules/party/party.py    Thu Oct 01 09:54:29 2026 +0200
+++ b/modules/party/party.py    Wed Sep 30 18:30:11 2026 +0200
@@ -395,12 +395,15 @@
         else:
             for country in eu_vat.guess_country(text):
                 if 'eu_vat' in identifier_types:
-                    code = eu_vat.compact(country + text)
-                    add('eu_vat', code)
+                    code = country + text
+                    if eu_vat.is_valid(code):
+                        code = eu_vat.compact(code)
+                        add('eu_vat', code)
                 elif f'{country}_vat' in identifier_types:
                     vat = get_cc_module(country, 'vat')
-                    code = vat.compact(text)
-                    add(f'{country}_vat', code)
+                    if vat.is_valid(text):
+                        code = vat.compact(text)
+                        add(f'{country}_vat', code)
         return result
 
     @classmethod
@@ -913,18 +916,20 @@
     @fields.depends('type', 'code')
     def on_change_with_code(self):
         code = self.code
-        if self.type and '_' in self.type:
+        if code and self.type and '_' in self.type:
             if module := get_cc_module(*self.type.split('_', 1)):
-                if hasattr(module, 'compact'):
-                    try:
-                        code = module.compact(code)
-                    except stdnum.exceptions.ValidationError:
-                        pass
-                if hasattr(module, 'format'):
-                    try:
-                        code = module.format(code)
-                    except stdnum.exceptions.ValidationError:
-                        pass
+                if (hasattr(module, 'is_valid')
+                        and module.is_valid(code)):
+                    if hasattr(module, 'compact'):
+                        try:
+                            code = module.compact(code)
+                        except stdnum.exceptions.ValidationError:
+                            pass
+                    if hasattr(module, 'format'):
+                        try:
+                            code = module.format(code)
+                        except stdnum.exceptions.ValidationError:
+                            pass
         return code
 
     def pre_validate(self):
@@ -955,16 +960,18 @@
             if ((type := values.get('type')) and '_' in type
                     and (code := values.get('code'))):
                 if module := get_cc_module(*type.split('_', 1)):
-                    if hasattr(module, 'format'):
-                        try:
-                            values['code'] = module.format(code)
-                        except stdnum.exceptions.ValidationError:
-                            pass
-                    if hasattr(module, 'compact'):
-                        try:
-                            values['code_compact'] = module.compact(code)
-                        except stdnum.exceptions.ValidationError:
-                            pass
+                    if (hasattr(module, 'is_valid')
+                            and module.is_valid(code)):
+                        if hasattr(module, 'format'):
+                            try:
+                                values['code'] = module.format(code)
+                            except stdnum.exceptions.ValidationError:
+                                pass
+                        if hasattr(module, 'compact'):
+                            try:
+                                values['code_compact'] = module.compact(code)
+                            except stdnum.exceptions.ValidationError:
+                                pass
         return values
 
     def compute_fields(self, field_names=None):
@@ -974,12 +981,14 @@
             code_compact = getattr(self, 'code_compact', None)
             if (type := getattr(self, 'type', None)) and '_' in type and code:
                 if module := get_cc_module(*type.split('_', 1)):
-                    if hasattr(module, 'format'):
-                        code = module.format(code)
-                    if hasattr(module, 'compact'):
-                        code_compact = module.compact(code)
-                    else:
-                        code_compact = code
+                    if (hasattr(module, 'is_valid')
+                            and module.is_valid(code)):
+                        if hasattr(module, 'format'):
+                            code = module.format(code)
+                        if hasattr(module, 'compact'):
+                            code_compact = module.compact(code)
+                        else:
+                            code_compact = code
                     if getattr(self, 'code', None) != code:
                         values['code'] = code
                     if getattr(self, 'code_compact', None) != code_compact:

Reply via email to