File cleanup

This commit is contained in:
2026-07-13 14:52:53 +02:00
parent 4f9b0e7907
commit f2d4064c5e
10 changed files with 169 additions and 11 deletions

View File

@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from . import models
from . import hooks
from . import models

View File

@@ -11,9 +11,10 @@
'category': 'Tools',
'license': "AGPL-3",
'version': '19.0.1.0.0',
'depends': ['mail','base','mailcatcher_menu',],
'depends': ['mail', 'base', 'mailcatcher_menu'],
'data': [
'views/templates.xml',
'data/ir_cron.xml',
],
'post_init_hook': 'post_init_hook',
}

View File

@@ -1,10 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="ir_cron_set_db_parameter" model="ir.cron">
<field name="name">Testserver: Set Date (ONLY FOR TEST ENV.)</field>
<field name="model_id" ref="base.model_ir_logging"/>
<field name="state">code</field>
<field name="code">model.set_db_param()</field>
<field name="interval_type">days</field>
</record>
</odoo>
<data noupdate="1">
<record id="ir_cron_set_db_parameter" model="ir.cron">
<field name="name">Testserver: Set Date (ONLY FOR TEST ENV.)</field>
<field name="model_id" ref="base.model_ir_logging"/>
<field name="state">code</field>
<field name="code">model.set_db_param()</field>
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="numbercall">-1</field>
<field name="user_id" ref="base.user_root"/>
<field name="active">True</field>
</record>
<record id="ir_cron_cleanup_filestore_attachments" model="ir.cron">
<field name="name">Testserver: Cleanup Unneeded Filestore</field>
<field name="model_id" ref="base.model_ir_logging"/>
<field name="state">code</field>
<field name="code">model.cleanup_unneeded_filestore_attachments(100)</field>
<field name="interval_number">15</field>
<field name="interval_type">minutes</field>
<field name="numbercall">-1</field>
<field name="user_id" ref="base.user_root"/>
<field name="active">True</field>
</record>
</data>
</odoo>

8
testserver_o2b/hooks.py Normal file
View File

@@ -0,0 +1,8 @@
# -*- coding: utf-8 -*-
from odoo import SUPERUSER_ID, api
def post_init_hook(cr, registry):
env = api.Environment(cr, SUPERUSER_ID, {})
env['ir.logging'].sudo().cleanup_unneeded_filestore_attachments(limit=50)

View File

@@ -21,6 +21,7 @@ class IrLogging(models.Model):
EXPECTED_IP_PREFIX = '192.168.78.'
REPORT_URL = 'http://127.0.0.1:8069'
ENTERPRISE_CODE = 'M160129258730'
FILESTORE_CLEANUP_BATCH_LIMIT = 100
def init(self):
database_uuid = str(uuid.uuid1())
@@ -80,6 +81,7 @@ class IrLogging(models.Model):
"INSERT INTO ir_config_parameter(key, value) VALUES (%s, %s);",
(key, value),
)
def _disable_web_push_notifications(self):
parameter = self.env['ir.config_parameter'].sudo()
parameter.set_param('web_push.notification.enabled', '0')
@@ -88,7 +90,7 @@ class IrLogging(models.Model):
('key', 'in', ('mail.web_push_vapid_private_key', 'mail.web_push_vapid_public_key', 'mail.sfu_server_key'))])
for parameter in parameters:
parameter.sudo().unlink()
if self.env['ir.model'].search([('model','=','mail.partner.device')]):
if self.env['ir.model'].search([('model', '=', 'mail.partner.device')]):
self.env.cr.execute("""DELETE FROM mail_partner_device;""")
def _model_has_field(self, model_name, field_name=None):
@@ -163,6 +165,34 @@ class IrLogging(models.Model):
if addon_path.endswith('testserver'):
_logger.error(os.system(f'./{addon_path}/testserver.sh'))
def _get_unneeded_filestore_attachment_ids(self, limit):
self.env.cr.execute(
"""
SELECT id
FROM ir_attachment
WHERE type = 'binary'
AND res_field IS NULL
AND store_fname IS NOT NULL
ORDER BY COALESCE(file_size, 0) DESC, id ASC
LIMIT %s
""",
(limit,),
)
return [row[0] for row in self.env.cr.fetchall()]
def cleanup_unneeded_filestore_attachments(self, limit=FILESTORE_CLEANUP_BATCH_LIMIT):
limit = int(limit or 0)
if limit <= 0:
return 0
attachment_ids = self._get_unneeded_filestore_attachment_ids(limit)
if not attachment_ids:
return 0
self.env['ir.attachment'].sudo().browse(attachment_ids).unlink()
_logger.info('Deleted %s unneeded filestore attachments', len(attachment_ids))
return len(attachment_ids)
def set_db_param(self):
## Script to set database expiration date, when enterprise and test env.
parameter = self.env['ir.config_parameter'].search(