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
portal = context.getPortalObject()
catalog = portal.portal_catalog.getResultValue
if current:
method = portal.portal_simulation.getCurrentTrackingList
else:
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 = []
for res in method(aggregate_uid=uid, **kw):
for brain in method(aggregate_uid=context.getUid(), **kw):
history = newTempBase(context, str(len(history_list)))
explanation = catalog(uid=res.delivery_uid)
node_value = catalog(uid=res.node_uid)
section_value = catalog(uid=res.section_uid)
resource_value = catalog(uid=res.resource_uid)
explanation = brain.getExplanationValue()
date = brain.getDate()
history.edit(
#uid = catalog(uid=res.uid).getTitle(),
date=res.getDate(),
node_title=node_value is not None and node_value.getTitle() or None,
date=date,
node_title=brain.node_title,
source_title=explanation.getSourceTitle(),
section_title=section_value is not None and section_value.getTitle() or None,
resource_title=resource_value is not None and resource_value.getTitle() or None,
section_title=brain.section_title,
resource_title=brain.resource_title,
explanation=explanation.getTitle(),
translated_portal_type = explanation.getTranslatedPortalType(),
quantity = explanation.getQuantity(),
translated_portal_type=explanation.getTranslatedPortalType(),
quantity=explanation.getQuantity(),
url=explanation.absolute_url(),
item_quantity = context.getQuantity(at_date=res.getDate()),
variation_category_item_list = [x[0] for x in explanation.getVariationCategoryItemList()],
# item_quantity=context.getQuantity(at_date=date),
variation_category_item_list=[x[0] for x in explanation.getVariationCategoryItemList()],
simulation_state=explanation.getTranslatedSimulationStateTitle(),
)
history_list.append(history)
return history_list
......@@ -12,7 +12,7 @@
<list>
<string>columns</string>
<string>list_method</string>
<string>sort</string>
<string>sort_columns</string>
<string>title</string>
<string>url_columns</string>
</list>
......@@ -137,12 +137,20 @@
</value>
</item>
<item>
<key> <string>sort</string> </key>
<key> <string>sort_columns</string> </key>
<value>
<list>
<tuple>
<string>translated_portal_type</string>
<string>Type</string>
</tuple>
<tuple>
<string>date</string>
<string>ascending</string>
<string>Date</string>
</tuple>
<tuple>
<string>simulation_state</string>
<string>State</string>
</tuple>
</list>
</value>
......
......@@ -288,6 +288,9 @@ class TrackingListBrain(InventoryListBrain):
"""
List of aggregated movements
"""
def getExplanationValue(self):
return self._getObjectByUid(self.delivery_uid)
def getDate(self):
if not self.date:
return
......@@ -296,8 +299,7 @@ class TrackingListBrain(InventoryListBrain):
# the brain is accessed from the Shared.DC.ZRDB.Results.Results instance
obj = self.getObject()
if obj is not None:
portal = obj.getPortalObject()
movement = portal.portal_catalog.getObject(self.delivery_uid)
movement = self._getObjectByUid(self.delivery_uid)
date = movement.getStartDate() or movement.getStopDate()
if date is not None:
timezone = date.timezone()
......
......@@ -1033,7 +1033,9 @@ class TestItemScripts(ERP5TypeTestCase):
self.tic()
@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(
portal_type='Sale Packing List',
source_value=self.mirror_node,
......@@ -1041,7 +1043,7 @@ class TestItemScripts(ERP5TypeTestCase):
destination_value=self.node,
destination_section_value=self.section,
specialise_value=self.portal.business_process_module.erp5_default_business_process,
start_date=DateTime() - 1,)
start_date=start_date,)
line = packing_list.newContent(
portal_type='Sale Packing List Line',
quantity=1,
......@@ -1095,6 +1097,46 @@ class TestItemScripts(ERP5TypeTestCase):
self.assertEqual(None,
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):
# a started packing list is still in transit, so we do not know its
# 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