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

        issue3672
diffstat:

 exceptions.py |   8 ++++++++
 forecast.py   |  44 ++++++++++++++++++++------------------------
 message.xml   |  16 ++++++++++++++++
 tryton.cfg    |   1 +
 4 files changed, 45 insertions(+), 24 deletions(-)

diffs (150 lines):

diff -r 7250342cb353 -r f284990b2d64 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.model.exceptions import ValidationError
+
+
+class ForecastValidationError(ValidationError):
+    pass
diff -r 7250342cb353 -r f284990b2d64 forecast.py
--- a/forecast.py       Sun Dec 23 17:31:51 2018 +0100
+++ b/forecast.py       Sat Dec 29 14:20:29 2018 +0100
@@ -9,13 +9,17 @@
 from sql.aggregate import Sum
 from sql.conditionals import Coalesce
 
+from trytond.i18n import gettext
 from trytond.model import ModelView, Workflow, ModelSQL, fields, Unique
+from trytond.model.exceptions import AccessError
 from trytond.wizard import Wizard, StateView, StateTransition, Button
 from trytond.pyson import Not, Equal, Eval, Or, Bool, If
 from trytond.transaction import Transaction
 from trytond.pool import Pool
 from trytond.tools import reduce_ids, grouped_slice
 
+from .exceptions import ForecastValidationError
+
 __all__ = ['Forecast', 'ForecastLine', 'ForecastLineMove',
     'ForecastCompleteAsk', 'ForecastCompleteChoose', 'ForecastComplete']
 
@@ -69,12 +73,6 @@
     @classmethod
     def __setup__(cls):
         super(Forecast, cls).__setup__()
-        cls._error_messages.update({
-                'date_overlap': ('Forecast "%(first)s" overlaps with dates '
-                    'of forecast "%(second)s" in the same location.'),
-                'delete_cancel': ('Forecast "%s" must be cancelled before '
-                    'deletion.'),
-                })
         cls._order.insert(0, ('from_date', 'DESC'))
         cls._order.insert(1, ('warehouse', 'ASC'))
         cls._transitions |= set((
@@ -191,10 +189,10 @@
         forecast_id = cursor.fetchone()
         if forecast_id:
             second = self.__class__(forecast_id[0])
-            self.raise_user_error('date_overlap', {
-                    'first': self.rec_name,
-                    'second': second.rec_name,
-                    })
+            raise ForecastValidationError(
+                gettext('stock_forecast.msg_forecast_date_overlap',
+                    first=self.rec_name,
+                    second=second.rec_name))
 
     @classmethod
     def delete(cls, forecasts):
@@ -202,7 +200,9 @@
         cls.cancel(forecasts)
         for forecast in forecasts:
             if forecast.state != 'cancel':
-                cls.raise_user_error('delete_cancel', forecast.rec_name)
+                raise AccessError(
+                    gettext('stock_forecast.msg_forecast_delete_cancel',
+                        forecast=forecast.rec_name))
         super(Forecast, cls).delete(forecasts)
 
     @classmethod
@@ -307,7 +307,7 @@
         t = cls.__table__()
         cls._sql_constraints += [
             ('forecast_product_uniq', Unique(t, t.forecast, t.product),
-                'Product must be unique by forecast'),
+                'stock_forecast.msg_forecast_line_product_unique'),
             ]
 
     @classmethod
@@ -512,8 +512,14 @@
 class ForecastCompleteAsk(ModelView):
     'Complete Forecast'
     __name__ = 'stock.forecast.complete.ask'
-    from_date = fields.Date('From Date', required=True)
-    to_date = fields.Date('To Date', required=True)
+    from_date = fields.Date(
+        "From Date", required=True,
+        domain=[('from_date', '<', Eval('to_date'))],
+        depends=['to_date'])
+    to_date = fields.Date(
+        "To Date", required=True,
+        domain=[('to_date', '>', Eval('from_date'))],
+        depends=['from_date'])
 
 
 class ForecastCompleteChoose(ModelView):
@@ -540,14 +546,6 @@
             ])
     complete = StateTransition()
 
-    @classmethod
-    def __setup__(cls):
-        super(ForecastComplete, cls).__setup__()
-        cls._error_messages.update({
-                'from_to_date': (
-                    '"From Date" should be smaller than "To Date".'),
-                })
-
     def default_ask(self, fields):
         """
         Forecast dates shifted by one year.
@@ -565,8 +563,6 @@
         Forecast = pool.get('stock.forecast')
         Product = pool.get('product.product')
         forecast = Forecast(Transaction().context['active_id'])
-        if self.ask.from_date > self.ask.to_date:
-            self.raise_user_error('from_to_date')
 
         with Transaction().set_context(
                 stock_destinations=[forecast.destination.id],
diff -r 7250342cb353 -r f284990b2d64 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,16 @@
+<?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_forecast_date_overlap">
+            <field name="text">The forecasts "%(first)s" and "%(second)s" 
overlap, you must use different dates.</field>
+        </record>
+        <record model="ir.message" id="msg_forecast_delete_cancel">
+            <field name="text">To delete forecast "%(forecast)s" you must 
cancel it.</field>
+        </record>
+        <record model="ir.message" id="msg_forecast_line_product_unique">
+            <field name="text">Product must be unique by forecast.</field>
+        </record>
+    </data>
+</tryton>
diff -r 7250342cb353 -r f284990b2d64 tryton.cfg
--- a/tryton.cfg        Sun Dec 23 17:31:51 2018 +0100
+++ b/tryton.cfg        Sat Dec 29 14:20:29 2018 +0100
@@ -8,3 +8,4 @@
     stock
 xml:
     forecast.xml
+    message.xml

Reply via email to