121 lines
4.3 KiB
Python
121 lines
4.3 KiB
Python
# -*- coding: utf-8 -*-
|
||
|
||
from odoo import http
|
||
from odoo.http import request, Response
|
||
import base64
|
||
import json
|
||
|
||
|
||
class QualityController(http.Controller):
|
||
|
||
@http.route(['/api/quality/report/download'], type='http', auth='public', csrf=False, website=False) # 移除 cors="*"
|
||
def get_quality_report(self, retrospect_ref=None, **kwargs):
|
||
"""获取质检报告的下载接口
|
||
|
||
Args:
|
||
retrospect_ref: 追溯码
|
||
|
||
Returns:
|
||
直接返回文件下载响应
|
||
"""
|
||
try:
|
||
# 如果retrospect_ref为None,尝试从查询参数获取
|
||
if not retrospect_ref:
|
||
retrospect_ref = kwargs.get('retrospect_ref')
|
||
|
||
# 参数验证
|
||
if not retrospect_ref:
|
||
return self._json_response({
|
||
'status': 'error',
|
||
'message': '追溯码不能为空'
|
||
})
|
||
|
||
# 查找对应的质检单
|
||
quality_check = request.env['quality.check'].sudo().search([
|
||
('picking_id.retrospect_ref', '=', retrospect_ref),
|
||
('publish_status', '=', 'published') # 只返回已发布的报告
|
||
], limit=1)
|
||
|
||
if not quality_check:
|
||
return self._json_response({
|
||
'status': 'error',
|
||
'message': '未找到对应的质检报告或报告未发布'
|
||
})
|
||
|
||
if not quality_check.report_number_id:
|
||
return self._json_response({
|
||
'status': 'error',
|
||
'message': '质检报告文件不存在'
|
||
})
|
||
|
||
# 获取文件内容
|
||
document = quality_check.report_number_id
|
||
if not document.raw: # 检查文件内容是否存在
|
||
return self._json_response({
|
||
'status': 'error',
|
||
'message': '文件内容不存在'
|
||
})
|
||
|
||
# 构建文件名(确保有.pdf后缀)
|
||
filename = document.name
|
||
if not filename.lower().endswith('.pdf'):
|
||
filename = f"{filename}.pdf"
|
||
|
||
# 返回文件下载响应
|
||
return Response(
|
||
document.raw,
|
||
headers=[
|
||
('Content-Type', 'application/pdf'),
|
||
('Content-Disposition', f'attachment; filename="{filename}"'),
|
||
('Access-Control-Allow-Origin', '*'),
|
||
('Access-Control-Allow-Methods', 'GET, OPTIONS'),
|
||
('Access-Control-Allow-Headers', 'Content-Type, Authorization')
|
||
]
|
||
)
|
||
|
||
except Exception as e:
|
||
return self._json_response({
|
||
'status': 'error',
|
||
'message': f'系统错误: {str(e)}'
|
||
})
|
||
|
||
def _json_response(self, data):
|
||
"""返回JSON格式的响应"""
|
||
return Response(
|
||
json.dumps(data, ensure_ascii=False),
|
||
mimetype='application/json;charset=utf-8',
|
||
headers=[
|
||
('Access-Control-Allow-Origin', '*'),
|
||
('Access-Control-Allow-Methods', 'GET, OPTIONS'),
|
||
('Access-Control-Allow-Headers', 'Content-Type, Authorization')
|
||
]
|
||
)
|
||
|
||
|
||
class QualityReportController(http.Controller):
|
||
@http.route('/quality/report/<int:document_id>', type='http', auth='public')
|
||
def get_public_report(self, document_id, **kw):
|
||
"""提供公开访问PDF报告的控制器"""
|
||
document = request.env['documents.document'].sudo().browse(int(document_id))
|
||
|
||
# 安全检查:确保只有质检报告文档可以被访问
|
||
if document.exists() and document.res_model == 'quality.check':
|
||
# 获取PDF内容
|
||
pdf_content = document.raw
|
||
|
||
# 返回PDF内容
|
||
return request.make_response(
|
||
pdf_content,
|
||
headers=[
|
||
('Content-Type', 'application/pdf'),
|
||
('Content-Disposition', f'inline; filename={document.name}.pdf')
|
||
]
|
||
)
|
||
return request.not_found()
|
||
|
||
@http.route('/quality/report/not_published', type='http', auth='public')
|
||
def get_not_published_report(self, **kw):
|
||
"""提供未发布报告的控制器"""
|
||
return "报告尚未发布"
|
||
|