Add ir.message and use custom exceptions
issue3672
This commit is contained in:
@@ -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, AccessError
|
||||
|
||||
|
||||
class FormulaError(ValidationError):
|
||||
pass
|
||||
|
||||
|
||||
class ShippingBlocked(AccessError):
|
||||
pass
|
||||
+13
@@ -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_term_line_invalid_formula">
|
||||
<field name="text">Invalid formula "%(formula)s" in term line "%(term_line)s" with exception "%(exception)s".</field>
|
||||
</record>
|
||||
<record model="ir.message" id="msg_shipping_blocked">
|
||||
<field name="text">To pack shipments the customer must paid the advance payment for sale "%(sale)s".</field>
|
||||
</record>
|
||||
</data>
|
||||
</tryton>
|
||||
@@ -4,6 +4,7 @@ from decimal import Decimal
|
||||
from simpleeval import simple_eval
|
||||
from itertools import chain
|
||||
|
||||
from trytond.i18n import gettext
|
||||
from trytond.pool import Pool, PoolMeta
|
||||
from trytond.model import (
|
||||
ModelSQL, ModelView, Workflow, DeactivableMixin, fields)
|
||||
@@ -12,6 +13,8 @@ from trytond.tools import decistmt
|
||||
from trytond.transaction import Transaction
|
||||
from trytond.modules.company.model import CompanyMultiValueMixin, CompanyValueMixin
|
||||
|
||||
from .exceptions import FormulaError
|
||||
|
||||
__all__ = [
|
||||
'AdvancePaymentTerm', 'AdvancePaymentTermLine',
|
||||
'AdvancePaymentTermLineAccount',
|
||||
@@ -80,14 +83,6 @@ class AdvancePaymentTermLine(ModelView, ModelSQL, CompanyMultiValueMixin):
|
||||
"- total_amount: The total amount of the sale.\n"
|
||||
"- untaxed_amount: The total untaxed amount of the sale.")
|
||||
|
||||
@classmethod
|
||||
def __setup__(cls):
|
||||
super(AdvancePaymentTermLine, cls).__setup__()
|
||||
cls._error_messages.update({
|
||||
'invalid_formula': ('Invalid formula "%(formula)s" with'
|
||||
' exception "%(exception)s".'),
|
||||
})
|
||||
|
||||
@fields.depends('formula')
|
||||
def pre_validate(self, **names):
|
||||
super(AdvancePaymentTermLine, self).pre_validate()
|
||||
@@ -96,10 +91,11 @@ class AdvancePaymentTermLine(ModelView, ModelSQL, CompanyMultiValueMixin):
|
||||
if not isinstance(self.compute_amount(**names), Decimal):
|
||||
raise Exception('The formula does not return a Decimal')
|
||||
except Exception as exception:
|
||||
self.raise_user_error('invalid_formula', {
|
||||
'formula': self.formula,
|
||||
'exception': exception,
|
||||
})
|
||||
raise FormulaError(
|
||||
gettext('sale_advance_payment.msg_term_line_invalid_formula',
|
||||
formula=self.formula,
|
||||
term_line=self.rec_name,
|
||||
exception=exception)) from exception
|
||||
|
||||
def get_compute_amount_context(self, **names):
|
||||
return {
|
||||
|
||||
@@ -1,24 +1,18 @@
|
||||
# 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 ModelView, Workflow
|
||||
from trytond.pool import Pool, PoolMeta
|
||||
|
||||
from .exceptions import ShippingBlocked
|
||||
|
||||
__all__ = ['ShipmentOut']
|
||||
|
||||
|
||||
class ShipmentOut(metaclass=PoolMeta):
|
||||
__name__ = 'stock.shipment.out'
|
||||
|
||||
@classmethod
|
||||
def __setup__(cls):
|
||||
super(ShipmentOut, cls).__setup__()
|
||||
cls._error_messages.update({
|
||||
'advance_payment_not_paid': ('The customer has not paid the'
|
||||
' required advance payment amount for the sale'
|
||||
' "%(sale)s".'),
|
||||
})
|
||||
|
||||
@classmethod
|
||||
@ModelView.button
|
||||
@Workflow.transition('packed')
|
||||
@@ -32,7 +26,7 @@ class ShipmentOut(metaclass=PoolMeta):
|
||||
if isinstance(move.origin, SaleLine)}
|
||||
for sale in Sale.browse([s.id for s in sales]):
|
||||
if sale.shipping_blocked:
|
||||
cls.raise_user_error('advance_payment_not_paid', {
|
||||
'sale': sale.rec_name,
|
||||
})
|
||||
raise ShippingBlocked(
|
||||
gettext('sale_advance_payment.msg_shipping_blocked',
|
||||
sale=sale.rec_name))
|
||||
super(ShipmentOut, cls).pack(shipments)
|
||||
|
||||
@@ -252,7 +252,7 @@ Let's try to pack it::
|
||||
>>> shipment.click('pack') # doctest: +IGNORE_EXCEPTION_DETAIL
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
UserError: ...
|
||||
ShippingBlocked: ...
|
||||
|
||||
Let's pay the advance payment invoice::
|
||||
|
||||
|
||||
@@ -9,3 +9,4 @@ extras_depend:
|
||||
sale_supply
|
||||
xml:
|
||||
sale.xml
|
||||
message.xml
|
||||
|
||||
Reference in New Issue
Block a user