diff --git a/dms/models/directory.py b/dms/models/directory.py index 4e4eec21c..fe0a5cd51 100644 --- a/dms/models/directory.py +++ b/dms/models/directory.py @@ -78,6 +78,10 @@ class DmsDirectory(models.Model): default=lambda self: self._default_parent_id(), ) + root_directory_id = fields.Many2one( + "dms.directory", "Root Directory", compute="_compute_root_id", store=True + ) + def _default_parent_id(self): context = self.env.context if context.get("active_model") == self._name and context.get("active_id"): @@ -486,6 +490,17 @@ def _compute_parent_id(self): # HACK: Not needed in v14 due to odoo/odoo#64359 record.parent_id = record.parent_id + @api.depends("is_root_directory", "parent_id") + def _compute_root_id(self): + for record in self: + if record.is_root_directory: + record.root_directory_id = record + else: + # recursively check all parent nodes up to the root directory + if not record.parent_id.root_directory_id: + record.parent_id._compute_root_id() + record.root_directory_id = record.parent_id.root_directory_id + @api.depends("category_id") def _compute_tags(self): for record in self: @@ -661,17 +676,20 @@ def create(self, vals_list): return res def write(self, vals): - if vals.get("storage_id"): + if any([k in vals.keys() for k in ["storage_id", "parent_id"]]): for item in self: - if item.storage_id.id != vals["storage_id"]: + new_storage_id = vals.get("storage_id", item.storage_id.id) + new_parent_id = vals.get("parent_id", item.parent_id.id) + old_storage_id = ( + item.storage_id or item.root_directory_id.storage_id + ).id + if new_parent_id: + if old_storage_id != self.browse(new_parent_id).storage_id.id: + raise UserError( + _("It is not possible to change parent to other storage.") + ) + elif old_storage_id != new_storage_id: raise UserError(_("It is not possible to change the storage.")) - if vals.get("parent_id"): - parent = self.browse([vals["parent_id"]]) - for item in self: - if item.parent_id.storage_id != parent.storage_id: - raise UserError( - _("It is not possible to change parent to other storage.") - ) # Groups part if any(key in vals for key in ["group_ids", "inherit_group_ids"]): with self.env.norecompute(): diff --git a/dms/tests/test_directory.py b/dms/tests/test_directory.py index 311421499..dffc9ef97 100644 --- a/dms/tests/test_directory.py +++ b/dms/tests/test_directory.py @@ -68,6 +68,38 @@ def test_move_directory(self): "parent_id": self.subdirectory.id, } ) + with self.assertRaises(UserError): + self.subdirectory.write( + { + "is_root_directory": True, + "storage_id": self.new_storage.id, + "parent_id": False, + } + ) + self.subdirectory.write( + { + "is_root_directory": True, + "storage_id": self.storage.id, + "parent_id": False, + } + ) + new_directory = self.create_directory(storage=self.storage) + self.subdirectory.write( + { + "is_root_directory": False, + "storage_id": False, + "parent_id": new_directory.id, + } + ) + with self.assertRaises(UserError): + self.directory.storage_id = self.new_storage.id + self.subdirectory.write( + { + "is_root_directory": False, + "storage_id": False, + "parent_id": self.directory.id, + } + ) @users("dms-manager", "dms-user") def test_unlink_root_directory(self): diff --git a/dms/tests/test_file_database.py b/dms/tests/test_file_database.py index 65ef29424..ea690d211 100644 --- a/dms/tests/test_file_database.py +++ b/dms/tests/test_file_database.py @@ -14,6 +14,8 @@ def setUp(self): super().setUp() self.file_demo_01 = self.env.ref("dms.file_01_demo") self.directory2 = self.create_directory(storage=self.storage) + self.new_storage2 = self.create_storage(save_type="database") + self.directory3 = self.create_directory(storage=self.new_storage2) @users("dms-manager", "dms-user") def test_create_file(self): @@ -59,7 +61,7 @@ def test_move_directory(self): self.directory.write( { "is_root_directory": False, - "parent_id": self.directory2.id, + "parent_id": self.directory3.id, } )