changeset f41db06e5013 in modules/account_product:default
details:
https://hg.tryton.org/modules/account_product?cmd=changeset;node=f41db06e5013
description:
Display proper missing field for account_used
The error message should be different between when the product has no
accounting category and when the accounting category has not requested
account.
issue9135
review284991002
diffstat:
product.py | 12 ++++++++----
tests/test_account_product.py | 25 +++++++++++++++++--------
2 files changed, 25 insertions(+), 12 deletions(-)
diffs (98 lines):
diff -r 8af7c037bf44 -r f41db06e5013 product.py
--- a/product.py Thu Mar 19 19:00:55 2020 +0100
+++ b/product.py Sat Mar 28 14:30:52 2020 +0100
@@ -18,9 +18,6 @@
def account_used(field_name, field_string=None):
- if field_string is None:
- field_string = field_name
-
def decorator(func):
@wraps(func)
def wrapper(self):
@@ -29,8 +26,15 @@
account = self.get_account(field_name + '_used')
# Allow empty values on on_change
if not account and not Transaction().readonly:
+ Model = self.__class__
+ field = field_name
+ if field_string:
+ if getattr(self, field_string, None):
+ Model = getattr(self, field_string).__class__
+ else:
+ field = field_string
field = (
- self.fields_get([field_string])[field_string]['string'])
+ Model.fields_get([field])[field]['string'])
raise AccountError(
gettext('account_product.msg_missing_account',
field=field,
diff -r 8af7c037bf44 -r f41db06e5013 tests/test_account_product.py
--- a/tests/test_account_product.py Thu Mar 19 19:00:55 2020 +0100
+++ b/tests/test_account_product.py Sat Mar 28 14:30:52 2020 +0100
@@ -39,27 +39,35 @@
# raise when empty
template = ProductTemplate(
- name='test account used',
+ name='Product',
list_price=Decimal(10),
default_uom=unit.id,
products=[],
)
template.save()
- with self.assertRaises(UserError):
+ with self.assertRaisesRegex(
+ UserError, 'Account Category.*Product'):
template.account_expense_used
# with account on category
- category = ProductCategory(name='test account used',
- account_expense=account_expense, accounting=True)
+ category = ProductCategory(
+ name='Category', accounting=True)
category.save()
template.account_category = category
template.save()
+ with self.assertRaisesRegex(
+ UserError, 'Account Expense.*Product'):
+ template.account_expense_used
+
+ category.account_expense = account_expense
+ category.save()
+
self.assertEqual(template.account_expense_used, account_expense)
# with account on grant category
- parent_category = ProductCategory(name='parent account used',
+ parent_category = ProductCategory(name='Parent Category',
account_expense=account_expense, accounting=True)
parent_category.save()
category.account_expense = None
@@ -72,11 +80,11 @@
# raise only at direct usage
categories = ProductCategory.create([{
- 'name': 'test with account',
+ 'name': 'Category 1',
'accounting': True,
'account_expense': account_expense.id,
}, {
- 'name': 'test without account',
+ 'name': 'Category 2',
'accounting': True,
'account_expense': None,
}])
@@ -84,7 +92,8 @@
self.assertEqual(categories[0].account_expense_used.id,
account_expense.id)
- with self.assertRaises(UserError):
+ with self.assertRaisesRegex(
+ UserError, 'Account Expense.*Category 2'):
categories[1].account_expense_used