Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[17.0][IMP+FIX] dms: Improve performance + Avoid non-existing record access error #360

Merged
merged 2 commits into from
Aug 20, 2024
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
1 change: 1 addition & 0 deletions dms/models/dms_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class DMSFile(models.Model):
readonly=True,
prefetch=False,
compute_sudo=True,
store=True,
)
require_migration = fields.Boolean(
compute="_compute_migration", store=True, compute_sudo=True
Expand Down
7 changes: 5 additions & 2 deletions dms/models/dms_security_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,11 @@ def _get_domain_by_inheritance(self, operation):
continue
domains.append([("res_model", "=", model._name), ("res_id", "=", False)])
# Check record access in batch too
group_ids = [i for i in group["res_id"] if i] # Hack to remove None res_id
related_ok = model.browse(group_ids)._filter_access_rules_python(operation)
res_ids = [i for i in group["res_id"] if i] # Hack to remove None res_id
# Apply exists to skip records that do not exist. (e.g. a res.partner
# deleted by database).
model_records = model.browse(res_ids).exists()
related_ok = model_records._filter_access_rules_python(operation)
if not related_ok:
continue
domains.append(
Expand Down
20 changes: 19 additions & 1 deletion dms/models/ir_attachment.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2021 Tecnativa - Víctor Martínez
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
from odoo import api, models
from odoo.tools import ormcache


class IrAttachment(models.Model):
Expand Down Expand Up @@ -34,9 +35,26 @@ def _dms_directories_create(self):
}
)

@ormcache("model")
def _dms_operations_from_model(self, model):
# Apply sudo to prevent ir.rule from being applied.
item = self.env["dms.storage"].sudo().search([("model_ids.model", "=", model)])
return bool(item)

def _dms_operations(self):
"""Perform the operation only if there is a storage with linked models.
The directory (dms.directory) linked to the record (if it does not exist)
and the file (dms.file) with the linked attachment would be created.
"""
for attachment in self:
if not attachment.res_model or not attachment.res_id:
if (
not attachment.res_model
or not attachment.res_id
or (
attachment.res_model
and not self._dms_operations_from_model(attachment.res_model)
)
):
continue
directories = attachment._get_dms_directories(
attachment.res_model, attachment.res_id
Expand Down
6 changes: 6 additions & 0 deletions dms/models/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,9 @@ def _compute_count_storage_directories(self):
def _compute_count_storage_files(self):
for record in self:
record.count_storage_files = len(record.storage_file_ids)

def write(self, values):
res = super().write(values)
if "model_ids" in values:
self.env.registry.clear_cache()
return res
13 changes: 13 additions & 0 deletions dms/tests/test_storage_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ def test_storage_attachment(self):
self.assertFalse(file_01.exists(), "File should not exist")
self.assertFalse(directory.exists(), "Directory should not exist")

@users("dms-manager")
def test_storage_attachment_record_db_unlink(self):
self._create_attachment("demo.txt")
self.assertTrue(
self.storage.storage_file_ids.filtered(lambda x: x.name == "demo.txt")
)
directory = self._get_partner_directory()
self.assertEqual(directory.res_model, self.partner._name)
self.assertEqual(directory.res_id, self.partner.id)
directory.res_id = -1 # Trick to reference a non-existing record
directories = self.env["dms.directory"].search([])
self.assertNotIn(directory.id, directories.ids)

@users("dms-manager")
def test_storage_attachment_misc(self):
attachment = self._create_attachment("demo.txt")
Expand Down
Loading