changeset 39e7fd8158da in modules/sale_shipment_cost:default
details: 
https://hg.tryton.org/modules/sale_shipment_cost?cmd=changeset;node=39e7fd8158da
description:
        Add ir.message and use custom exceptions

        issue3672
diffstat:

 exceptions.py |   8 ++++++++
 message.xml   |  13 +++++++++++++
 sale.py       |  19 ++++++-------------
 stock.py      |  17 +++++++----------
 tryton.cfg    |   1 +
 5 files changed, 35 insertions(+), 23 deletions(-)

diffs (131 lines):

diff -r eed0e955a709 -r 39e7fd8158da 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,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 -r eed0e955a709 -r 39e7fd8158da 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_sale_invalid_carrier">
+            <field name="text">To confirm sale "%(sale)s" you must change the 
carrier "%(carrier)s".</field>
+        </record>
+        <record model="ir.message" 
id="msg_shipment_cost_invoice_missing_account_revenue">
+            <field name="text">To invoice shipment cost of "%(shipment)s", you 
must define an account revenue for product "%(product)s".</field>
+        </record>
+    </data>
+</tryton>
diff -r eed0e955a709 -r 39e7fd8158da sale.py
--- a/sale.py   Tue Dec 04 17:45:05 2018 +0100
+++ b/sale.py   Sat Dec 29 14:20:29 2018 +0100
@@ -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 @@
             '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 @@
     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 -r eed0e955a709 -r 39e7fd8158da stock.py
--- a/stock.py  Tue Dec 04 17:45:05 2018 +0100
+++ b/stock.py  Sat Dec 29 14:20:29 2018 +0100
@@ -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 @@
     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 @@
 
         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 -r eed0e955a709 -r 39e7fd8158da tryton.cfg
--- a/tryton.cfg        Tue Dec 04 17:45:05 2018 +0100
+++ b/tryton.cfg        Sat Dec 29 14:20:29 2018 +0100
@@ -13,3 +13,4 @@
 xml:
     sale.xml
     stock.xml
+    message.xml

Reply via email to