Cédric Krier pushed to branch branch/default at Tryton / Tryton


Commits:
0d27ddd4 by Cédric Krier at 2023-04-13T16:40:09+02:00
Handle evaluation error in get_cost_price of cost price revision

The method can be called with unsaved record so the check has not yet been
called.

Closes #12216
- - - - -


2 changed files:

- modules/stock/message.xml
- modules/stock/product.py


Changes:

=====================================
modules/stock/message.xml
=====================================
@@ -16,6 +16,9 @@
         <record model="ir.message" id="msg_invalid_cost_price">
             <field name="text">Invalid cost price "%(cost_price)s" for product 
"%(product)s" with exception "%(exception)s".</field>
         </record>
+        <record model="ir.message" id="msg_invalid_cost_price_not_number">
+            <field name="text">The value "%(value)s" of "%(cost_price)s" for 
product "%(product)s" is not a number.</field>
+        </record>
         <record model="ir.message" id="msg_location_invalid_type_for_moves">
             <field name="text">You cannot change the type of location 
"%(location)s" to "%(type)s"
 because the type does not support moves and location has existing 
moves.</field>


=====================================
modules/stock/product.py
=====================================
@@ -6,7 +6,7 @@
 from copy import deepcopy
 from decimal import Decimal
 
-from simpleeval import simple_eval
+from simpleeval import InvalidExpression, simple_eval
 from sql import Literal, Select, Window, With
 from sql.aggregate import Max, Sum
 from sql.conditionals import Case, Coalesce
@@ -1253,18 +1253,8 @@
         if field_names and 'cost_price' not in field_names:
             return
         for revision in revisions:
-            try:
-                if not isinstance(
-                        revision.get_cost_price(Decimal(0)), Decimal):
-                    raise ValueError
-            except Exception as exception:
-                product = revision.product or revision.template
-                raise ProductCostPriceError(
-                    gettext('stock.msg_invalid_cost_price',
-                        cost_price=revision.cost_price,
-                        product=product.rec_name,
-                        exception=exception)) from exception
+            revision.get_cost_price(Decimal(0))
 
     def get_cost_price(self, cost_price, **context):
         context.setdefault('names', {})['cost_price'] = cost_price
         context.setdefault('functions', {})['Decimal'] = Decimal
@@ -1267,8 +1257,23 @@
 
     def get_cost_price(self, cost_price, **context):
         context.setdefault('names', {})['cost_price'] = cost_price
         context.setdefault('functions', {})['Decimal'] = Decimal
-        return simple_eval(decistmt(self.cost_price), **context)
+        try:
+            amount = simple_eval(decistmt(self.cost_price), **context)
+        except (InvalidExpression, SyntaxError) as exception:
+            product = self.product or self.template
+            raise ProductCostPriceError(
+                gettext('stock.msg_invalid_cost_price',
+                    cost_price=self.cost_price,
+                    product=product.rec_name,
+                    exception=exception)) from exception
+        if not isinstance(amount, Decimal):
+            raise ProductCostPriceError(
+                gettext('stock.msg_invalid_cost_price_not_number',
+                    value=amount,
+                    cost_price=self.cost_price,
+                    product=product.rec_name))
+        return amount
 
     @classmethod
     def _get_for_product_domain(cls):



View it on Heptapod: 
https://foss.heptapod.net/tryton/tryton/-/commit/0d27ddd4d6176b3e33f9be1a71d8320f39e0ddd7

-- 
View it on Heptapod: 
https://foss.heptapod.net/tryton/tryton/-/commit/0d27ddd4d6176b3e33f9be1a71d8320f39e0ddd7
You're receiving this email because of your account on foss.heptapod.net.


Reply via email to