From c2768248326739617fd0f7ef07a454dd11438069 Mon Sep 17 00:00:00 2001
From: Alexandre Boeglin <alex@nexedi.com>
Date: Wed, 6 Feb 2008 18:23:13 +0000
Subject: [PATCH] When a field property becomes delegated, field.values and
 field.tales entries for this property should be removed (they are no longer
 required, no need to export them in bt5 and make the diff less readable).

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@19113 20353a03-c40f-0410-a6d1-a30d3c3de9de
---
 product/ERP5Form/ProxyField.py       |  4 +++
 product/ERP5Form/tests/testFields.py | 49 ++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)

diff --git a/product/ERP5Form/ProxyField.py b/product/ERP5Form/ProxyField.py
index a093115aa2..601eac3102 100644
--- a/product/ERP5Form/ProxyField.py
+++ b/product/ERP5Form/ProxyField.py
@@ -260,6 +260,8 @@ class ProxyField(ZMIField):
     for key, value in result.items():
       if key not in surcharge_list:
         result.pop(key)
+        if key in self.tales:
+          self.tales.pop(key)
 
     # now do actual update of values
     values.update(result)
@@ -337,6 +339,8 @@ class ProxyField(ZMIField):
     for key, value in result.items():
       if key not in surcharge_list:
         result.pop(key)
+        if key in self.values:
+          self.values.pop(key)
 
     # now do actual update of values
     tales.update(result)
diff --git a/product/ERP5Form/tests/testFields.py b/product/ERP5Form/tests/testFields.py
index fb2b0f035d..a5b190ab14 100644
--- a/product/ERP5Form/tests/testFields.py
+++ b/product/ERP5Form/tests/testFields.py
@@ -326,6 +326,55 @@ class TestProxyField(unittest.TestCase):
     # Next, call field which the id is same to the template field's.
     self.assertEqual('123', proxy_field1.get_value('default'))
 
+  def test_dicts_cleared_on_edit(self):
+    """
+    Test that values and tales dicts are cleared when property is switched to
+    not surcharged.
+    """
+    # create a field
+    original_field = self.addField(self.container.Base_viewProxyFieldLibrary,
+                                   'my_title', 'OrigTitle', 'StringField')
+    field = self.addField(self.container.Base_view,
+                                   'my_dict_test', '', 'ProxyField')
+    field.manage_edit_xmlrpc(dict(form_id='Base_viewProxyFieldLibrary',
+                                         field_id='my_title',))
+    def surcharge_edit():
+      #surcharge from edit
+      field._surcharged_edit(dict(title='TestTitle'), ['title'])
+      self.assertTrue('title' in field.delegated_list)
+      self.assertEquals(field.values['title'], 'TestTitle')
+      self.assertTrue('title' not in field.tales)
+
+    def delegate_edit():
+      # delegate the field from edit view
+      field._surcharged_edit(dict(title='TestTitle'), [])
+      self.assertTrue('title' not in field.delegated_list)
+      self.assertTrue('title' not in field.values)
+      self.assertTrue('title' not in field.tales)
+
+    def surcharge_tales():
+      #surcharge from tales
+      field._surcharged_tales(dict(title='string:TestTitle'), ['title'])
+      self.assertTrue('title' in field.delegated_list)
+      self.assertTrue(field.values['title'], 'OrigTitle')
+      self.assertEquals(field.tales['title'], 'string:TestTitle')
+
+    def delegate_tales():
+      # delegate the field from tales view
+      field._surcharged_tales(dict(title='string:TestTitle'), [])
+      self.assertTrue('title' not in field.delegated_list)
+      self.assertTrue('title' not in field.values)
+      self.assertTrue('title' not in field.tales)
+    
+    surcharge_edit()
+    delegate_edit()
+    surcharge_edit()
+    delegate_tales()
+    surcharge_tales()
+    delegate_edit()
+    surcharge_tales()
+    delegate_tales()
+
 
 class TestFieldValueCache(unittest.TestCase):
   """Tests field value caching system
-- 
2.30.9