Skip to content

Commit

Permalink
[IMP+FIX] dms: Improve performance + Avoid non-existing record access…
Browse files Browse the repository at this point in the history
… error

Changes done:
- Improve _dms_operations() method to do nothing if the model is not one of
those used in dms.storage.
- Avoid access error with .browse() if we access a non-existing record (for
example, deleted by database).

TT50231
  • Loading branch information
victoralmau committed Aug 9, 2024
1 parent 18e60eb commit bdbd0ae
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
7 changes: 5 additions & 2 deletions dms/models/dms_security_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,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 @@ -35,9 +36,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 @@ -145,3 +145,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["ir.attachment"].clear_caches()
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 @@ -29,6 +29,19 @@ def test_storage_attachment(self):
self.assertFalse(file_01.exists())
self.assertFalse(directory.exists())

@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 = 0 # 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

0 comments on commit bdbd0ae

Please sign in to comment.