Skip to content

Commit

Permalink
[FIX] dms: Improve the unlink method in base to avoid errors
Browse files Browse the repository at this point in the history
In the uninstall process of dms, checks were made that prevented the
removal of fields and models being deleted, also causing the dms_file
and dms_directory tables to continue to exist.
  • Loading branch information
victoralmau committed Jan 25, 2024
1 parent df31f7d commit 7a6c10d
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions dms/models/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2021 Tecnativa - Jairo Llopis
# Copyright 2024 Tecnativa - Víctor Martínez
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl).

from odoo import models
Expand All @@ -8,12 +9,20 @@ class Base(models.AbstractModel):
_inherit = "base"

def unlink(self):
"""Cascade DMS related resources removal."""
"""Cascade DMS related resources removal.
Avoid executing in the transient models, in the models we want to check
(dms.file and dms.directory) and make sure that the models exist (in the
uninstall process of dms)."""
result = super().unlink()
self.env["dms.file"].sudo().search(
[("res_model", "=", self._name), ("res_id", "in", self.ids)]
).unlink()
self.env["dms.directory"].sudo().search(
[("res_model", "=", self._name), ("res_id", "in", self.ids)]
).unlink()
models_to_check = ("dms.file", "dms.directory")
if self._name not in models_to_check and not self.is_transient():
models = (
self.env["ir.model"]
.sudo()
.search([("model", "in", models_to_check)])
.mapped("model")
)
domain = [("res_model", "=", self._name), ("res_id", "in", self.ids)]
for model in models:
self.env[model].sudo().search(domain).unlink()
return result

0 comments on commit 7a6c10d

Please sign in to comment.