Skip to content

Commit

Permalink
[18.0][MIG] autovacuum_message_attachment: Migration to 18.0
Browse files Browse the repository at this point in the history
  • Loading branch information
yankinmax committed Jan 21, 2025
1 parent 8651a4a commit 79ce63a
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 59 deletions.
2 changes: 1 addition & 1 deletion autovacuum_message_attachment/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{
"name": "AutoVacuum Mail Message and Attachment",
"version": "16.0.1.0.1",
"version": "18.0.1.0.0",
"category": "Tools",
"website": "https://github.com/OCA/server-tools",
"author": "Akretion, Odoo Community Association (OCA)",
Expand Down
4 changes: 0 additions & 4 deletions autovacuum_message_attachment/data/data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
<field name="user_id" ref="base.user_root" />
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="numbercall">-1</field>
<field name="state">code</field>
<field name="code">model.autovacuum('message')</field>
<field eval="False" name="doall" />
<field name="model_id" ref="mail.model_mail_message" />
</record>
<record id="ir_cron_vacuum_attachment" model="ir.cron">
Expand All @@ -18,10 +16,8 @@
<field name="user_id" ref="base.user_root" />
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="numbercall">-1</field>
<field name="state">code</field>
<field name="code">model.autovacuum('attachment')</field>
<field eval="False" name="doall" />
<field name="model_id" ref="base.model_ir_attachment" />
</record>
</odoo>
10 changes: 0 additions & 10 deletions autovacuum_message_attachment/i18n/autovacuum_mail_message.pot
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,6 @@ msgstr ""
msgid "Message"
msgstr ""

#. module: autovacuum_mail_message
#: model:ir.ui.view,arch_db:autovacuum_mail_message.message_vacuum_rule_form_view
msgid "Message Models"
msgstr ""

#. module: autovacuum_mail_message
#: model:ir.ui.view,arch_db:autovacuum_mail_message.message_vacuum_rule_form_view
msgid "Message Subtypes"
msgstr ""

#. module: autovacuum_mail_message
#: model:ir.actions.act_window,name:autovacuum_mail_message.action_message_vacuum_rule
#: model:ir.ui.menu,name:autovacuum_mail_message.menu_action_message_vacuum_rule
Expand Down
6 changes: 3 additions & 3 deletions autovacuum_message_attachment/models/autovacuum_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import logging

import odoo
from odoo import api, models
from odoo.modules.registry import Registry
from odoo.tools.safe_eval import datetime, safe_eval

_logger = logging.getLogger(__name__)
Expand All @@ -15,7 +15,7 @@ class AutovacuumMixin(models.AbstractModel):
_description = "Mixin used to delete messages or attachments"

def batch_unlink(self):
with odoo.registry(self.env.cr.dbname).cursor() as new_cr:
with Registry(self.env.cr.dbname).cursor() as new_cr:
new_env = api.Environment(new_cr, self.env.uid, self.env.context)
try:
while self:
Expand Down Expand Up @@ -54,7 +54,7 @@ def _get_autovacuum_records_model(self, rule):
)
autovacuum_relation = self._autovacuum_relation
for leaf in domain:
if not isinstance(leaf, (tuple, list)):
if not isinstance(leaf, (tuple | list)):
record_domain.append(leaf)
continue
field, operator, value = leaf
Expand Down
47 changes: 27 additions & 20 deletions autovacuum_message_attachment/models/vacuum_rule.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
# Copyright (C) 2018 Akretion
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).

from odoo import _, api, exceptions, fields, models
from odoo import api, exceptions, fields, models


class VacuumRule(models.Model):
_name = "vacuum.rule"
_description = "Rules Used to delete message historic"

@api.depends("model_ids")
def _compute_model_id(self):
for rule in self:
if rule.model_ids and len(rule.model_ids) == 1:
rule.model_id = rule.model_ids.id
rule.model = rule.model_id.model
else:
rule.model_id = False
rule.model = False

name = fields.Char(required=True)
ttype = fields.Selection(
selection=[("attachment", "Attachment"), ("message", "Message")],
Expand Down Expand Up @@ -54,13 +44,12 @@ def _compute_model_id(self):
)
model_id = fields.Many2one(
"ir.model",
readonly=True,
compute="_compute_model_id",
help="Technical field used to set attributes (invisible/required, "
"domain, etc...for other fields, like the domain filter",
)
model_filter_domain = fields.Text()
model = fields.Char(readonly=True, compute="_compute_model_id", string="Model code")
model = fields.Char(compute="_compute_model_id", string="Model code")
empty_model = fields.Boolean(
help="Take into account attachment not linked to any model, but only if a "
"pattern is set, to avoid deleting attachments generated/needed by odoo"
Expand All @@ -84,38 +73,56 @@ def _compute_model_id(self):
active = fields.Boolean(default=True)
description = fields.Text()

@api.depends("model_ids")
def _compute_model_id(self):
for rule in self:
model_id = False
model = False

if rule.model_ids and len(rule.model_ids) == 1:
model_id = rule.model_ids.id
model = rule.model_id.model

rule.model_id = model_id
rule.model = model

@api.constrains("retention_time")
def retention_time_not_null(self):
for rule in self:
if not rule.retention_time:
raise exceptions.ValidationError(
_("The Retention Time can't be 0 days")
self.env._("The Retention Time can't be 0 days")
)

@api.constrains("inheriting_model")
def _check_inheriting_model(self):
for rule in self.filtered(lambda r: r.inheriting_model):
if rule.ttype != "attachment":
raise exceptions.ValidationError(

Check warning on line 101 in autovacuum_message_attachment/models/vacuum_rule.py

View check run for this annotation

Codecov / codecov/patch

autovacuum_message_attachment/models/vacuum_rule.py#L101

Added line #L101 was not covered by tests
_(
"Inheriting model cannot be used on rule where type is not attachment"
self.env._(
"Inheriting model cannot be used on "
"rule where type is not attachment"
)
)
if (
rule.inheriting_model
not in self.env["ir.attachment"]._inherits_children
):
raise exceptions.ValidationError(

Check warning on line 111 in autovacuum_message_attachment/models/vacuum_rule.py

View check run for this annotation

Codecov / codecov/patch

autovacuum_message_attachment/models/vacuum_rule.py#L111

Added line #L111 was not covered by tests
_("No inheritance of ir.attachment was found on model %s")
% rule.inheriting_model
self.env._(
"No inheritance of ir.attachment "
f"was found on model {rule.inheriting_model}"
)
)
attachment_field = self.env[rule.inheriting_model]._inherits.get(

Check warning on line 117 in autovacuum_message_attachment/models/vacuum_rule.py

View check run for this annotation

Codecov / codecov/patch

autovacuum_message_attachment/models/vacuum_rule.py#L117

Added line #L117 was not covered by tests
"ir.attachment"
)
if not attachment_field:
raise exceptions.ValidationError(

Check warning on line 121 in autovacuum_message_attachment/models/vacuum_rule.py

View check run for this annotation

Codecov / codecov/patch

autovacuum_message_attachment/models/vacuum_rule.py#L121

Added line #L121 was not covered by tests
_("Cannot find relation to ir.attachment on model %s")
% rule.inheriting_model
self.env._(
"Cannot find relation to ir.attachment "
f"on model {rule.inheriting_model}"
)
)

def _search_autovacuum_records(self):
Expand Down
7 changes: 4 additions & 3 deletions autovacuum_message_attachment/tests/test_vacuum_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import base64
from datetime import date, timedelta

from odoo import api, exceptions, registry
from odoo import api, exceptions
from odoo.modules.registry import Registry
from odoo.tests import common
from odoo.tools import DEFAULT_SERVER_DATE_FORMAT

Expand Down Expand Up @@ -161,12 +162,12 @@ def test_res_model_domain(self):
# we need to check the existence of the message in a new cursor because
# it its deleted in batch in separated cursor. The original test's cursor
# is not aware of the deletion otherwise.
with registry(self.env.cr.dbname).cursor() as new_cr:
with Registry(self.env.cr.dbname).cursor() as new_cr:
partner_new_env = partner.with_env(partner.env(cr=new_cr))
self.assertEqual(len(partner_new_env.message_ids), 1)

rule.write({"model_filter_domain": "[['name', '=', 'Test Partner']]"})
self.message_obj.autovacuum(ttype="message")
with registry(self.env.cr.dbname).cursor() as new_cr:
with Registry(self.env.cr.dbname).cursor() as new_cr:
partner_new_env = partner.with_env(partner.env(cr=new_cr))
self.assertEqual(len(partner_new_env.message_ids), 0)
27 changes: 9 additions & 18 deletions autovacuum_message_attachment/views/rule_vacuum.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
name="web_ribbon"
text="Archived"
bg_color="bg-danger"
attrs="{'invisible': [('active', '=', True)]}"
invisible="active"
/>
<field name="active" invisible="1" />
<group col="1">
Expand All @@ -20,28 +20,19 @@
<field name="company_id" />
<field name="retention_time" />
</group>
<group
col="4"
attrs="{'invisible': [('ttype', '!=', 'message')]}"
>
<field
name="message_type"
attrs="{'required': [('ttype', '=', 'message')]}"
/>
<group col="4" invisible="ttype != 'message'">
<field name="message_type" required="ttype == 'message'" />
<field name="empty_subtype" />
</group>
<group
string="Message Subtypes"
attrs="{'invisible': [('ttype', '!=', 'message')]}"
>
<group string="Message Subtypes" invisible="ttype != 'message'">
<field name="message_subtype_ids" nolabel="1" colspan="4" />
</group>
<group attrs="{'invisible': [('ttype', '!=', 'attachment')]}">
<group invisible="ttype != 'attachment'">
<field name="filename_pattern" />
<field name="inheriting_model" />
<field
name="empty_model"
attrs="{'invisible': ['|', ('model_ids', '!=', []), ('filename_pattern', '=', False)]}"
invisible="model_ids != [] or not filename_pattern"
/>
</group>
<group string="Models">
Expand All @@ -52,7 +43,7 @@
<field name="model" invisible="1" />
<field
name="model_filter_domain"
attrs="{'invisible': [('model_id', '=', False)]}"
invisible="not model_id"
widget="domain"
options="{'model': 'model'}"
/>
Expand All @@ -69,11 +60,11 @@
<field name="name">vacuum.rule.form.view</field>
<field name="model">vacuum.rule</field>
<field name="arch" type="xml">
<tree>
<list>
<field name="name" />
<field name="company_id" />
<field name="retention_time" />
</tree>
</list>
</field>
</record>
<record model="ir.actions.act_window" id="action_vacuum_rule">
Expand Down

0 comments on commit 79ce63a

Please sign in to comment.