Commit 0d699075 authored by Romain Courteaud's avatar Romain Courteaud

TransformedResource is now a subclass of Predicate (useful for optional lines).

So, I modified implementation of getAggregatedAmountList, to return nothing in
some cases (when test method failed on TransformedResource).


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@3804 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 9ae743a7
...@@ -40,10 +40,11 @@ from Products.ERP5.Document.Amount import Amount ...@@ -40,10 +40,11 @@ from Products.ERP5.Document.Amount import Amount
from Products.ERP5.Document.Transformation import AggregatedAmountList from Products.ERP5.Document.Transformation import AggregatedAmountList
from Products.CMFCore.Expression import Expression from Products.CMFCore.Expression import Expression
from Products.ERP5.Document.Predicate import Predicate
from zLOG import LOG from zLOG import LOG
class TransformedResource(XMLObject, XMLMatrix, Amount): class TransformedResource(Predicate, XMLObject, XMLMatrix, Amount):
""" """
TransformedResource defines which TransformedResource defines which
resource is being transformed resource is being transformed
...@@ -81,27 +82,32 @@ class TransformedResource(XMLObject, XMLMatrix, Amount): ...@@ -81,27 +82,32 @@ class TransformedResource(XMLObject, XMLMatrix, Amount):
### Variation matrix definition ### Variation matrix definition
# #
security.declareProtected(Permissions.AccessContentsInformation, 'updateVariationCategoryList') security.declareProtected(Permissions.AccessContentsInformation,
'updateVariationCategoryList')
def updateVariationCategoryList(self): def updateVariationCategoryList(self):
""" """
Check if variation category list of the resource changed and update transformed resource Check if variation category list of the resource changed and
by doing a set cell range update transformed resource by doing a set cell range
""" """
self.setQVariationBaseCategoryList( self.getQVariationBaseCategoryList() ) self.setQVariationBaseCategoryList(self.getQVariationBaseCategoryList())
self.setVVariationBaseCategoryList( self.getVVariationBaseCategoryList() ) self.setVVariationBaseCategoryList(self.getVVariationBaseCategoryList())
security.declareProtected(Permissions.ModifyPortalContent, '_updateQMatrixCellRange') security.declareProtected(Permissions.ModifyPortalContent,
'_updateQMatrixCellRange')
def _updateQMatrixCellRange(self): def _updateQMatrixCellRange(self):
# XXX use base_id parameter instead # XXX use base_id parameter instead
cell_range = self.TransformedResource_asCellRange('quantity') cell_range = self.TransformedResource_asCellRange('quantity')
# XXX TransformedResource works only for a maximum of 3 variation base category... # XXX TransformedResource works only for a maximum of 3 variation
# Matrixbox must be rewrite for a clean implementation of n base category # base category...
# Matrixbox must be rewrite for a clean implementation of n base
# category
if len(cell_range) <= 3: if len(cell_range) <= 3:
self.setCellRange(base_id='quantity', *cell_range) self.setCellRange(base_id='quantity', *cell_range)
else: else:
raise MoreThan3VariationBaseCategory raise MoreThan3VariationBaseCategory
security.declareProtected(Permissions.ModifyPortalContent, '_setQVariationBaseCategoryList') security.declareProtected(Permissions.ModifyPortalContent,
'_setQVariationBaseCategoryList')
def _setQVariationBaseCategoryList(self, value): def _setQVariationBaseCategoryList(self, value):
""" """
Defines the possible base categories which Quantity value (Q) Defines the possible base categories which Quantity value (Q)
...@@ -111,7 +117,8 @@ class TransformedResource(XMLObject, XMLMatrix, Amount): ...@@ -111,7 +117,8 @@ class TransformedResource(XMLObject, XMLMatrix, Amount):
# XXX calling updatecellRange is better # XXX calling updatecellRange is better
self._updateQMatrixCellRange() self._updateQMatrixCellRange()
security.declareProtected(Permissions.ModifyPortalContent, 'setQVariationBaseCategoryList') security.declareProtected(Permissions.ModifyPortalContent,
'setQVariationBaseCategoryList')
def setQVariationBaseCategoryList(self, value): def setQVariationBaseCategoryList(self, value):
""" """
Defines the possible base categories which Quantity value (Q) Defines the possible base categories which Quantity value (Q)
...@@ -120,18 +127,22 @@ class TransformedResource(XMLObject, XMLMatrix, Amount): ...@@ -120,18 +127,22 @@ class TransformedResource(XMLObject, XMLMatrix, Amount):
self._setQVariationBaseCategoryList(value) self._setQVariationBaseCategoryList(value)
self.reindexObject() self.reindexObject()
security.declareProtected(Permissions.ModifyPortalContent, '_updateVMatrixCellRange') security.declareProtected(Permissions.ModifyPortalContent,
'_updateVMatrixCellRange')
def _updateVMatrixCellRange(self): def _updateVMatrixCellRange(self):
# XXX use base_id parameter instead # XXX use base_id parameter instead
cell_range = self.TransformedResource_asCellRange('variation') cell_range = self.TransformedResource_asCellRange('variation')
# XXX TransformedResource works only for a maximum of 3 variation base category... # XXX TransformedResource works only for a maximum of 3 variation
# Matrixbox must be rewrite for a clean implementation of n base category # base category...
# Matrixbox must be rewrite for a clean implementation of n base
# category
if len(cell_range) <= 3: if len(cell_range) <= 3:
self.setCellRange(base_id='variation', *cell_range) self.setCellRange(base_id='variation', *cell_range)
else: else:
raise MoreThan3VariationBaseCategory raise MoreThan3VariationBaseCategory
security.declareProtected(Permissions.ModifyPortalContent, '_setVVariationBaseCategoryList') security.declareProtected(Permissions.ModifyPortalContent,
'_setVVariationBaseCategoryList')
def _setVVariationBaseCategoryList(self, value): def _setVVariationBaseCategoryList(self, value):
""" """
Defines the possible base categories which Variation value (V) Defines the possible base categories which Variation value (V)
...@@ -141,7 +152,8 @@ class TransformedResource(XMLObject, XMLMatrix, Amount): ...@@ -141,7 +152,8 @@ class TransformedResource(XMLObject, XMLMatrix, Amount):
# XXX calling updatecellRange is better # XXX calling updatecellRange is better
self._updateVMatrixCellRange() self._updateVMatrixCellRange()
security.declareProtected(Permissions.ModifyPortalContent, 'setVVariationBaseCategoryList') security.declareProtected(Permissions.ModifyPortalContent,
'setVVariationBaseCategoryList')
def setVVariationBaseCategoryList(self, value): def setVVariationBaseCategoryList(self, value):
""" """
Defines the possible base categories which Variation value (V) Defines the possible base categories which Variation value (V)
...@@ -151,7 +163,8 @@ class TransformedResource(XMLObject, XMLMatrix, Amount): ...@@ -151,7 +163,8 @@ class TransformedResource(XMLObject, XMLMatrix, Amount):
self.reindexObject() self.reindexObject()
security.declareProtected(Permissions.AccessContentsInformation,'getVariationRangeCategoryItemList') security.declareProtected(Permissions.AccessContentsInformation,
'getVariationRangeCategoryItemList')
def getVariationRangeCategoryItemList(self, base_category_list = (), def getVariationRangeCategoryItemList(self, base_category_list = (),
omit_individual_variation=1, base=1, omit_individual_variation=1, base=1,
current_category=None, current_category=None,
...@@ -172,7 +185,6 @@ class TransformedResource(XMLObject, XMLMatrix, Amount): ...@@ -172,7 +185,6 @@ class TransformedResource(XMLObject, XMLMatrix, Amount):
if resource != None: if resource != None:
if base_category_list is (): if base_category_list is ():
base_category_list = resource.getVariationBaseCategoryList() base_category_list = resource.getVariationBaseCategoryList()
result = resource.getVariationCategoryItemList( result = resource.getVariationCategoryItemList(
base_category_list=base_category_list, base_category_list=base_category_list,
omit_individual_variation=0, omit_individual_variation=0,
...@@ -181,10 +193,10 @@ class TransformedResource(XMLObject, XMLMatrix, Amount): ...@@ -181,10 +193,10 @@ class TransformedResource(XMLObject, XMLMatrix, Amount):
display_base_category=display_base_category, display_base_category=display_base_category,
display_id=display_id, display_id=display_id,
**kw) **kw)
return result return result
security.declareProtected(Permissions.AccessContentsInformation,'getVariationRangeCategoryItemList') security.declareProtected(Permissions.AccessContentsInformation,
'getVariationRangeCategoryItemList')
def getVariationRangeCategoryList(self, base_category_list=()): def getVariationRangeCategoryList(self, base_category_list=()):
""" """
Returns possible variation category values for the Returns possible variation category values for the
...@@ -195,7 +207,8 @@ class TransformedResource(XMLObject, XMLMatrix, Amount): ...@@ -195,7 +207,8 @@ class TransformedResource(XMLObject, XMLMatrix, Amount):
base_category_list=base_category_list)) base_category_list=base_category_list))
return result return result
security.declareProtected(Permissions.AccessContentsInformation, 'getAggregatedAmountList') security.declareProtected(Permissions.AccessContentsInformation,
'getAggregatedAmountList')
def getAggregatedAmountList(self, context=None, REQUEST=None, **kw): def getAggregatedAmountList(self, context=None, REQUEST=None, **kw):
""" """
Get all interesting amount value and return AggregatedAmountList Get all interesting amount value and return AggregatedAmountList
...@@ -203,98 +216,97 @@ class TransformedResource(XMLObject, XMLMatrix, Amount): ...@@ -203,98 +216,97 @@ class TransformedResource(XMLObject, XMLMatrix, Amount):
context = self.asContext(context=context, REQUEST=REQUEST, **kw) context = self.asContext(context=context, REQUEST=REQUEST, **kw)
# Create the result object # Create the result object
aggregated_amount_list = AggregatedAmountList() aggregated_amount_list = AggregatedAmountList()
test_result = self.test(context)
if test_result:
# The line must match the context
# If no predicate is defined on line, the result of the test
# must be true
# Create temporary object to store amount # Create temporary object to store amount
from Products.ERP5Type.Document import newTempAmount from Products.ERP5Type.Document import newTempAmount
tmp_amount = newTempAmount(self.getPortalObject(), self.getId()) tmp_amount = newTempAmount(self.getPortalObject(), self.getId())
# Create error string
error_string = '' error_string = ''
# Add resource relation
# add resource relation
resource = self.getDefaultResourceValue() resource = self.getDefaultResourceValue()
if resource != None: if resource != None:
tmp_amount.setResourceValue(resource) tmp_amount.setResourceValue(resource)
else: else:
error_string += 'No resource defined on %s' % self.getRelativeUrl() error_string += 'No resource defined on %s' % self.getRelativeUrl()
# First, we set initial values for quantity and variation # First, we set initial values for quantity and variation
# Currently, we only consider discrete variations # Currently, we only consider discrete variations
# Continuous variations will be implemented in a future version of ERP5 # Continuous variations will be implemented in a future version
# of ERP5
# Set quantity unit
quantity_unit = self.getQuantityUnit() quantity_unit = self.getQuantityUnit()
if quantity_unit is not None: if quantity_unit is not None:
tmp_amount.setQuantityUnitValue(quantity_unit) tmp_amount.setQuantityUnitValue(quantity_unit)
# Set efficiency
efficiency = self.getEfficiency() efficiency = self.getEfficiency()
if efficiency is None or efficiency is '' or efficiency == 0.0: if efficiency is None or efficiency is '' or efficiency == 0.0:
efficiency = 1.0 efficiency = 1.0
else: else:
efficiency = float(efficiency) efficiency = float(efficiency)
### current get quantity comportment exemple ###
### current get quantity comportment exemple ### # We define on transformation line:
# We define on transformation line: # default_quantity = q
# default_quantity = q # quantity matrix
# quantity matrix # | Child | Child/32 | Child/34 | Men | Women |
# | Child | Child/32 | Child/34 | Men | Women | # | a | | b | c | |
# | a | | b | c | | # Result from getAggregatedAmountList:
# Result from getAggregatedAmountList: # context | quantity
# context | quantity # _________________________
# _________________________ # Child | a
# Child | a # Child/32 | a => acquired from Child
# Child/32 | a => acquired from Child # Child/34 | a or b => we do not know which cell will be choosed
# Child/34 | a or b => we do not know which cell will be choosed # Child/36 | a => acquired from Child
# Child/36 | a => acquired from Child # Men | c
# Men | c # Women | Error => no cell found
# Women | Error => no cell found # noContext | Error => cell exist, but no context given
# noContext | Error => cell exist, but no context given
### comportment that JPS want ?? ###
### comportment that JPS want ?? ### # We define on transformation line:
# We define on transformation line: # default_quantity = q
# default_quantity = q # quantity matrix
# quantity matrix # | Child | Child/32 | Child/34 | Men | Women |
# | Child | Child/32 | Child/34 | Men | Women | # | a | | b | c | |
# | a | | b | c | | # Result from getAggregatedAmountList:
# Result from getAggregatedAmountList: # context | quantity
# context | quantity # _________________________
# _________________________ # Child | a
# Child | a # Child/32 | a => acquired from Child
# Child/32 | a => acquired from Child # Child/34 | a or b => we do not know which cell will be choosed
# Child/34 | a or b => we do not know which cell will be choosed # Child/36 | Error => no such key in matrixbox cell range
# Child/36 | Error => no such key in matrixbox cell range # Men | c
# Men | c # Women | Error => no cell found
# Women | Error => no cell found # noContext | Error => cell exist, but no context given
# noContext | Error => cell exist, but no context given
# futur cool get quantity comportment exemple
# futur cool get quantity comportment exemple # We define on transformation line:
# We define on transformation line: # default_quantity = q
# default_quantity = q # quantity matrix
# quantity matrix # | Child | Child/32 | Child/34 | Men | Women |
# | Child | Child/32 | Child/34 | Men | Women | # | a | | b | c | |
# | a | | b | c | | # Result from getAggregatedAmountList:
# Result from getAggregatedAmountList: # context | quantity
# context | quantity # _________________________
# _________________________ # Child | a
# Child | a # Child/32 | a => acquired from Child
# Child/32 | a => acquired from Child # Child/34 | b => test method must return a priority to choose between Child and Child/34
# Child/34 | b => test method must return a priority to choose between Child and Child/34 # Child/36 | Error => no such key in matrixbox cell range
# Child/36 | Error => no such key in matrixbox cell range # Men | c
# Men | c # Women | q => acquired from default quantity
# Women | q => acquired from default quantity # noContext | q => acquired from default quantity
# noContext | q => acquired from default quantity
quantity_defined_by = None
# get Quantity # get Quantity
quantity_defined_by = None
quantity = None quantity = None
# We will browse the mapped values and determine which apply # We will browse the mapped values and determine which apply
cell_key_list = self.getCellKeyList( base_id = 'quantity') cell_key_list = self.getCellKeyList(base_id='quantity')
if cell_key_list not in [(),[]]: if cell_key_list not in [(),[]]:
if context == None: if context == None:
raise KeyError, "No context defined on TransformedResource '%s'" % ( self.getRelativeUrl() , ) raise KeyError, \
"No context defined on TransformedResource '%s'" % \
(self.getRelativeUrl(), )
for key in cell_key_list: for key in cell_key_list:
if self.hasCell(base_id='quantity', *key): if self.hasCell(base_id='quantity', *key):
mapped_value = self.getCell(base_id='quantity', *key) mapped_value = self.getCell(base_id='quantity', *key)
...@@ -302,71 +314,64 @@ class TransformedResource(XMLObject, XMLMatrix, Amount): ...@@ -302,71 +314,64 @@ class TransformedResource(XMLObject, XMLMatrix, Amount):
if 'quantity' in mapped_value.getMappedValuePropertyList(): if 'quantity' in mapped_value.getMappedValuePropertyList():
quantity = mapped_value.getProperty('quantity') quantity = mapped_value.getProperty('quantity')
quantity_defined_by = mapped_value.getRelativeUrl() quantity_defined_by = mapped_value.getRelativeUrl()
if quantity in [None,'']: if quantity in [None,'']:
raise KeyError, "No cell quantity matching on TransformedResource '%s' for current context" % ( self.getRelativeUrl() , ) raise KeyError, \
"No cell quantity matching on TransformedResource '%s' for \
current context" % ( self.getRelativeUrl() , )
else: else:
quantity = self.getQuantity() quantity = self.getQuantity()
quantity_defined_by = self.getRelativeUrl() quantity_defined_by = self.getRelativeUrl()
if quantity in [None,'']: if quantity in [None,'']:
raise KeyError, "No quantity defined on TransformedResource '%s' for current context" % ( self.getRelativeUrl() , ) raise KeyError, \
"No quantity defined on TransformedResource '%s' for \
current context" % (self.getRelativeUrl(), )
# If we have to do this, then there is a problem.... # If we have to do this, then there is a problem....
# We'd better have better API for this, like an update function in the mapped_value # We'd better have better API for this,
# like an update function in the mapped_value
try: try:
quantity = float(quantity) quantity = float(quantity)
except ValueError: except ValueError:
error_string += 'Quantity is not a float.' error_string += 'Quantity is not a float.'
# Get the variation category list
variation_category_list_defined_by = None variation_category_list_defined_by = None
variation_category_list = None variation_category_list = None
# We will browse the mapped values and determine which apply # We will browse the mapped values and determine which apply
cell_key_list = self.getCellKeyList( base_id = 'variation') cell_key_list = self.getCellKeyList( base_id = 'variation')
if cell_key_list not in [(),[]]: if cell_key_list not in [(),[]]:
if context == None: if context == None:
raise KeyError, "No context defined on TransformedResource '%s'" % ( self.getRelativeUrl() , ) raise KeyError, \
"No context defined on TransformedResource '%s'" % \
(self.getRelativeUrl(), )
for key in cell_key_list: for key in cell_key_list:
if self.hasCell(base_id='variation', *key): if self.hasCell(base_id='variation', *key):
mapped_value = self.getCell(base_id='variation', *key) mapped_value = self.getCell(base_id='variation', *key)
if mapped_value.test(context): if mapped_value.test(context):
vcl = mapped_value.getCategoryList() vcl = mapped_value.getCategoryList()
if vcl != []: if vcl != []:
variation_category_list = vcl variation_category_list = vcl
variation_category_list_defined_by = mapped_value.getRelativeUrl() variation_category_list_defined_by = \
mapped_value.getRelativeUrl()
if variation_category_list in [None,'',[], ()]: if variation_category_list in [None,'',[], ()]:
if quantity == 0: if quantity == 0:
return aggregated_amount_list return aggregated_amount_list
else: else:
raise KeyError, "No cell variation matching on TransformedResource '%s' for current context" % ( self.getRelativeUrl() , ) raise KeyError, \
"No cell variation matching on TransformedResource '%s' \
for current context" % (self.getRelativeUrl(), )
else: else:
variation_category_list = self._getVariationCategoryList() variation_category_list = self._getVariationCategoryList()
variation_category_list_defined_by = self.getRelativeUrl() variation_category_list_defined_by = self.getRelativeUrl()
# Store values in Amount
tmp_amount._edit( tmp_amount._edit(
# Properties define on transformation line # Properties define on transformation line
description = self.getDescription(), description = self.getDescription(),
efficiency = efficiency, efficiency = efficiency,
quantity = quantity, quantity = quantity,
# This fields only store some informations for debugging if necessary # This fields only store some informations for debugging if necessary
quantity_defined_by = quantity_defined_by, quantity_defined_by = quantity_defined_by,
variation_category_list_defined_by = variation_category_list_defined_by, variation_category_list_defined_by = variation_category_list_defined_by,
error_string = error_string error_string = error_string
) )
tmp_amount.setVariationCategoryList(variation_category_list) tmp_amount.setVariationCategoryList(variation_category_list)
aggregated_amount_list.append( tmp_amount ) aggregated_amount_list.append( tmp_amount )
return aggregated_amount_list return aggregated_amount_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