Commit 0ea27f70 authored by Jérome Perrin's avatar Jérome Perrin

Item_getTrackingList assorted cleanups

Several changes to `Item_getTrackingList`.

My original goal was to fix a problem happening when users sort the listbox by `source_title` column. Because catalog understands it, it adds a join condition which would filter movements not "directly" having a source (ie. having a source by acquisition from parent). 

Then I realized this script was doing a lots of catalog queries that are done in a most efficient/safe way on the brain, so I switched to this.

Also add a few missing tests.

I did not add test for my original problem, because the real fix is that listbox should not declare `source_title` to be a sortable column, because this method does not support it.


/reviewed-on !469
parents af42987d 322e5e69
from Products.ERP5Type.Document import newTempBase from Products.ERP5Type.Document import newTempBase
portal = context.getPortalObject() portal = context.getPortalObject()
catalog = portal.portal_catalog.getResultValue
if current: if current:
method = portal.portal_simulation.getCurrentTrackingList method = portal.portal_simulation.getCurrentTrackingList
else: else:
method = portal.portal_simulation.getTrackingList method = portal.portal_simulation.getTrackingList
uid = context.getUid() # tracking list is much more readable if locations are sorted by dates.
kw.setdefault('sort_on', (('date', 'ascending',),))
history_list = [] history_list = []
for brain in method(aggregate_uid=context.getUid(), **kw):
for res in method(aggregate_uid=uid, **kw):
history = newTempBase(context, str(len(history_list))) history = newTempBase(context, str(len(history_list)))
explanation = catalog(uid=res.delivery_uid) explanation = brain.getExplanationValue()
node_value = catalog(uid=res.node_uid) date = brain.getDate()
section_value = catalog(uid=res.section_uid)
resource_value = catalog(uid=res.resource_uid)
history.edit( history.edit(
#uid = catalog(uid=res.uid).getTitle(), date=date,
date=res.getDate(), node_title=brain.node_title,
node_title=node_value is not None and node_value.getTitle() or None,
source_title=explanation.getSourceTitle(), source_title=explanation.getSourceTitle(),
section_title=section_value is not None and section_value.getTitle() or None, section_title=brain.section_title,
resource_title=resource_value is not None and resource_value.getTitle() or None, resource_title=brain.resource_title,
explanation=explanation.getTitle(), explanation=explanation.getTitle(),
translated_portal_type = explanation.getTranslatedPortalType(), translated_portal_type=explanation.getTranslatedPortalType(),
quantity = explanation.getQuantity(), quantity=explanation.getQuantity(),
url=explanation.absolute_url(), url=explanation.absolute_url(),
item_quantity = context.getQuantity(at_date=res.getDate()), # item_quantity=context.getQuantity(at_date=date),
variation_category_item_list = [x[0] for x in explanation.getVariationCategoryItemList()], variation_category_item_list=[x[0] for x in explanation.getVariationCategoryItemList()],
simulation_state=explanation.getTranslatedSimulationStateTitle(), simulation_state=explanation.getTranslatedSimulationStateTitle(),
) )
history_list.append(history) history_list.append(history)
return history_list return history_list
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
<list> <list>
<string>columns</string> <string>columns</string>
<string>list_method</string> <string>list_method</string>
<string>sort</string> <string>sort_columns</string>
<string>title</string> <string>title</string>
<string>url_columns</string> <string>url_columns</string>
</list> </list>
...@@ -137,12 +137,20 @@ ...@@ -137,12 +137,20 @@
</value> </value>
</item> </item>
<item> <item>
<key> <string>sort</string> </key> <key> <string>sort_columns</string> </key>
<value> <value>
<list> <list>
<tuple>
<string>translated_portal_type</string>
<string>Type</string>
</tuple>
<tuple> <tuple>
<string>date</string> <string>date</string>
<string>ascending</string> <string>Date</string>
</tuple>
<tuple>
<string>simulation_state</string>
<string>State</string>
</tuple> </tuple>
</list> </list>
</value> </value>
......
...@@ -288,6 +288,9 @@ class TrackingListBrain(InventoryListBrain): ...@@ -288,6 +288,9 @@ class TrackingListBrain(InventoryListBrain):
""" """
List of aggregated movements List of aggregated movements
""" """
def getExplanationValue(self):
return self._getObjectByUid(self.delivery_uid)
def getDate(self): def getDate(self):
if not self.date: if not self.date:
return return
...@@ -296,8 +299,7 @@ class TrackingListBrain(InventoryListBrain): ...@@ -296,8 +299,7 @@ class TrackingListBrain(InventoryListBrain):
# the brain is accessed from the Shared.DC.ZRDB.Results.Results instance # the brain is accessed from the Shared.DC.ZRDB.Results.Results instance
obj = self.getObject() obj = self.getObject()
if obj is not None: if obj is not None:
portal = obj.getPortalObject() movement = self._getObjectByUid(self.delivery_uid)
movement = portal.portal_catalog.getObject(self.delivery_uid)
date = movement.getStartDate() or movement.getStopDate() date = movement.getStartDate() or movement.getStopDate()
if date is not None: if date is not None:
timezone = date.timezone() timezone = date.timezone()
......
...@@ -1033,7 +1033,9 @@ class TestItemScripts(ERP5TypeTestCase): ...@@ -1033,7 +1033,9 @@ class TestItemScripts(ERP5TypeTestCase):
self.tic() self.tic()
@reindex @reindex
def _makeSalePackingListLine(self): def _makeSalePackingListLine(self, start_date=None):
if start_date is None:
start_date = DateTime() - 1
packing_list = self.portal.sale_packing_list_module.newContent( packing_list = self.portal.sale_packing_list_module.newContent(
portal_type='Sale Packing List', portal_type='Sale Packing List',
source_value=self.mirror_node, source_value=self.mirror_node,
...@@ -1041,7 +1043,7 @@ class TestItemScripts(ERP5TypeTestCase): ...@@ -1041,7 +1043,7 @@ class TestItemScripts(ERP5TypeTestCase):
destination_value=self.node, destination_value=self.node,
destination_section_value=self.section, destination_section_value=self.section,
specialise_value=self.portal.business_process_module.erp5_default_business_process, specialise_value=self.portal.business_process_module.erp5_default_business_process,
start_date=DateTime() - 1,) start_date=start_date,)
line = packing_list.newContent( line = packing_list.newContent(
portal_type='Sale Packing List Line', portal_type='Sale Packing List Line',
quantity=1, quantity=1,
...@@ -1095,6 +1097,46 @@ class TestItemScripts(ERP5TypeTestCase): ...@@ -1095,6 +1097,46 @@ class TestItemScripts(ERP5TypeTestCase):
self.assertEqual(None, self.assertEqual(None,
self.item.Item_getCurrentSiteTitle(at_date=DateTime() - 2)) self.item.Item_getCurrentSiteTitle(at_date=DateTime() - 2))
def test_Item_getTrackingList_empty(self):
self.assertEqual([], self.item.Item_getTrackingList())
def test_Item_getTrackingList_explanation_brain_attribute(self):
line = self._makeSalePackingListLine(start_date=DateTime(2001, 2, 3))
line.setTitle('explanation title')
self.tic()
history_item, = self.item.Item_getTrackingList()
self.assertEqual(DateTime(2001, 2, 3), history_item.date)
self.assertEqual('Node', history_item.node_title)
self.assertEqual('Mirror Node', history_item.source_title)
self.assertEqual('Section', history_item.section_title)
self.assertEqual('Product', history_item.resource_title)
self.assertEqual('explanation title', history_item.explanation)
self.assertEqual('Sale Packing List Line', history_item.translated_portal_type)
self.assertEqual(1, history_item.quantity)
self.assertEqual(line.absolute_url(), history_item.url)
self.assertEqual((), history_item.variation_category_item_list)
self.assertEqual('Delivered', history_item.simulation_state)
def test_Item_getTrackingList_default_sort(self):
# Item_getTrackingList returns lines sorted in chronological order
implicit_movement = self.portal.implicit_item_movement_module.newContent(
portal_type='Implicit Item Movement',
destination_value=self.mirror_node,
destination_section_value=self.mirror_section,
stop_date=DateTime(2016, 1, 1),
aggregate_value=self.item,
)
implicit_movement.deliver()
self._makeSalePackingListLine(start_date=DateTime(2017, 1, 1))
self.assertEqual(
[DateTime(2016, 1, 1), DateTime(2017, 1, 1)],
[brain.date for brain in self.item.Item_getTrackingList()])
self.assertEqual(
['Mirror Node', 'Node'],
[brain.node_title for brain in self.item.Item_getTrackingList()])
def test_item_current_location_and_transit_movement(self): def test_item_current_location_and_transit_movement(self):
# a started packing list is still in transit, so we do not know its # a started packing list is still in transit, so we do not know its
# current location until it is delivered. # current location until it is delivered.
......
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