changeset e122ce7c714f in modules/sale_stock_quantity:default
details:
https://hg.tryton.org/modules/sale_stock_quantity?cmd=changeset;node=e122ce7c714f
description:
Add ir.message and use custom exceptions
issue3672
diffstat:
exceptions.py | 12 ++++++++++++
message.xml | 10 ++++++++++
sale.py | 24 +++++++++++-------------
tests/scenario_sale_stock_quantity.rst | 6 +++---
tryton.cfg | 1 +
5 files changed, 37 insertions(+), 16 deletions(-)
diffs (129 lines):
diff -r 5a008389b7bf -r e122ce7c714f 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,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
diff -r 5a008389b7bf -r e122ce7c714f 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,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>
diff -r 5a008389b7bf -r e122ce7c714f sale.py
--- a/sale.py Mon Oct 01 15:19:15 2018 +0200
+++ b/sale.py Sat Dec 29 14:20:29 2018 +0100
@@ -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']
@@ -16,16 +19,6 @@
__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')
def quote(cls, sales):
@@ -72,6 +65,7 @@
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 @@
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
diff -r 5a008389b7bf -r e122ce7c714f tests/scenario_sale_stock_quantity.rst
--- a/tests/scenario_sale_stock_quantity.rst Mon Oct 01 15:19:15 2018 +0200
+++ b/tests/scenario_sale_stock_quantity.rst Sat Dec 29 14:20:29 2018 +0100
@@ -132,7 +132,7 @@
>>> sale.click('quote') # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
- UserWarning: ...
+ StockQuantityWarning: ...
Clean sales::
@@ -149,7 +149,7 @@
>>> 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.click('quote') # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
- UserWarning: ...
+ StockQuantityWarning: ...
>>> sale.delete()
Sale 2 products with enough forecast::
diff -r 5a008389b7bf -r e122ce7c714f tryton.cfg
--- a/tryton.cfg Mon Oct 01 15:19:15 2018 +0200
+++ b/tryton.cfg Sat Dec 29 14:20:29 2018 +0100
@@ -9,3 +9,4 @@
stock_supply
xml:
sale.xml
+ message.xml