!12388 !12387 清理7天前的消息提醒日志记录

Merge pull request !12388 from 李鹏宇/dev
This commit is contained in:
李鹏宇
2026-07-10 02:25:25 +00:00
committed by Gitee
4 changed files with 43 additions and 0 deletions
+1
View File
@@ -21,6 +21,7 @@
'data/attendance_code_data.xml',
'data/attendance_time_data.xml',
'data/attendance_month_date.xml',
'data/clean_message_log_cron.xml',
'data/mail_template.xml',
'report/go_approval_report.xml',
'report/go_approval_templates.xml',
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<data noupdate="1">
<!-- 定时任务:每天早上6点执行,删除7天前的消息提醒日志 -->
<record forcecreate="True" id="cron_clean_attendance_message_log" model="ir.cron">
<field name="name">每天清理7天前的消息提醒日志</field>
<field name="model_id" ref="model_attendance_message_log"/>
<field name="state">code</field>
<field name="code">model.action_clean_old_logs()</field>
<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 eval="False" name="doall"/>
<!-- 首次执行时间设为次日早上6点,之后每隔24小时自动执行 -->
<field name="nextcall" eval="(datetime.now() + relativedelta(days=1, hour=6, minute=0, second=0)).strftime('%Y-%m-%d %H:%M:%S')"/>
</record>
</data>
</odoo>
@@ -3701,6 +3701,26 @@ class AttendanceMessageLog(models.Model):
}
}
def action_clean_old_logs(self):
"""清理7天前的消息提醒日志记录(定时任务和手动按钮共用)"""
# 计算截止日期:当前时间往前推7天
deadline = fields.Datetime.now() - timedelta(days=7)
# 搜索所有提醒时间早于截止日期的记录
old_logs = self.search([('update_date', '<', deadline)])
count = len(old_logs)
# 批量删除
old_logs.sudo().unlink()
# 返回清理结果通知(定时任务调用时自动忽略返回值)
return {
'type': 'ir.actions.client',
'tag': 'display_notification',
'params': {
'title': '清理完成',
'message': f'已删除 {count} 条7天前的日志记录',
'type': 'success',
}
}
@api.depends('employee_id')
def _compute_shift_id(self):
for rec in self:
@@ -4,6 +4,9 @@
<field name="model">attendance.message.log</field>
<field name="arch" type="xml">
<tree string="消息提醒日志" editable="bottom">
<header>
<button name="action_clean_old_logs" type="object" string="清理7天前日志" icon="fa-trash-o" class="oe_highlight" confirm="确认删除7天前的所有日志记录?"/>
</header>
<button name="action_recall_message" type="object" string="撤回" icon="fa-undo" class="oe_highlight"/>
<field name="employee_id"/>
<field name="shift_id"/>