缓存报告附件,并清理旧附件
This commit is contained in:
@@ -420,6 +420,36 @@ class QualityCheck(models.Model):
|
||||
)
|
||||
)
|
||||
|
||||
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):
|
||||
for record in self:
|
||||
|
||||
@@ -9,7 +9,8 @@
|
||||
<field name="print_report_name">'QC-' + object.name + '.pdf'</field>
|
||||
<field name="binding_model_id" ref="model_quality_check"/> <!-- 请替换为实际的模型ID引用 -->
|
||||
<field name="binding_type">report</field>
|
||||
<field name="attachment_use">False</field>
|
||||
<field name="attachment">'QC-'+object.name+'-'+str(object.write_date)+'.pdf'</field>
|
||||
<field name="attachment_use">True</field>
|
||||
</record>
|
||||
|
||||
<!-- 定义HTML预览报告动作 -->
|
||||
|
||||
Reference in New Issue
Block a user