From f8a7ac6ae2bf30fc9f3929dff3c18c58c6140257 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 29 Dec 2018 14:20:29 +0100 Subject: [PATCH] Add ir.message and use custom exceptions issue3672 --- exceptions.py | 8 ++++++++ message.xml | 13 +++++++++++++ sale.py | 19 ++++++------------- stock.py | 17 +++++++---------- tryton.cfg | 1 + 5 files changed, 35 insertions(+), 23 deletions(-) create mode 100644 exceptions.py create mode 100644 message.xml diff --git a/exceptions.py b/exceptions.py new file mode 100644 index 0000000000..172a4daf87 --- /dev/null +++ b/exceptions.py @@ -0,0 +1,8 @@ +# 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.exceptions import UserError + + +class InvoiceShipmentCostError(UserError): + pass diff --git a/message.xml b/message.xml new file mode 100644 index 0000000000..37517489f6 --- /dev/null +++ b/message.xml @@ -0,0 +1,13 @@ + + + + + + To confirm sale "%(sale)s" you must change the carrier "%(carrier)s". + + + To invoice shipment cost of "%(shipment)s", you must define an account revenue for product "%(product)s". + + + diff --git a/sale.py b/sale.py index 673f6e8b4c..37aed80f91 100644 --- a/sale.py +++ b/sale.py @@ -3,12 +3,14 @@ from decimal import Decimal from trytond import backend +from trytond.i18n import gettext from trytond.model import ModelView, Workflow, fields from trytond.transaction import Transaction from trytond.pyson import Eval, If from trytond.pool import Pool, PoolMeta from trytond.modules.product import price_digits +from trytond.modules.sale.exceptions import SaleConfirmError __all__ = ['Configuration', 'ConfigurationSaleMethod', 'Sale', 'SaleLine', 'ReturnSale', 'Promotion'] @@ -96,15 +98,6 @@ class Sale(metaclass=PoolMeta): 'readonly': Eval('state') != 'draft', }, depends=['state']) - @classmethod - def __setup__(cls): - super(Sale, cls).__setup__() - cls._error_messages.update({ - 'invalid_carrier': ( - "The carrier '%(carrier)s' on sale '%(sale)s' " - "is not valid."), - }) - @staticmethod def default_shipment_cost_method(): Config = Pool().get('sale.configuration') @@ -193,10 +186,10 @@ class Sale(metaclass=PoolMeta): def confirm(cls, sales): for sale in sales: if sale.carrier and sale.carrier not in sale.available_carriers: - cls.raise_user_error('invalid_carrier', { - 'sale': sale.rec_name, - 'carrier': sale.carrier.rec_name, - }) + raise SaleConfirmError( + gettext('sale_shipment_cost.msg_sale_invalid_carrier', + sale=sale.rec_name, + carrier=sale.carrier.rec_name)) super(Sale, cls).confirm(sales) def _get_carrier_context(self): diff --git a/stock.py b/stock.py index f0fdd691da..5fd3861123 100644 --- a/stock.py +++ b/stock.py @@ -2,12 +2,14 @@ # this repository contains the full copyright notices and license terms. from decimal import Decimal +from trytond.i18n import gettext from trytond.model import fields from trytond.pyson import Eval, Bool from trytond.transaction import Transaction from trytond.pool import Pool, PoolMeta from trytond.modules.product import price_digits +from .exceptions import InvoiceShipmentCostError __all__ = ['ShipmentOut'] @@ -35,14 +37,6 @@ class ShipmentOut(metaclass=PoolMeta): cost_invoice_line = fields.Many2One('account.invoice.line', 'Cost Invoice Line', readonly=True) - @classmethod - def __setup__(cls): - super(ShipmentOut, cls).__setup__() - cls._error_messages.update({ - 'missing_account_revenue': ('Missing "Account Revenue" on ' - 'product "%s".'), - }) - def _get_carrier_context(self): return {} @@ -119,6 +113,9 @@ class ShipmentOut(metaclass=PoolMeta): invoice_line.account = product.account_revenue_used if not invoice_line.account: - self.raise_user_error('missing_account_revenue', - error_args=(product.rec_name,)) + raise InvoiceShipmentCostError( + gettext('sale_shipment_cost' + '.msg_shipment_cost_invoice_missing_account_revenue', + shipment=self.rec_name, + product=product.rec_name)) return invoice_line diff --git a/tryton.cfg b/tryton.cfg index b82ebe3f55..c5bc285854 100644 --- a/tryton.cfg +++ b/tryton.cfg @@ -13,3 +13,4 @@ extras_depend: xml: sale.xml stock.xml + message.xml