changeset ce9d9e78f041 in modules/party_siret:default
details:
https://hg.tryton.org/modules/party_siret?cmd=changeset;node=ce9d9e78f041
description:
Add ir.message and use custom exceptions
issue3672
diffstat:
address.py | 19 ++++++-------------
exceptions.py | 12 ++++++++++++
message.xml | 13 +++++++++++++
party.py | 18 ++++++------------
tryton.cfg | 1 +
5 files changed, 38 insertions(+), 25 deletions(-)
diffs (126 lines):
diff -r 82a20dc474cd -r ce9d9e78f041 address.py
--- a/address.py Mon Oct 01 12:40:49 2018 +0200
+++ b/address.py Sat Dec 29 14:20:29 2018 +0100
@@ -1,9 +1,11 @@
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
+from trytond.i18n import gettext
from trytond.model import fields
from trytond.pyson import Eval
from trytond.pool import PoolMeta
from . import luhn
+from .exceptions import SIRETValidationError
__all__ = ['Address']
@@ -16,15 +18,6 @@
}, size=5, depends=['active'])
siret = fields.Function(fields.Char('SIRET'), 'get_siret')
- @classmethod
- def __setup__(cls):
- super(Address, cls).__setup__()
- cls._error_messages.update({
- 'invalid_siret': (
- 'Invalid SIRET number "%(siret)s" on address '
- '"%(address)s"'),
- })
-
def get_siret(self, name):
if self.party.siren and self.siret_nic:
return self.party.siren + self.siret_nic
@@ -42,7 +35,7 @@
if self.siret:
if (len(self.siret) != 14
or not luhn.validate(self.siret)):
- self.raise_user_error('invalid_siret', {
- 'siret': self.siret,
- 'address': self.rec_name,
- })
+ raise SIRETValidationError(
+ gettext('party_siret.msg_invalid_siret',
+ number=self.siret,
+ address=self.rec_name))
diff -r 82a20dc474cd -r ce9d9e78f041 exceptions.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/exceptions.py Sat Dec 29 14:20:29 2018 +0100
@@ -0,0 +1,12 @@
+# This file is part of Tryton. The COPYRIGHT file at the top level of
+# this repository contains the full copyright notices and license terms.
+
+from trytond.model.exceptions import ValidationError
+
+
+class SIRENValidationError(ValidationError):
+ pass
+
+
+class SIRETValidationError(SIRENValidationError):
+ pass
diff -r 82a20dc474cd -r ce9d9e78f041 message.xml
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/message.xml Sat Dec 29 14:20:29 2018 +0100
@@ -0,0 +1,13 @@
+<?xml version="1.0"?>
+<!-- This file is part of Tryton. The COPYRIGHT file at the top level of
+this repository contains the full copyright notices and license terms. -->
+<tryton>
+ <data group="1">
+ <record model="ir.message" id="msg_invalid_siren">
+ <field name="text">The SIREN number "%(number)s" for party
"%(party)s" is not valid.</field>
+ </record>
+ <record model="ir.message" id="msg_invalid_siret">
+ <field name="text">The SIRET number "%(number)s" for address
"%(address)s" is not valid.</field>
+ </record>
+ </data>
+</tryton>
diff -r 82a20dc474cd -r ce9d9e78f041 party.py
--- a/party.py Mon Oct 01 12:40:49 2018 +0200
+++ b/party.py Sat Dec 29 14:20:29 2018 +0100
@@ -1,9 +1,11 @@
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
+from trytond.i18n import gettext
from trytond.model import fields
from trytond.pyson import Eval
from trytond.pool import PoolMeta
from . import luhn
+from .exceptions import SIRENValidationError
__all__ = ['Party']
@@ -16,14 +18,6 @@
}, size=9, depends=['active'])
@classmethod
- def __setup__(cls):
- super(Party, cls).__setup__()
- cls._error_messages.update({
- 'invalid_siren': ('Invalid SIREN number "%(siren)s" on party '
- '"%(party)s".'),
- })
-
- @classmethod
def validate(cls, parties):
super(Party, cls).validate(parties)
for party in parties:
@@ -35,7 +29,7 @@
'''
if self.siren:
if len(self.siren) != 9 or not luhn.validate(self.siren):
- self.raise_user_error('invalid_siren', {
- 'siren': self.siren,
- 'party': self.rec_name,
- })
+ raise SIRENValidationError(
+ gettext('party_siret.msg_invalid_siren',
+ number=self.siren,
+ party=self.rec_name))
diff -r 82a20dc474cd -r ce9d9e78f041 tryton.cfg
--- a/tryton.cfg Mon Oct 01 12:40:49 2018 +0200
+++ b/tryton.cfg Sat Dec 29 14:20:29 2018 +0100
@@ -6,3 +6,4 @@
xml:
party.xml
address.xml
+ message.xml