Skip to content
Merged
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
4 changes: 4 additions & 0 deletions dms_field/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ Contributors
* Víctor Martínez
* Carlos Roca

* `PyTech <https://www.pytech.it>`_:

* Simone Rubino <simone.rubino@pytech.it>

Maintainers
~~~~~~~~~~~

Expand Down
20 changes: 15 additions & 5 deletions dms_field/models/dms_access_group.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Copyright 2024 Tecnativa - Víctor Martínez
# Copyright 2025 Simone Rubino - PyTech
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from odoo import _, api, fields, models
from odoo.exceptions import UserError
from odoo.osv import expression


class DmsAccessGroups(models.Model):
Expand Down Expand Up @@ -38,18 +40,26 @@ def _compute_company_id(self):
else False
)

def _get_domain_for_item_from_dms_field_ref(self, record):
return [
("dms_field_ref", "=", "%s,%s" % (record._name, record.id)),
]

def _get_item_from_dms_field_ref(self, record):
return self.env["dms.access.group"].search(
[("dms_field_ref", "=", "%s,%s" % (record._name, record.id))]
self._get_domain_for_item_from_dms_field_ref(record)
)

@api.constrains("dms_field_ref")
def _check_dms_field_ref(self):
for item in self.filtered("dms_field_ref"):
dms_field_ref = "%s,%s" % (item.dms_field_ref._name, item.dms_field_ref.id)
if self.search(
[("dms_field_ref", "=", dms_field_ref), ("id", "!=", item.id)]
):
domain = expression.AND(
[
item._get_domain_for_item_from_dms_field_ref(item.dms_field_ref),
[("id", "!=", item.id)],
]
)
if self.search(domain):
raise UserError(
_("There is already an access group created for this record.")
)
24 changes: 19 additions & 5 deletions dms_field/models/dms_field_template.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2024 Tecnativa - Víctor Martínez
# Copyright 2025 Simone Rubino - PyTech
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import _, api, fields, models
from odoo.exceptions import UserError, ValidationError
Expand Down Expand Up @@ -152,16 +153,29 @@ def _get_autogenerated_group(self, record):
# Create the autogenerated group linked to the record
return group_model.create(self._prepare_autogenerated_group(record))

def _prepare_child_directory_vals(self, parent, template_child_directory):
"""Values to create child directories on the record.

:param parent: Directory already created from the template for the record
:param template_child_directory: Directory in the template directories structure
"""
return {
"name": template_child_directory.name,
"is_root_directory": False,
"parent_id": parent.id,
"inherit_group_ids": template_child_directory.inherit_group_ids,
"group_ids": [
fields.Command.link(group.id)
for group in template_child_directory.group_ids
],
}

def _create_child_directories(self, parent, directory):
# Create child directories (all leves) + files
directory_model = self.env["dms.directory"].sudo()
for child_directory in directory.child_directory_ids:
child = directory_model.create(
{
"name": child_directory.name,
"is_root_directory": False,
"parent_id": parent.id,
}
self._prepare_child_directory_vals(parent, child_directory)
)
self._copy_files_from_directory(child_directory, child)
self._create_child_directories(child, child_directory)
Expand Down
4 changes: 4 additions & 0 deletions dms_field/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@

* Víctor Martínez
* Carlos Roca

* `PyTech <https://www.pytech.it>`_:

* Simone Rubino <simone.rubino@pytech.it>
4 changes: 4 additions & 0 deletions dms_field/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,10 @@ <h2><a class="toc-backref" href="#toc-entry-7">Contributors</a></h2>
<li>Carlos Roca</li>
</ul>
</li>
<li><a class="reference external" href="https://www.pytech.it">PyTech</a>:<ul>
<li>Simone Rubino &lt;<a class="reference external" href="mailto:simone.rubino&#64;pytech.it">simone.rubino&#64;pytech.it</a>&gt;</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand Down
34 changes: 34 additions & 0 deletions dms_field/tests/test_dms_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,37 @@ def test_parents(self):
{"id": directory.id, "name": directory.name},
directory.search_read_parents(fields=["id", "name"]),
)

def test_child_values(self):
"""Values of the child directory in the template
are propagated to the new directories."""
# Arrange
partner = self.partner
access_group = self.env["dms.access.group"].create(
{
"name": "Test Access group",
}
)
child_dir = self.subdirectory_1
child_dir.inherit_group_ids = False
child_dir.group_ids = access_group

# Act
self.env["dms.field.template"].with_context(
res_model=partner._name,
res_id=partner.id,
).create_dms_directory()

# Assert
partner.invalidate_model()
new_subdirectory_1 = partner.dms_directory_ids.child_directory_ids.filtered(
lambda d, dir_name=child_dir.name: d.name == dir_name
)
self.assertFalse(new_subdirectory_1.inherit_group_ids)
self.assertEqual(new_subdirectory_1.group_ids, access_group)

new_subdirectory_2 = partner.dms_directory_ids.child_directory_ids.filtered(
lambda d, dir_name=self.subdirectory_2.name: d.name == dir_name
)
self.assertTrue(new_subdirectory_2.inherit_group_ids)
self.assertIn(self.group, new_subdirectory_2.complete_group_ids.group_ids)