Commit b5900907 authored by Jérome Perrin's avatar Jérome Perrin

budget: fix NodeBudgetVariation with same categories as variation

This factorize code and fix a problem that when the variation
category was a Category from the same variation base category,
we had the base category twice ( something like
group/group/nexedi )
parent f15a5f40
...@@ -89,19 +89,34 @@ class NodeBudgetVariation(BudgetVariation): ...@@ -89,19 +89,34 @@ class NodeBudgetVariation(BudgetVariation):
node_title_method_id = self.getProperty('node_title_method_id', 'getTitle') node_title_method_id = self.getProperty('node_title_method_id', 'getTitle')
return guarded_getattr(node, node_title_method_id)() return guarded_getattr(node, node_title_method_id)()
security.declareProtected(Permissions.AccessContentsInformation, def _getNodeItemList(self, context, is_right_display=False):
'getCellRangeForBudgetLine') """Returns a list of (title, relative URL) for all nodes of a node.
def getCellRangeForBudgetLine(self, budget_line, matrixbox=0):
"""The cell range added by this variation If `is_right_display` is true, returns a list of (relative URL, title), for
compatibility with matrixbox.
""" """
base_category = self.getProperty('variation_base_category') base_category = self.getProperty('variation_base_category')
prefix = '' prefix = ''
if base_category: if base_category:
prefix = '%s/' % base_category prefix = '%s/' % base_category
def addPrefix(relative_url):
if relative_url.startswith(prefix):
return relative_url
return '%s%s' % (prefix, relative_url)
if is_right_display:
return [
(addPrefix(node.getRelativeUrl()), self._getNodeTitle(node))
for node in self._getNodeList(context)]
return [
(self._getNodeTitle(node), addPrefix(node.getRelativeUrl()))
for node in self._getNodeList(context)]
node_item_list = [('%s%s' % (prefix, node.getRelativeUrl()), security.declareProtected(Permissions.AccessContentsInformation,
self._getNodeTitle(node)) 'getCellRangeForBudgetLine')
for node in self._getNodeList(budget_line)] def getCellRangeForBudgetLine(self, budget_line, matrixbox=0):
"""The cell range added by this variation
"""
node_item_list = self._getNodeItemList(budget_line, is_right_display=True)
variation_category_list = budget_line.getVariationCategoryList() variation_category_list = budget_line.getVariationCategoryList()
if matrixbox: if matrixbox:
return [[i for i in node_item_list if i[0] in variation_category_list]] return [[i for i in node_item_list if i[0] in variation_category_list]]
...@@ -115,15 +130,12 @@ class NodeBudgetVariation(BudgetVariation): ...@@ -115,15 +130,12 @@ class NodeBudgetVariation(BudgetVariation):
cell_range = self.getCellRangeForBudgetLine(budget_line, matrixbox) cell_range = self.getCellRangeForBudgetLine(budget_line, matrixbox)
if not self.getProperty('full_consumption_detail'): if not self.getProperty('full_consumption_detail'):
return cell_range return cell_range
base_category = self.getProperty('variation_base_category') base_category = self.getProperty('variation_base_category')
prefix = '' prefix = ''
if base_category: if base_category:
prefix = '%s/' % base_category prefix = '%s/' % base_category
node_item_list = [('%s%s' % (prefix, node.getRelativeUrl()), node_item_list = self._getNodeItemList(budget_line, is_right_display=True)
self._getNodeTitle(node))
for node in self._getNodeList(budget_line)]
if matrixbox: if matrixbox:
used_node_item_set = {item[0] for item in cell_range[0]} used_node_item_set = {item[0] for item in cell_range[0]}
else: else:
...@@ -325,12 +337,7 @@ class NodeBudgetVariation(BudgetVariation): ...@@ -325,12 +337,7 @@ class NodeBudgetVariation(BudgetVariation):
"""Returns the Variation Range Category List that can be applied to this """Returns the Variation Range Category List that can be applied to this
budget line. budget line.
""" """
base_category = self.getProperty('variation_base_category') return self._getNodeItemList(budget_line)
prefix = ''
if base_category:
prefix = '%s/' % base_category
return [(self._getNodeTitle(node), '%s%s' % (prefix, node.getRelativeUrl()))
for node in self._getNodeList(budget_line)]
security.declareProtected(Permissions.AccessContentsInformation, security.declareProtected(Permissions.AccessContentsInformation,
'getBudgetVariationRangeCategoryList') 'getBudgetVariationRangeCategoryList')
...@@ -338,12 +345,7 @@ class NodeBudgetVariation(BudgetVariation): ...@@ -338,12 +345,7 @@ class NodeBudgetVariation(BudgetVariation):
"""Returns the Variation Range Category List that can be applied to this """Returns the Variation Range Category List that can be applied to this
budget. budget.
""" """
base_category = self.getProperty('variation_base_category') return self._getNodeItemList(budget)
prefix = ''
if base_category:
prefix = '%s/' % base_category
return [(self._getNodeTitle(node), '%s%s' % (prefix, node.getRelativeUrl()))
for node in self._getNodeList(budget)]
security.declareProtected(Permissions.ModifyPortalContent, security.declareProtected(Permissions.ModifyPortalContent,
'initializeBudgetLine') 'initializeBudgetLine')
......
...@@ -156,6 +156,72 @@ class TestBudget(ERP5TypeTestCase): ...@@ -156,6 +156,72 @@ class TestBudget(ERP5TypeTestCase):
# there is no budget transfer # there is no budget transfer
self.assertEqual(5, budget_cell.getCurrentBalance()) self.assertEqual(5, budget_cell.getCurrentBalance())
def test_budget_cell_node_variation_with_aggregate_using_category(self):
budget_model = self.portal.budget_model_module.newContent(
portal_type='Budget Model')
budget_model.newContent(
portal_type='Node Budget Variation',
int_index=1,
budget_variation='budget_cell',
inventory_axis='node_category',
variation_base_category='account_type',
aggregate_value_list=(
self.portal.portal_categories.account_type.expense,
self.portal.portal_categories.account_type.income,
))
budget = self.portal.budget_module.newContent(
portal_type='Budget',
specialise_value=budget_model)
budget_line = budget.newContent(portal_type='Budget Line')
self.assertEqual(['account_type'],
budget_line.getVariationBaseCategoryList())
self.assertEqual(
[('Expense', 'account_type/expense'),
('Income', 'account_type/income')],
budget_line.BudgetLine_getVariationRangeCategoryList())
budget_line.setVariationCategoryList(
('account_type/expense',))
self.assertEqual(
['account_type/expense'],
budget_line.getVariationCategoryList())
# This was a budget cell variation, so no criterion is set on budget line
self.assertEqual(budget_line.getMembershipCriterionCategoryList(), [])
self.assertEqual(
budget_line.getMembershipCriterionBaseCategoryList(), [])
# simuate a request and call Base_edit, which does all the work of creating
# cell and setting cell properties.
form = budget_line.BudgetLine_view
self.portal.REQUEST.other.update(
dict(AUTHENTICATED_USER=getSecurityManager().getUser(),
field_membership_criterion_base_category_list=
form.membership_criterion_base_category_list.get_value('default'),
field_mapped_value_property_list=
form.mapped_value_property_list.get_value('default'),
field_matrixbox_quantity_cell_0_0_0="5",
field_matrixbox_membership_criterion_category_list_cell_0_0_0=[
'account_type/expense'],
))
budget_line.Base_edit(form_id=form.getId())
self.assertEqual(1, len(budget_line.contentValues()))
budget_cell = budget_line.getCell('account_type/expense')
self.assertNotEqual(None, budget_cell)
self.assertEqual(['account_type/expense'],
budget_cell.getMembershipCriterionCategoryList())
self.assertEqual(5, budget_cell.getQuantity())
# there is no budget consumption
self.assertEqual(0, budget_cell.getConsumedBudget())
self.assertEqual(0, budget_cell.getEngagedBudget())
self.assertEqual(5, budget_cell.getAvailableBudget())
# there is no budget transfer
self.assertEqual(5, budget_cell.getCurrentBalance())
def test_category_budget_cell_variation(self): def test_category_budget_cell_variation(self):
budget_model = self.portal.budget_model_module.newContent( budget_model = self.portal.budget_model_module.newContent(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment