Commit d0256637 authored by Sebastien Robin's avatar Sebastien Robin

2008-04-10 Seb

* As suggested by Romain, move some checks from a workflow script to a constraint.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@20408 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 793e16ff
...@@ -76,49 +76,44 @@ class DuplicateInventory(Constraint): ...@@ -76,49 +76,44 @@ class DuplicateInventory(Constraint):
# way to make sure that it is impossible to validate two inventories # way to make sure that it is impossible to validate two inventories
# in the same time (required because we have message with right tags # in the same time (required because we have message with right tags
# only when the transaction is finished) # only when the transaction is finished)
node_value.serialize() if node_value is not None:
node_value.serialize()
# For each resource, we look that there is not any inventory for # For each resource, we look that there is not any inventory for
# the same date, the same resource and the same node, or if there # the same date, the same resource and the same node, or if there
# is already such kind of inventories being indexed # is already such kind of inventories being indexed
resource_and_variation_list = [] resource_and_variation_list = []
date = inventory.getStartDate() date = inventory.getStartDate()
date_string = repr(date) date_string = repr(date)
countMessageWithTag = inventory.portal_activities.countMessageWithTag countMessageWithTag = inventory.portal_activities.countMessageWithTag
portal = inventory.getPortalObject() portal = inventory.getPortalObject()
getObjectFromUid = portal.portal_catalog.getObject getObjectFromUid = portal.portal_catalog.getObject
getCurrentInventoryList = portal.portal_simulation.getCurrentInventoryList getCurrentInventoryList = portal.portal_simulation.getCurrentInventoryList
resource_and_variation_list = [] resource_and_variation_list = []
for movement in inventory.getMovementList(): for movement in inventory.getMovementList():
resource = movement.getResource() resource = movement.getResource()
if resource is not None and movement.getQuantity() not in (None,''): if resource is not None and movement.getQuantity() not in (None,''):
variation_text = movement.getVariationText() variation_text = movement.getVariationText()
if (resource,variation_text) not in resource_and_variation_list: if (resource,variation_text) not in resource_and_variation_list:
resource_and_variation_list.append((resource,variation_text)) resource_and_variation_list.append((resource,variation_text))
tag = '%s_%s_%s' % (date_string, resource, variation_text) tag = '%s_%s_%s' % (date_string, resource, variation_text)
if countMessageWithTag(tag) > 0 : if countMessageWithTag(tag) > 0 :
errors.append(self.generateDuplicateError(portal, obj, resource,
variation_text))
# Call sql request in order to see if there is another inventory
# for this node, resource, variation_text and date
inventory_list = getCurrentInventoryList(resource=resource,
variation_text=variation_text,
from_date=date, at_date=date,
default_stock_table='inventory_stock',
node=node)
LOG('inventory_list sql src', 0, getCurrentInventoryList(resource=resource,
variation_text=variation_text,
from_date=date, at_date=date,
default_stock_table='inventory_stock',
node=node, src__=1))
LOG('len inventory_list',0,len(inventory_list))
for inventory in inventory_list:
movement = getObjectFromUid(inventory.stock_uid)
if movement.getPortalType().find('Inventory') >= 0:
errors.append(self.generateDuplicateError(portal, obj, resource, errors.append(self.generateDuplicateError(portal, obj, resource,
variation_text)) variation_text))
# Now we must reindex with some particular tags # Call sql request in order to see if there is another inventory
activate_kw = {'tag': tag} # for this node, resource, variation_text and date
movement.reindexObject(activate_kw=activate_kw) inventory_list = getCurrentInventoryList(resource=resource,
variation_text=variation_text,
from_date=date, at_date=date,
default_stock_table='inventory_stock',
node=node)
for inventory in inventory_list:
movement = getObjectFromUid(inventory.stock_uid)
if movement.getPortalType().find('Inventory') >= 0:
errors.append(self.generateDuplicateError(portal, obj, resource,
variation_text))
# Now we must reindex with some particular tags
activate_kw = {'tag': tag}
movement.reindexObject(activate_kw=activate_kw)
return errors return errors
...@@ -25,6 +25,8 @@ ...@@ -25,6 +25,8 @@
# #
############################################################################## ##############################################################################
from Products.CMFCore.Expression import Expression
class InventoryConstraint: class InventoryConstraint:
""" """
Inventory Constraint Inventory Constraint
...@@ -34,5 +36,22 @@ class InventoryConstraint: ...@@ -34,5 +36,22 @@ class InventoryConstraint:
'id': 'duplicate_inventory', 'id': 'duplicate_inventory',
'type': 'DuplicateInventory', 'type': 'DuplicateInventory',
}, },
{ 'id' : 'category_existence',
'description' : 'Source, Destination and Sections must be defined',
'type' : 'CategoryExistence',
'portal_type' : Expression('python: portal.getPortalNodeTypeList()'),
'destination' : 1,
'destination_section': 1,
},
{ 'id' : 'start_date',
'description' : 'Start Date must be defined',
'type' : 'PropertyExistence',
'start_date' : 1,
},
{ 'id' : 'resource_on_line',
'description' : 'Resource must be defined on all lines',
'type' : 'TALESConstraint',
'expression' : 'python: None not in [x.getResourceValue() for x in object.getMovementList()]',
'message_expression_false': 'You must define a resource on all lines',
},
) )
...@@ -65,42 +65,14 @@ ...@@ -65,42 +65,14 @@
</item> </item>
<item> <item>
<key> <string>_body</string> </key> <key> <string>_body</string> </key>
<value> <string encoding="cdata"><![CDATA[ <value> <string>from Products.ERP5Type.Message import Message\n
from Products.ERP5Type.Message import Message\n
from Products.DCWorkflow.DCWorkflow import ValidationFailed\n from Products.DCWorkflow.DCWorkflow import ValidationFailed\n
\n \n
inventory = state_change[\'object\']\n inventory = state_change[\'object\']\n
\n \n
# Make sure the start_date is defined\n
start_date = inventory.getStartDate()\n
if start_date is None:\n
text = "Sorry, you must define the inventory date"\n
message = Message(domain=\'ui\', message=text)\n
raise ValidationFailed, message\n
\n
# Make sure the node is defined\n
node = inventory.getDestination()\n
if node is None:\n
text = "Sorry, you must define the inventory warehouse"\n
message = Message(domain=\'ui\', message=text)\n
raise ValidationFailed, message\n
\n
for line in inventory.getMovementList():\n
if line.getResource() is None:\n
text = "Sorry, no resource for line $line_title"\n
message = Message(domain=\'ui\', message=text,\n
mapping={\'line_title\': line.getTitle()})\n
raise ValidationFailed, message\n
\n
\n
# use of the constraint\n # use of the constraint\n
error_list = inventory.checkConsistency()\n inventory.Base_checkConsistency()\n
if len(error_list) > 0:\n </string> </value>
raise ValidationFailed, (error_list[0].getTranslatedMessage(),)\n
]]></string> </value>
</item> </item>
<item> <item>
<key> <string>_code</string> </key> <key> <string>_code</string> </key>
...@@ -151,15 +123,6 @@ if len(error_list) > 0:\n ...@@ -151,15 +123,6 @@ if len(error_list) > 0:\n
<string>_getitem_</string> <string>_getitem_</string>
<string>inventory</string> <string>inventory</string>
<string>_getattr_</string> <string>_getattr_</string>
<string>start_date</string>
<string>None</string>
<string>text</string>
<string>message</string>
<string>node</string>
<string>_getiter_</string>
<string>line</string>
<string>error_list</string>
<string>len</string>
</tuple> </tuple>
</value> </value>
</item> </item>
......
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<tuple>
<global name="VariableDefinition" module="Products.DCWorkflow.Variables"/>
<tuple/>
</tuple>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>__ac_local_roles__</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>default_expr</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>default_value</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>description</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>for_catalog</string> </key>
<value> <int>1</int> </value>
</item>
<item>
<key> <string>for_status</string> </key>
<value> <int>0</int> </value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>portal_type</string> </value>
</item>
<item>
<key> <string>info_guard</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>update_always</string> </key>
<value> <int>0</int> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
231 232
\ No newline at end of file \ No newline at end of file
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