Add ir.message and use custom exceptions

issue3672
This commit is contained in:
Cédric Krier
2018-12-29 14:20:29 +01:00
parent 7e270726de
commit 1a1dbba305
5 changed files with 37 additions and 16 deletions
+12
View File
@@ -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.exceptions import UserError, UserWarning
class StockQuantityError(UserError):
pass
class StockQuantityWarning(UserWarning):
pass
+10
View File
@@ -0,0 +1,10 @@
<?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_stock_quantity">
<field name="text">The forcast quantity (%(forecast_quantity)s%(default_uom)s) for line "%(line)s" is lower than its quantity (%(quantity)s%(unit)s).</field>
</record>
</data>
</tryton>
+11 -13
View File
@@ -3,11 +3,14 @@
import datetime
from trytond.i18n import gettext
from trytond.pool import PoolMeta, Pool
from trytond.model import ModelView, Workflow
from trytond.transaction import Transaction
from trytond.tools import grouped_slice
from .exceptions import StockQuantityError, StockQuantityWarning
__all__ = ['Sale']
@@ -15,16 +18,6 @@ __all__ = ['Sale']
class Sale(metaclass=PoolMeta):
__name__ = 'sale.sale'
@classmethod
def __setup__(cls):
super(Sale, cls).__setup__()
cls._error_messages.update({
'stock_quantity': (
'The forcast quantity '
'%(forecast_quantity)s%(default_uom)s of '
'"%(line)s" is lower than %(quantity)s%(unit)s.'),
})
@classmethod
@ModelView.button
@Workflow.transition('quotation')
@@ -72,6 +65,7 @@ class Sale(metaclass=PoolMeta):
User = pool.get('res.user')
ModelData = pool.get('ir.model.data')
Line = pool.get('sale.line')
Warning = pool.get('res.user.warning')
transaction = Transaction()
@@ -116,10 +110,14 @@ class Sale(metaclass=PoolMeta):
def raise_(line_id, message_values):
if not in_group():
self.raise_user_error('stock_quantity', message_values)
raise StockQuantityError(
gettext('sale_stock_quantity.msg_sale_stock_quantity',
**message_values))
warning_name = 'stock_quantity_warning_%s' % line_id
self.raise_user_warning(
warning_name, 'stock_quantity', message_values)
if Warning.check(warning_name):
raise StockQuantityWarning(warning_name,
gettext('sale_stock_quantity.msg_sale_stock_quantity',
**message_values))
today = Date.today()
sale_date = self.sale_date or today
+3 -3
View File
@@ -132,7 +132,7 @@ Sale 2 more products with not enough stock::
>>> sale.click('quote') # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
UserWarning: ...
StockQuantityWarning: ...
Clean sales::
@@ -149,7 +149,7 @@ Sale 6 products with not enough stock::
>>> sale.click('quote') # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
UserWarning: ...
StockQuantityWarning: ...
>>> sale.delete()
Make an inventory of 3 products in 2 days::
@@ -175,7 +175,7 @@ Sale 4 products with not enough forecast::
>>> sale.click('quote') # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
UserWarning: ...
StockQuantityWarning: ...
>>> sale.delete()
Sale 2 products with enough forecast::
+1
View File
@@ -9,3 +9,4 @@ extras_depend:
stock_supply
xml:
sale.xml
message.xml