Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion dms_field/models/dms_field_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,37 @@ def create_dms_directory(self):
record = self.env[res_model].browse(res_id)
directory_model = self.env["dms.directory"].sudo()
if res_model == "dms.field.template":
existing_directory = directory_model.search(
[
("storage_id", "=", record.storage_id.id),
("res_id", "=", record.id),
("res_model", "=", record._name),
("is_root_directory", "=", True),
],
limit=1,
)
if existing_directory:
missing_groups = record.group_ids - existing_directory.group_ids
if missing_groups:
existing_directory.write(
{
"group_ids": [
fields.Command.link(group.id)
for group in missing_groups
],
}
)
return existing_directory
return directory_model.create(
{
"storage_id": record.storage_id.id,
"res_id": record.id,
"res_model": record._name,
"is_root_directory": True,
"name": record.display_name,
"group_ids": record.group_ids.ids,
"group_ids": [
fields.Command.link(group.id) for group in record.group_ids
],
}
)
template = self._get_template_from_model(res_model).sudo()
Expand Down
21 changes: 12 additions & 9 deletions dms_field/static/src/views/dms_list/dms_list_controller.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,22 @@ export function getDMSListControllerObject() {
directory_domain = [];
} else if (model === "dms.field.template") {
if (this.model.root.resId) {
const rootDirectoryIds =
this.model.root.data.dms_directory_ids.records.map((record) => {
return record.resId;
});
storage_domain = [["id", "=", this.model.root.data.storage_id[0]]];
directory_domain = Domain.or([
new Domain([["root_directory_id", "in", rootDirectoryIds]]),
new Domain([
["res_model", "=", model],
["res_id", "=", this.model.root.resId],
]),
]).toList();
} else {
storage_domain = [["id", "=", 0]];
directory_domain = [["id", "=", 0]];
}
directory_domain = [
[
"root_directory_id",
"in",
this.model.root.data.dms_directory_ids.records.map((record) => {
return record.resId;
}),
],
];
} else {
storage_domain = [["field_template_ids.model", "=", model]];
autocompute_directory = true;
Expand Down
41 changes: 41 additions & 0 deletions dms_field/tests/test_dms_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,47 @@ def test_template_directory(self):
self.template.group_ids, self.template.dms_directory_ids.group_ids
)

def test_template_directory_creation(self):
template = self.env["dms.field.template"].create(
{
"name": "Users template",
"storage_id": self.storage.id,
"model_id": self.env.ref("base.model_res_users").id,
"group_ids": [fields.Command.link(self.template.group_ids.id)],
}
)
directory = template.with_context(
res_model=template._name,
res_id=template.id,
).create_dms_directory()

self.assertEqual(directory.storage_id, template.storage_id)
self.assertEqual(directory.res_model, template._name)
self.assertEqual(directory.res_id, template.id)
self.assertTrue(directory.is_root_directory)
self.assertIn(template.group_ids, directory.group_ids)
second_directory = template.with_context(
res_model=template._name,
res_id=template.id,
).create_dms_directory()
self.assertEqual(second_directory, directory)

def test_template_directory_creation_is_idempotent(self):
directory = self.template.dms_directory_ids
directory.group_ids = [fields.Command.clear()]
self.assertFalse(directory.group_ids)

template = self.env["dms.field.template"].with_context(
res_model=self.template._name,
res_id=self.template.id,
)
first_directory = template.create_dms_directory()
second_directory = template.create_dms_directory()

self.assertEqual(first_directory, directory)
self.assertEqual(second_directory, directory)
self.assertIn(self.template.group_ids, directory.group_ids)

@mute_logger("odoo.models.unlink")
def test_creation_process_01(self):
self.assertFalse(self.partner.dms_directory_ids)
Expand Down
Loading