From 6f6c628cf5f372f0754f7638ef1a2407b9ddd7a2 Mon Sep 17 00:00:00 2001
From: Alexandre Boeglin <alex@nexedi.com>
Date: Fri, 15 Sep 2006 09:48:43 +0000
Subject: [PATCH] _edit() must not detect a property modified in a interaction
 workflow script as modified by the user

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@9970 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5/tests/testInteractionWorkflow.py | 36 +++++++++++++++++++
 product/ERP5Type/Base.py                      |  7 +++-
 2 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/product/ERP5/tests/testInteractionWorkflow.py b/product/ERP5/tests/testInteractionWorkflow.py
index 53c055dbb8..a1d9cd3d7c 100644
--- a/product/ERP5/tests/testInteractionWorkflow.py
+++ b/product/ERP5/tests/testInteractionWorkflow.py
@@ -424,6 +424,42 @@ class TestInteractionWorkflow(ERP5TypeTestCase):
     self.assertEquals(organisation.getVatCode(),'fooa')
     self.assertEquals(organisation.getDefaultEmailText(),'bar')
 
+  def test_13(self, quiet=0, run=run_all_test):
+    if not run: return
+    if not quiet:
+      self.logMessage('Interactions, Check that edit does not detect the '
+          'property modified in interaction script as modified by user')
+    self.createInteractionWorkflow()
+    self.interaction.setProperties(
+            'afterEdit',
+            method_id='setTitle',
+            after_script_name=('afterEdit',))
+    params = 'sci,**kw'
+    body = "context = sci.object\n" +\
+           "vat_code = context.getVatCode()\n" +\
+           "if vat_code is None:\n" +\
+           "  vat_code = ''\n" +\
+           "context.setVatCode(vat_code + 'a')"
+    self.script.ZPythonScript_edit(params,body)
+    self.createData()
+    organisation = self.organisation
+    organisation.setTitle('foo')
+    organisation.setVatCode('bar')
+    self.assertEquals(organisation.getTitle(), 'foo')
+    self.assertEquals(organisation.getVatCode(), 'bar')
+
+    organisation.edit(title='baz', vat_code='bar')
+    self.assertEquals(organisation.getTitle(),'baz')
+    # here, the wrong behaviour was:
+    # - edit:setTitle(baz)
+    # - interaction:setVatCode(bara)
+    # - edit:setVatCode(bar)
+    # whereas, the correct order is:
+    # - edit:setTitle(baz)
+    # - edit:setVatCode(bar)
+    # - interaction:setVatCode(bara)
+    self.assertEquals(organisation.getVatCode(),'bara')
+    
 if __name__ == '__main__':
     framework()
 else:
diff --git a/product/ERP5Type/Base.py b/product/ERP5Type/Base.py
index 1714774a18..8734efddba 100644
--- a/product/ERP5Type/Base.py
+++ b/product/ERP5Type/Base.py
@@ -984,6 +984,7 @@ class Base( CopyContainer, PortalContent, ActiveObject, ERP5PropertyManager ):
       be updated through this generic edit method
     """
     self._v_modified_property_dict = {}
+    my_modified_property_list = []
     for key in kw.keys():
       if key != 'id':
         # We only change if the value is different
@@ -999,12 +1000,16 @@ class Base( CopyContainer, PortalContent, ActiveObject, ERP5PropertyManager ):
         if old_value != kw[key] or force_update:
           # We keep in a thread var the previous values
           # this can be useful for interaction workflow to implement lookups
+          # XXX If iteraction workflow script is triggered by edit and calls
+          # edit itself, this is useless as the dict will be overwritten
           self._v_modified_property_dict[key] = old_value
-          self._setProperty(key, kw[key])
+          my_modified_property_list.append(key)
       elif self.id != kw['id']:
         self.setId(kw['id'], reindex=reindex_object)
     # Modification date is supported by edit_workflow in ERP5
     # There is no need to change it here
+    for key in my_modified_property_list:
+      self._setProperty(key, kw[key])
     if reindex_object:
       # We do not want to reindex the object if nothing is changed
       if (self._v_modified_property_dict != {}):
-- 
2.30.9