changeset 04c50d261209 in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset;node=04c50d261209
description:
        Allow to delete record from XML with noupdate

        We set the db_id to None to avoid to link later to a different record.

        issue7668
        review50671002
diffstat:

 CHANGELOG                          |   1 +
 trytond/convert.py                 |  11 ++++++-
 trytond/ir/model.py                |  14 ++++++-
 trytond/model/modelstorage.py      |  30 +++++++++++++-----
 trytond/tests/test_modelstorage.py |  60 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 104 insertions(+), 12 deletions(-)

diffs (205 lines):

diff -r e3431889bb9d -r 04c50d261209 CHANGELOG
--- a/CHANGELOG Tue Jan 01 15:47:23 2019 +0100
+++ b/CHANGELOG Mon Jan 07 22:08:52 2019 +0100
@@ -1,3 +1,4 @@
+* Allow records from XML with noupdate to be deleted
 * Add ir.calendar for month and day
 * Rename languages: hu_HU, it_IT and pt_BR into hu, it and pt
 * Define custom exceptions
diff -r e3431889bb9d -r 04c50d261209 trytond/convert.py
--- a/trytond/convert.py        Tue Jan 01 15:47:23 2019 +0100
+++ b/trytond/convert.py        Mon Jan 07 22:08:52 2019 +0100
@@ -330,6 +330,11 @@
             self.fetch_new_module(module)
         return self.fs2db[module].get(fs_id, None)
 
+    def exists(self, module, fs_id):
+        if module not in self.fetched_modules:
+            self.fetch_new_module(module)
+        return fs_id in self.fs2db[module]
+
     def get_browserecord(self, module, model_name, db_id):
         if module not in self.fetched_modules:
             self.fetch_new_module(module)
@@ -583,7 +588,7 @@
 
         Model = self.pool.get(model)
 
-        if self.fs2db.get(module, fs_id):
+        if self.fs2db.exists(module, fs_id):
 
             # Remove this record from the to_delete list. This means that
             # the corresponding record have been found.
@@ -599,6 +604,10 @@
                 self.fs2db.get(module, fs_id)[x]
                 for x in ["db_id", "model", "id", "values"]]
 
+            # Check if record has not been deleted
+            if db_id is None:
+                return
+
             if not old_values:
                 old_values = {}
             else:
diff -r e3431889bb9d -r 04c50d261209 trytond/ir/model.py
--- a/trytond/ir/model.py       Tue Jan 01 15:47:23 2019 +0100
+++ b/trytond/ir/model.py       Mon Jan 07 22:08:52 2019 +0100
@@ -1066,9 +1066,12 @@
         select=True)
     model = fields.Char('Model', required=True, select=True)
     module = fields.Char('Module', required=True, select=True)
-    db_id = fields.Integer('Resource ID',
-        help="The id of the record in the database.", select=True,
-        required=True)
+    db_id = fields.Integer(
+        "Resource ID", select=True,
+        states={
+            'required': ~Eval('noupdate', False),
+            },
+        help="The id of the record in the database.")
     values = fields.Text('Values')
     fs_values = fields.Text('Values on File System')
     noupdate = fields.Boolean('No Update')
@@ -1100,12 +1103,17 @@
 
         super(ModelData, cls).__register__(module_name)
 
+        table_h = cls.__table_handler__(module_name)
+
         # Migration from 4.6: register buttons on ir module
         cursor.execute(*model_data.update(
                 [model_data.module], ['ir'],
                 where=((model_data.module == 'res')
                     & (model_data.fs_id == 'model_data_sync_button'))))
 
+        # Migration from 5.0: remove required on db_id
+        table_h.not_null_action('db_id', action='remove')
+
     @staticmethod
     def default_noupdate():
         return False
diff -r e3431889bb9d -r 04c50d261209 trytond/model/modelstorage.py
--- a/trytond/model/modelstorage.py     Tue Jan 01 15:47:23 2019 +0100
+++ b/trytond/model/modelstorage.py     Mon Jan 07 22:08:52 2019 +0100
@@ -260,13 +260,25 @@
         '''
         Delete records.
         '''
-        ModelAccess = Pool().get('ir.model.access')
+        pool = Pool()
+        ModelAccess = pool.get('ir.model.access')
+        ModelData = pool.get('ir.model.data')
 
         ModelAccess.check(cls.__name__, 'delete')
         if not cls.check_xml_record(records, None):
             raise AccessError(
                 gettext('ir.msg_delete_xml_record'),
                 gettext('ir.msg_base_config_record'))
+        if ModelData.has_model(cls.__name__):
+            with Transaction().set_context(_check_access=False):
+                data = []
+                for sub_records in grouped_slice(records):
+                    ids = [r.id for r in sub_records]
+                    data += ModelData.search([
+                            ('model', '=', cls.__name__),
+                            ('db_id', 'in', ids),
+                            ])
+                ModelData.write(data, {'db_id': None})
 
         # Increase transaction counter
         Transaction().counter += 1
@@ -902,15 +914,17 @@
                 ])
             if not models_data:
                 return True
-            if values is None:
-                return False
             for model_data in models_data:
-                if not model_data.values or model_data.noupdate:
-                    continue
-                xml_values = ModelData.load_values(model_data.values)
-                for key, val in values.items():
-                    if key in xml_values and val != xml_values[key]:
+                if values is None:
+                    if not model_data.noupdate:
                         return False
+                else:
+                    if not model_data.values or model_data.noupdate:
+                        continue
+                    xml_values = ModelData.load_values(model_data.values)
+                    for key, val in values.items():
+                        if key in xml_values and val != xml_values[key]:
+                            return False
         return True
 
     @classmethod
diff -r e3431889bb9d -r 04c50d261209 trytond/tests/test_modelstorage.py
--- a/trytond/tests/test_modelstorage.py        Tue Jan 01 15:47:23 2019 +0100
+++ b/trytond/tests/test_modelstorage.py        Mon Jan 07 22:08:52 2019 +0100
@@ -293,6 +293,66 @@
 
         self.assertTrue(result)
 
+    @with_transaction()
+    def test_check_xml_record_with_record_no_values(self):
+        "Test check_xml_record with record and no values"
+        pool = Pool()
+        Model = pool.get('test.modelstorage')
+        ModelData = pool.get('ir.model.data')
+        record, = Model.create([{'name': "Foo"}])
+        ModelData.create([{
+                    'fs_id': 'test',
+                    'model': 'test.modelstorage',
+                    'module': 'tests',
+                    'db_id': record.id,
+                    'values': None,
+                    'noupdate': False,
+                    }])
+
+        result = Model.check_xml_record([record], None)
+
+        self.assertFalse(result)
+
+    @with_transaction()
+    def test_check_xml_record_with_record_no_values_noupdate(self):
+        "Test check_xml_record with record and no values but noupdate"
+        pool = Pool()
+        Model = pool.get('test.modelstorage')
+        ModelData = pool.get('ir.model.data')
+        record, = Model.create([{'name': "Foo"}])
+        ModelData.create([{
+                    'fs_id': 'test',
+                    'model': 'test.modelstorage',
+                    'module': 'tests',
+                    'db_id': record.id,
+                    'values': None,
+                    'noupdate': True,
+                    }])
+
+        result = Model.check_xml_record([record], None)
+
+        self.assertTrue(result)
+
+    @with_transaction()
+    def test_delete_clear_db_id_model_data(self):
+        "Test delete record clear DB id from model data"
+        pool = Pool()
+        Model = pool.get('test.modelstorage')
+        ModelData = pool.get('ir.model.data')
+        record, = Model.create([{'name': "Foo"}])
+        data, = ModelData.create([{
+                    'fs_id': 'test',
+                    'model': 'test.modelstorage',
+                    'module': 'tests',
+                    'db_id': record.id,
+                    'values': None,
+                    'noupdate': True,
+                    }])
+
+        Model.delete([record])
+
+        self.assertIsNone(data.db_id)
+
 
 def suite():
     return unittest.TestLoader().loadTestsFromTestCase(ModelStorageTestCase)

Reply via email to