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

Support related keys in group_by= parameters.

If group_by= is passed, default group by parameters will not be applied.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@36232 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent cf8b3f2a
......@@ -426,15 +426,24 @@ class SimulationTool(BaseTool):
# Some columns cannot be found automatically, prepend table name to
# avoid ambiguities.
# Group-by expression
# Group-by expression (eg. group_by=['node_uid'])
group_by = new_kw.pop('group_by', [])
column_group_by = new_kw.pop('column_group_by', [])
# group by from stock table (eg. group_by_node=True)
column_group_by = new_kw.pop('column_group_by', [])
if column_group_by:
group_by.extend(['%s.%s' % (table, x) for x in column_group_by])
# group by from related keys columns (eg. group_by_node_category=True)
related_key_group_by = new_kw.pop('related_key_group_by', [])
if related_key_group_by:
group_by.extend(['%s_%s' % (table, x) for x in related_key_group_by])
# group by involving a related key (eg. group_by=['product_line_uid'])
related_key_dict_passthrough_group_by = new_kw.get(
'related_key_dict_passthrough', dict()).pop('group_by', [])
group_by.extend(related_key_dict_passthrough_group_by)
if group_by:
new_kw['group_by'] = group_by
......@@ -962,6 +971,7 @@ class SimulationTool(BaseTool):
group_by_section_category_strict_membership=0,
group_by_resource=None,
movement_list_mode=0,
group_by=None,
**ignored):
"""
Set defaults group_by parameters
......@@ -979,7 +989,7 @@ class SimulationTool(BaseTool):
group by statements in SQL).
"""
new_group_by_dict = {}
if not ignore_group_by:
if not ignore_group_by and group_by is None:
if group_by_node or group_by_mirror_node or group_by_section or \
group_by_project or group_by_function or \
group_by_mirror_section or group_by_payment or \
......
......@@ -187,6 +187,7 @@ class InventoryAPITestCase(ERP5TypeTestCase):
'group/level1/level2',
'group/anotherlevel',
'product_line/level1/level2',
'product_line/anotherlevel',
'function/function1',
'function/function1/function2',
# we create a huge group category for consolidation tests
......@@ -877,6 +878,37 @@ class TestInventoryList(InventoryAPITestCase):
self.assertEquals([r for r in inventory_list
if r.date.year() == 2001][0].inventory, 1)
def test_GroupByRelatedKey(self):
getInventoryList = self.getSimulationTool().getInventoryList
self._makeMovement(quantity=2, product_line='level1')
self._makeMovement(quantity=3, product_line='level1',
destination_value=self.other_node)
self._makeMovement(quantity=11, product_line='anotherlevel')
# note that grouping by related key only make sense if you group by strict
# memebership related keys
inventory_list = getInventoryList(node_uid=(self.node.getUid(),
self.other_node.getUid()),
ignore_group_by=True,
group_by=('strict_product_line_uid',))
self.assertEquals(2, len(inventory_list))
self.assertEquals([r for r in inventory_list
if r.getObject().getProductLine() == 'level1'][0].inventory, 5)
self.assertEquals([r for r in inventory_list
if r.getObject().getProductLine() == 'anotherlevel'][0].inventory, 11)
inventory_list = getInventoryList(node_uid=(self.node.getUid(),
self.other_node.getUid()),
group_by_node=True,
group_by=('strict_product_line_uid',))
self.assertEquals(3, len(inventory_list))
self.assertEquals([r for r in inventory_list
if r.getObject().getProductLine() == 'level1'
and r.node_uid == self.node.getUid()][0].inventory, 2)
self.assertEquals([r for r in inventory_list
if r.getObject().getProductLine() == 'level1'
and r.node_uid == self.other_node.getUid()][0].inventory, 3)
self.assertEquals([r for r in inventory_list
if r.getObject().getProductLine() == 'anotherlevel'][0].inventory, 11)
def test_OmitInputOmitOutput(self):
getInventoryList = self.getSimulationTool().getInventoryList
......@@ -1719,8 +1751,8 @@ class TestMovementHistoryList(InventoryAPITestCase):
# group by explanation and resource
mvt_history_list = getMovementHistoryList(node_uid=self.node.getUid(),
group_by=('explanation_uid',
'resource_uid'),
group_by_resource=True,
group_by=('explanation_uid',),
sort_on=(('stock.date', 'ASC'),))
self.assertEquals(2, len(mvt_history_list))
......
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