Skip to content

Commit

Permalink
Merge PR #259 into 14.0
Browse files Browse the repository at this point in the history
Signed-off-by pedrobaeza
  • Loading branch information
OCA-git-bot committed Jan 5, 2024
2 parents 4b17bf7 + 63cc5ae commit 34ac878
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 10 deletions.
36 changes: 27 additions & 9 deletions dms/models/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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():
Expand Down
32 changes: 32 additions & 0 deletions dms/tests/test_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 3 additions & 1 deletion dms/tests/test_file_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
}
)

Expand Down

0 comments on commit 34ac878

Please sign in to comment.