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

Inventory API: reimplement variation_category

In its current implementation ( introduced in  4787eadd ), `variation_category` parameter of Inventory API require movements to be in the `predicate_category` table. The approach is to do a first query to get uids of all movements members of the variation and pass their `uid` to catalog. My guess is that this does not scale well. It also uses deprecated `where_expression` and generates invalid SQL when no movements match.

As far as I know, it is only used in [testInventory](https://lab.nexedi.com/nexedi/erp5/blob/ba541ad46e5c593f94fe1ebe79e99d67aafdecb1/product/ERP5/tests/testInventory.py#L1024). We could also consider dropping it, but keeping compatibility is easy.

This implementation just use `variation_text`.

[Test results](https://nexedi.erp5.net/test_result_module/20170502-1FDBAB66) are OK; there is one functional test failure but it really looks unrelated.

@yusei @vpelletier  @seb @romain do you see problems with this ?

/reviewed-on nexedi/erp5!265
parents 17fd0046 9beac557
...@@ -678,6 +678,24 @@ class SimulationTool(BaseTool): ...@@ -678,6 +678,24 @@ class SimulationTool(BaseTool):
column_value_dict.setUIDList('section_uid', section) column_value_dict.setUIDList('section_uid', section)
column_value_dict.setUIDList('mirror_section_uid', mirror_section) column_value_dict.setUIDList('mirror_section_uid', mirror_section)
# Handle variation_category as variation_text
if variation_category:
if variation_text:
raise ValueError(
"Passing both variation_category and variation_text is not supported")
warn("variation_category is deprecated, please use variation_text instead",
DeprecationWarning)
if isinstance(variation_category, basestring):
variation_category = (variation_category,)
# variation text is a \n separated list of variation categories, but without
# trailing nor leading \n
variation_text = [
"{}\n%".format(x) for x in variation_category] + [
"%\n{}\n%".format(x) for x in variation_category] + [
"%\n{}".format(x) for x in variation_category] + [
"{}".format(x) for x in variation_category]
column_value_dict.setUIDList('variation_text', variation_text, column_value_dict.setUIDList('variation_text', variation_text,
as_text=1) as_text=1)
column_value_dict.setUIDList('sub_variation_text', sub_variation_text, column_value_dict.setUIDList('sub_variation_text', sub_variation_text,
...@@ -763,10 +781,6 @@ class SimulationTool(BaseTool): ...@@ -763,10 +781,6 @@ class SimulationTool(BaseTool):
if related_group_by: if related_group_by:
new_kw['related_key_dict_passthrough']['group_by_list'] = related_group_by new_kw['related_key_dict_passthrough']['group_by_list'] = related_group_by
#variation_category_uid_list = self._generatePropertyUidList(variation_category)
#if len(variation_category_uid_list) :
# new_kw['variationCategory'] = variation_category_uid_list
new_kw['simulation_dict'] = self._getSimulationStateDict( new_kw['simulation_dict'] = self._getSimulationStateDict(
simulation_state=simulation_state, simulation_state=simulation_state,
omit_transit=omit_transit, omit_transit=omit_transit,
...@@ -793,28 +807,6 @@ class SimulationTool(BaseTool): ...@@ -793,28 +807,6 @@ class SimulationTool(BaseTool):
}, },
'simulation_dict': self._getSimulationStateDict(**reserved_kw), 'simulation_dict': self._getSimulationStateDict(**reserved_kw),
} }
# It is necessary to use here another SQL query (or at least a subquery)
# to get _DISTINCT_ uid from predicate_category table.
# Otherwise, by using a where_expression, cells which fit conditions
# more than one time are counted more than one time, and the resulting
# inventory is false
# XXX Perhaps is there a better solution
add_kw = {}
if variation_category is not None and variation_category:
where_expression = self.getPortalObject().portal_categories\
.buildSQLSelector(
category_list = variation_category,
query_table = 'predicate_category')
if where_expression != '':
add_kw['where_expression'] = where_expression
add_kw['predicate_category.uid'] = '!=NULL'
add_kw['group_by'] = ['uid']
add_query = self.portal_catalog(**add_kw)
uid_list = []
for line in add_query:
uid_list.append(line.uid)
new_kw['where_expression'] = '( %s )' % ' OR '.join(
['catalog.uid=%s' % uid for uid in uid_list])
# build the group by expression # build the group by expression
# if we group by a criterion, we also add this criterion to the select # if we group by a criterion, we also add this criterion to the select
...@@ -1009,8 +1001,6 @@ class SimulationTool(BaseTool): ...@@ -1009,8 +1001,6 @@ class SimulationTool(BaseTool):
variation_text - only take rows in stock table with specified variation_text - only take rows in stock table with specified
variation_text. variation_text.
This needs to be extended with some kind of
variation_category ?
XXX this way of implementing variation selection is far XXX this way of implementing variation selection is far
from perfect from perfect
...@@ -1018,7 +1008,8 @@ class SimulationTool(BaseTool): ...@@ -1018,7 +1008,8 @@ class SimulationTool(BaseTool):
variation_text variation_text
variation_category - variation or list of possible variations (it is not variation_category - variation or list of possible variations (it is not
a cross-search ; SQL query uses OR) a cross-search ; SQL query uses OR).
Deprecated, use variation_text.
simulation_state - only take rows with specified simulation_state simulation_state - only take rows with specified simulation_state
...@@ -2136,10 +2127,10 @@ class SimulationTool(BaseTool): ...@@ -2136,10 +2127,10 @@ class SimulationTool(BaseTool):
section_category - only take rows in stock table which section_uid is in section_category section_category - only take rows in stock table which section_uid is in section_category
variation_text - only take rows in stock table with specified variation_text variation_text - only take rows in stock table with specified variation_text
this needs to be extended with some kind of variation_category ?
XXX this way of implementing variation selection is far from perfect XXX this way of implementing variation selection is far from perfect
variation_category - variation or list of possible variations variation_category - variation or list of possible variations
Deprecated, use variation_text
simulation_state - only take rows with specified simulation_state simulation_state - only take rows with specified simulation_state
......
...@@ -65,5 +65,4 @@ ...@@ -65,5 +65,4 @@
<key>translated_simulation_state_title | translation/translated_message/z_related_translated_simulation_state_title</key> <key>translated_simulation_state_title | translation/translated_message/z_related_translated_simulation_state_title</key>
<key>translated_validation_state | translation/translated_message/z_related_translated_validation_state</key> <key>translated_validation_state | translation/translated_message/z_related_translated_validation_state</key>
<key>translated_validation_state_title | translation/translated_message/z_related_translated_validation_state_title</key> <key>translated_validation_state_title | translation/translated_message/z_related_translated_validation_state_title</key>
<key>variationCategory | category/category_uid/z_related_uid</key>
</key_list> </key_list>
\ No newline at end of file
...@@ -41,7 +41,6 @@ item_section_category_uid | category/category_uid/z_related_section_uid_from_ite ...@@ -41,7 +41,6 @@ item_section_category_uid | category/category_uid/z_related_section_uid_from_ite
parent_relative_url | catalog/relative_url/z_related_parent parent_relative_url | catalog/relative_url/z_related_parent
resourceType | catalog/portal_type/z_related_resource_uid_from_stock resourceType | catalog/portal_type/z_related_resource_uid_from_stock
grand_parent_portal_type | catalog,catalog/portal_type/z_related_grand_parent grand_parent_portal_type | catalog,catalog/portal_type/z_related_grand_parent
variationCategory | category/category_uid/z_related_uid
translated_validation_state | translation/translated_message/z_related_translated_validation_state translated_validation_state | translation/translated_message/z_related_translated_validation_state
stock_resource_category_uid | category/category_uid/z_related_resource_uid_from_stock stock_resource_category_uid | category/category_uid/z_related_resource_uid_from_stock
parent_title | catalog/title/z_related_parent parent_title | catalog/title/z_related_parent
......
...@@ -37,5 +37,4 @@ ...@@ -37,5 +37,4 @@
<key>translated_simulation_state_title | translation/translated_message/z_related_translated_simulation_state_title</key> <key>translated_simulation_state_title | translation/translated_message/z_related_translated_simulation_state_title</key>
<key>translated_validation_state | translation/translated_message/z_related_translated_validation_state</key> <key>translated_validation_state | translation/translated_message/z_related_translated_validation_state</key>
<key>translated_validation_state_title | translation/translated_message/z_related_translated_validation_state_title</key> <key>translated_validation_state_title | translation/translated_message/z_related_translated_validation_state_title</key>
<key>variationCategory | category/category_uid/z_related_uid</key>
</key_list> </key_list>
\ No newline at end of file
...@@ -30,7 +30,6 @@ item_section_category_uid | category/category_uid/z_related_section_uid_from_ite ...@@ -30,7 +30,6 @@ item_section_category_uid | category/category_uid/z_related_section_uid_from_ite
parent_specialise_portal_type | category,catalog/portal_type/z_related_parent_specialise parent_specialise_portal_type | category,catalog/portal_type/z_related_parent_specialise
resourceType | movement,catalog/portal_type/z_related_resource_from_movement resourceType | movement,catalog/portal_type/z_related_resource_from_movement
grand_parent_portal_type | catalog,catalog/portal_type/z_related_grand_parent grand_parent_portal_type | catalog,catalog/portal_type/z_related_grand_parent
variationCategory | category/category_uid/z_related_uid
translated_validation_state | translation/translated_message/z_related_translated_validation_state translated_validation_state | translation/translated_message/z_related_translated_validation_state
stock_resource_category_uid | category/category_uid/z_related_resource_uid_from_stock stock_resource_category_uid | category/category_uid/z_related_resource_uid_from_stock
parent_title | catalog/title/z_related_parent parent_title | catalog/title/z_related_parent
......
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