缓存报告附件,并清理旧附件

This commit is contained in:
mgw
2025-03-14 17:44:57 +08:00
parent 7755cc3982
commit 5d703b8b73
2 changed files with 32 additions and 1 deletions

View File

@@ -419,6 +419,36 @@ class QualityCheck(models.Model):
width=140, height=140)
)
)
def get_latest_report_attachment(self, check_id):
"""获取指定质检记录的最新报告附件,并删除旧的报告附件"""
# 查找特定质检记录的所有附件
attachments = self.env['ir.attachment'].search([
('res_model', '=', 'quality.check'),
('res_id', '=', check_id),
('name', 'like', 'QC-QC') # 根据您的命名规则调整
], order='create_date DESC') # 按创建日期降序排序
# 如果附件数量大于1则删除除最新报告外的其他报告附件
if len(attachments) > 1:
for attachment in attachments[1:]:
attachment.unlink()
# 返回最新的附件(如果存在)
return attachments and attachments[0] or False
def get_report_url(self):
"""生成报告访问URL确保获取最新版本"""
base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url')
report_url = f"{base_url}/web/content/ir.attachment"
# 获取最新附件的ID
latest_attachment = self.get_latest_report_attachment(self.id)
if latest_attachment:
# 生成包含附件ID的URL
print(f"{report_url}/{latest_attachment.id}/datas")
return f"{report_url}/{latest_attachment.id}/datas"
return False
@depends('product_id')
def _compute_material_name(self):