changeset a131b05bbb5d in modules/product:default
details: https://hg.tryton.org/modules/product?cmd=changeset&node=a131b05bbb5d
description:
Prevent editing list and cost prices without proper context
issue10773
review338951002
diffstat:
CHANGELOG | 1 +
product.py | 29 +++++++++++++++++++++++++++--
tests/scenario_product_identifier.rst | 1 -
tests/scenario_product_variant.rst | 5 +++++
tests/test_product.py | 6 ------
5 files changed, 33 insertions(+), 9 deletions(-)
diffs (158 lines):
diff -r 48e257979e4b -r a131b05bbb5d CHANGELOG
--- a/CHANGELOG Mon Sep 13 23:29:56 2021 +0200
+++ b/CHANGELOG Fri Oct 08 19:16:44 2021 +0200
@@ -1,3 +1,4 @@
+* Prevent editing list and cost prices without a company
* Make list price not required on product
* Provide digits from unit of measure
* Allow only fixed cost price as method for service
diff -r 48e257979e4b -r a131b05bbb5d product.py
--- a/product.py Mon Sep 13 23:29:56 2021 +0200
+++ b/product.py Fri Oct 08 19:16:44 2021 +0200
@@ -75,6 +75,9 @@
"regardless of stock level.")
list_price = fields.MultiValue(fields.Numeric(
"List Price", digits=price_digits,
+ states={
+ 'readonly': ~Eval('context', {}).get('company'),
+ },
help="The standard price the product is sold at."))
list_prices = fields.One2Many(
'product.list_price', 'template', "List Prices")
@@ -360,7 +363,10 @@
'product.identifier', 'product', "Identifiers",
help="Other identifiers associated with the variant.")
cost_price = fields.MultiValue(fields.Numeric(
- "Cost Price", required=True, digits=price_digits,
+ "Cost Price", digits=price_digits,
+ states={
+ 'readonly': ~Eval('context', {}).get('company'),
+ },
help="The amount it costs to purchase or make the variant, "
"or carry out the service."))
cost_prices = fields.One2Many(
@@ -478,9 +484,18 @@
return pool.get('product.cost_price')
return super(Product, cls).multivalue_model(field)
+ def set_multivalue(self, name, value, save=True, **pattern):
+ context = Transaction().context
+ if name in {'cost_price', 'list_price'} and not value:
+ if not pattern.get('company', context.get('company')):
+ return []
+ return super().set_multivalue(name, value, save=save, **pattern)
+
@classmethod
def default_cost_price(cls, **pattern):
- return Decimal(0)
+ context = Transaction().context
+ if pattern.get('company', context.get('company')):
+ return Decimal(0)
@classmethod
def search_template(cls, name, clause):
@@ -623,6 +638,11 @@
list_price = fields.Numeric("List Price", digits=price_digits)
@classmethod
+ def __setup__(cls):
+ super().__setup__()
+ cls.company.required = True
+
+ @classmethod
def __register__(cls, module_name):
exist = backend.TableHandler.table_exist(cls._table)
@@ -714,6 +734,11 @@
"Cost Price", required=True, digits=price_digits)
@classmethod
+ def __setup__(cls):
+ super().__setup__()
+ cls.company.required = True
+
+ @classmethod
def __register__(cls, module_name):
pool = Pool()
Product = pool.get('product.product')
diff -r 48e257979e4b -r a131b05bbb5d tests/scenario_product_identifier.rst
--- a/tests/scenario_product_identifier.rst Mon Sep 13 23:29:56 2021 +0200
+++ b/tests/scenario_product_identifier.rst Fri Oct 08 19:16:44 2021 +0200
@@ -21,7 +21,6 @@
>>> template.name = 'product'
>>> template.default_uom = unit
>>> template.type = 'service'
- >>> template.list_price = Decimal('40')
>>> template.save()
>>> product, = template.products
diff -r 48e257979e4b -r a131b05bbb5d tests/scenario_product_variant.rst
--- a/tests/scenario_product_variant.rst Mon Sep 13 23:29:56 2021 +0200
+++ b/tests/scenario_product_variant.rst Fri Oct 08 19:16:44 2021 +0200
@@ -7,11 +7,16 @@
>>> from decimal import Decimal
>>> from proteus import Model
>>> from trytond.tests.tools import activate_modules
+ >>> from trytond.modules.company.tests.tools import create_company
Activate modules::
>>> config = activate_modules('product')
+Create company::
+
+ >>> _ = create_company()
+
Create a template::
>>> ProductUom = Model.get('product.uom')
diff -r 48e257979e4b -r a131b05bbb5d tests/test_product.py
--- a/tests/test_product.py Mon Sep 13 23:29:56 2021 +0200
+++ b/tests/test_product.py Fri Oct 08 19:16:44 2021 +0200
@@ -279,7 +279,6 @@
pt1, pt2 = Template.create([{
'name': 'P1',
'type': 'goods',
- 'list_price': Decimal(20),
'default_uom': kilogram.id,
'products': [('create', [{
'code': '1',
@@ -287,7 +286,6 @@
}, {
'name': 'P2',
'type': 'goods',
- 'list_price': Decimal(20),
'default_uom': millimeter.id,
'products': [('create', [{
'code': '2',
@@ -318,7 +316,6 @@
'name': 'Some product-1',
'categories': [('add', [category1.id])],
'type': 'goods',
- 'list_price': Decimal('10'),
'default_uom': uom.id,
'products': [('create', [{}])],
}
@@ -326,7 +323,6 @@
'name': 'Some product-2',
'categories': [('add', [category2.id])],
'type': 'goods',
- 'list_price': Decimal('10'),
'default_uom': uom.id,
'products': [('create', [{}])],
}
@@ -397,14 +393,12 @@
values1 = {
'name': 'Product A',
'type': 'assets',
- 'list_price': Decimal('10'),
'default_uom': uom.id,
'products': [('create', [{'suffix_code': 'AA'}])],
}
values2 = {
'name': 'Product B',
'type': 'goods',
- 'list_price': Decimal('10'),
'default_uom': uom.id,
'products': [('create', [{'suffix_code': 'BB'}])],
}