Skip to content

Commit c0c924a

Browse files
committed
[FIX] dms_field: make template root directory creation idempotent
Changed files: - dms_field/models/dms_field_template.py - dms_field/static/src/views/dms_list/dms_list_controller.esm.js Root cause: When initializing the Documents tab of a dms.field.template, the backend created the template root directory, but repeated calls to create_dms_directory() could try to create the same root again, causing: "A directory with the same name already exists." The created directory could also be missing template access groups, making it invisible to regular users. On the frontend, the DMS widget relied on cached dms_directory_ids, so a newly created template root directory could exist in the database but not appear immediately in the tree. Solution: Make dms.field.template root directory creation idempotent, keep access groups aligned with the template, and adjust the widget domain so the created root can be displayed after refresh.
1 parent 6da9585 commit c0c924a

2 files changed

Lines changed: 36 additions & 10 deletions

File tree

dms_field/models/dms_field_template.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,37 @@ def create_dms_directory(self):
7979
record = self.env[res_model].browse(res_id)
8080
directory_model = self.env["dms.directory"].sudo()
8181
if res_model == "dms.field.template":
82+
existing_directory = directory_model.search(
83+
[
84+
("storage_id", "=", record.storage_id.id),
85+
("res_id", "=", record.id),
86+
("res_model", "=", record._name),
87+
("is_root_directory", "=", True),
88+
],
89+
limit=1,
90+
)
91+
if existing_directory:
92+
missing_groups = record.group_ids - existing_directory.group_ids
93+
if missing_groups:
94+
existing_directory.write(
95+
{
96+
"group_ids": [
97+
fields.Command.link(group.id)
98+
for group in missing_groups
99+
],
100+
}
101+
)
102+
return existing_directory
82103
return directory_model.create(
83104
{
84105
"storage_id": record.storage_id.id,
85106
"res_id": record.id,
86107
"res_model": record._name,
87108
"is_root_directory": True,
88109
"name": record.display_name,
89-
"group_ids": record.group_ids.ids,
110+
"group_ids": [
111+
fields.Command.link(group.id) for group in record.group_ids
112+
],
90113
}
91114
)
92115
template = self._get_template_from_model(res_model).sudo()

dms_field/static/src/views/dms_list/dms_list_controller.esm.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,22 @@ export function getDMSListControllerObject() {
7878
directory_domain = [];
7979
} else if (model === "dms.field.template") {
8080
if (this.model.root.resId) {
81+
const rootDirectoryIds =
82+
this.model.root.data.dms_directory_ids.records.map((record) => {
83+
return record.resId;
84+
});
8185
storage_domain = [["id", "=", this.model.root.data.storage_id[0]]];
86+
directory_domain = Domain.or([
87+
new Domain([["root_directory_id", "in", rootDirectoryIds]]),
88+
new Domain([
89+
["res_model", "=", model],
90+
["res_id", "=", this.model.root.resId],
91+
]),
92+
]).toList();
8293
} else {
8394
storage_domain = [["id", "=", 0]];
95+
directory_domain = [["id", "=", 0]];
8496
}
85-
directory_domain = [
86-
[
87-
"root_directory_id",
88-
"in",
89-
this.model.root.data.dms_directory_ids.records.map((record) => {
90-
return record.resId;
91-
}),
92-
],
93-
];
9497
} else {
9598
storage_domain = [["field_template_ids.model", "=", model]];
9699
autocompute_directory = true;

0 commit comments

Comments
 (0)