changeset 0467f8c478b6 in modules/stock_package_shipping_dpd:default details: https://hg.tryton.org/modules/stock_package_shipping_dpd?cmd=changeset;node=0467f8c478b6 description: Add ir.message and use custom exceptions
issue3672 diffstat: carrier.py | 16 +++++----------- exceptions.py | 8 ++++++++ message.xml | 17 +++++++++++++++++ stock.py | 45 ++++++++++++++++++--------------------------- tryton.cfg | 1 + 5 files changed, 49 insertions(+), 38 deletions(-) diffs (177 lines): diff -r a5b19cf0e349 -r 0467f8c478b6 carrier.py --- a/carrier.py Fri Nov 02 20:07:30 2018 +0100 +++ b/carrier.py Sat Dec 29 14:20:30 2018 +0100 @@ -5,10 +5,12 @@ from zeep.exceptions import Fault +from trytond.i18n import gettext from trytond.model import ModelSQL, ModelView, MatchMixin, fields from trytond.pool import PoolMeta from .configuration import get_client, LOGIN_SERVICE +from .exceptions import DPDError __all__ = ['CredentialDPD', 'Carrier'] @@ -28,14 +30,6 @@ token = fields.Char('Token', readonly=True) @classmethod - def __setup__(cls): - super(CredentialDPD, cls).__setup__() - cls._error_messages.update({ - 'dpd_webservice_error': ('DPD webservice call failed with the' - ' following error message:\n\n%(message)s'), - }) - - @classmethod def default_server(cls): return 'testing' @@ -50,9 +44,9 @@ messageLanguage=lang) except Fault as e: error_message = e.detail[0].find('errorMessage') - self.raise_user_error('authentication_error', { - 'message': error_message.text, - }) + raise DPDError( + gettext('stock_package_shipping_dpd.msg_dpd_webservice_error', + message=error_message.text)) from e self.token = result.authToken self.depot = result.depot diff -r a5b19cf0e349 -r 0467f8c478b6 exceptions.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/exceptions.py Sat Dec 29 14:20:30 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 DPDError(UserError): + pass diff -r a5b19cf0e349 -r 0467f8c478b6 message.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/message.xml Sat Dec 29 14:20:30 2018 +0100 @@ -0,0 +1,17 @@ +<?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_dpd_webservice_error"> + <field name="text">DPD webservice call failed with the following error message: +%(message)s</field> + </record> + <record model="ir.message" id="msg_warehouse_address_required"> + <field name="text">To validate shipment "%(shipment)s" you must set an address on the warehouse "%(warehouse)s".</field> + </record> + <record model="ir.message" id="msg_shipment_has_reference_number"> + <field name="text">You cannot create shipping for shipment "%(shipment)s" because it has already a reference number.</field> + </record> + </data> +</tryton> diff -r a5b19cf0e349 -r 0467f8c478b6 stock.py --- a/stock.py Fri Nov 02 20:07:30 2018 +0100 +++ b/stock.py Sat Dec 29 14:20:30 2018 +0100 @@ -7,12 +7,18 @@ from zeep.exceptions import Fault from PyPDF2 import PdfFileReader, PdfFileWriter +from trytond.i18n import gettext from trytond.pool import Pool, PoolMeta from trytond.model import fields +from trytond.model.exceptions import AccessError from trytond.wizard import Wizard, StateAction, StateTransition from trytond.transaction import Transaction +from trytond.modules.stock_package_shipping.exceptions import ( + PackingValidationError) + from .configuration import get_client, SHIPMENT_SERVICE +from .exceptions import DPDError __all__ = ['ShipmentOut', 'CreateShipping', 'CreateDPDShipping'] @@ -20,20 +26,14 @@ class ShipmentOut(metaclass=PoolMeta): __name__ = 'stock.shipment.out' - @classmethod - def __setup__(cls): - super(ShipmentOut, cls).__setup__() - cls._error_messages.update({ - 'warehouse_address_required': ('An address is required for' - ' warehouse "%(warehouse)s".'), - }) - def validate_packing_dpd(self): warehouse_address = self.warehouse.address if not warehouse_address: - self.raise_user_error('warehouse_address_required', { - 'warehouse': self.warehouse.rec_name, - }) + raise PackingValidationError( + gettext('stock_package_shipping_dpd' + '.msg_warehouse_address_required', + shipment=self.rec_name, + warehouse=self.warehouse.rec_name)) class CreateShipping(metaclass=PoolMeta): @@ -67,16 +67,6 @@ start = StateTransition() - @classmethod - def __setup__(cls): - super(CreateDPDShipping, cls).__setup__() - cls._error_messages.update({ - 'has_reference_number': ('Shipment "%(shipment)s" already has' - ' a reference number.'), - 'dpd_webservice_error': ('DPD webservice call failed with the' - ' following error message:\n\n%(message)s'), - }) - def transition_start(self): pool = Pool() ShipmentOut = pool.get('stock.shipment.out') @@ -84,9 +74,10 @@ shipment = ShipmentOut(Transaction().context['active_id']) if shipment.reference: - self.raise_user_error('has_reference_number', { - 'shipment': shipment.rec_name, - }) + raise AccessError( + gettext('stock_package_shipping_dpd' + '.msg_shipment_has_reference_number', + shipment=shipment.rec_name)) credential = self.get_credential(shipment) if not credential.depot or not credential.token: @@ -128,9 +119,9 @@ response, = shipment_response.shipmentResponses if response.faults: message = '\n'.join(f.message for f in response.faults) - self.raise_user_error('dpd_webservice_error', { - 'message': message, - }) + raise DPDError( + gettext('stock_package_shipping_dpd.msg_dpd_webservice_error', + message=message)) labels = [] labels_pdf = BytesIO(shipment_response.parcellabelsPDF) diff -r a5b19cf0e349 -r 0467f8c478b6 tryton.cfg --- a/tryton.cfg Fri Nov 02 20:07:30 2018 +0100 +++ b/tryton.cfg Sat Dec 29 14:20:30 2018 +0100 @@ -12,3 +12,4 @@ xml: carrier.xml stock.xml + message.xml