我正在使用 Odoo 8 中的 qWeb 制作报告。这些生成的 PDF 文件以“默认”名称保存。我想为每个生成的文件设置一个特定的名称(不是在保存文件之后,而是在“生成”时间)。
那可能吗?如果是,怎么做?
提前致谢。
最佳答案
在 Odoo 8 中,您可以像下面这样(在 FIX START 和 END 之间)修补 addons/report/controllers/main.py 的方法 report_download。然后它将使用报告操作定义中附件属性的代码。由于系统将始终将文件作为附件保存在数据库中,因此您可以进一步更改行为以仅在附件使用设置为 True 时保存在数据库中。通过这种方式,不需要添加额外的字段和更改 View 。
@route(['/report/download'], type='http', auth="user")
def report_download(self, data, token):
"""This function is used by 'qwebactionmanager.js' in order to trigger the download of
a pdf/controller report.
:param data: a javascript array JSON.stringified containg report internal url ([0]) and
type [1]
:returns: Response with a filetoken cookie and an attachment header
"""
requestcontent = simplejson.loads(data)
url, type = requestcontent[0], requestcontent[1]
try:
if type == 'qweb-pdf':
reportname = url.split('/report/pdf/')[1].split('?')[0]
docids = None
if '/' in reportname:
reportname, docids = reportname.split('/')
if docids:
# Generic report:
response = self.report_routes(reportname, docids=docids, converter='pdf')
##### FIX START: switch reportname with the evaluated attachment attribute of the action if available
docids = [int(i) for i in docids.split(',')]
report_obj = request.registry['report']
cr, uid, context = request.cr, request.uid, request.context
report = report_obj._get_report_from_name(cr, uid, reportname)
if report.attachment:
obj = report_obj.pool[report.model].browse(cr, uid, docids[0])
reportname=eval(report.attachment, {'object': obj, 'time': time}).split('.pdf')[0]
##### FIX END
else:
# Particular report:
data = url_decode(url.split('?')[1]).items() # decoding the args represented in JSON
response = self.report_routes(reportname, converter='pdf', **dict(data))
response.headers.add('Content-Disposition', 'attachment; filename=%s.pdf;' % reportname)
response.set_cookie('fileToken', token)
return response
elif type =='controller':
reqheaders = Headers(request.httprequest.headers)
response = Client(request.httprequest.app, BaseResponse).get(url, headers=reqheaders, follow_redirects=True)
response.set_cookie('fileToken', token)
return response
else:
return
except Exception, e:
se = _serialize_exception(e)
error = {
'code': 200,
'message': "Odoo Server Error",
'data': se
}
return request.make_response(html_escape(simplejson.dumps(error)))
例如,您可以通过以下方式设置报告操作的附件属性:
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<!-- rename the file names of the standard rfq report -->
<record id="purchase.report_purchase_quotation" model="ir.actions.report.xml">
<field name="attachment">'RFQ_'+object.name+'.pdf'</field>
</record>
</data>
</openerp>
这是 addons/report/report.py 中 Report 对象的 _check_attachment 补丁,用于修改附件使用行为:
@api.v7
def _check_attachment_use(self, cr, uid, ids, report):
""" Check attachment_use field. If set to true and an existing pdf is already saved, load
this one now. Else, mark save it.
"""
save_in_attachment = {}
save_in_attachment['model'] = report.model
save_in_attachment['loaded_documents'] = {}
if report.attachment:
for record_id in ids:
obj = self.pool[report.model].browse(cr, uid, record_id)
filename = eval(report.attachment, {'object': obj, 'time': time})
# If the user has checked 'Reload from Attachment'
if report.attachment_use:
alreadyindb = [('datas_fname', '=', filename),
('res_model', '=', report.model),
('res_id', '=', record_id)]
attach_ids = self.pool['ir.attachment'].search(cr, uid, alreadyindb)
if attach_ids:
# Add the loaded pdf in the loaded_documents list
pdf = self.pool['ir.attachment'].browse(cr, uid, attach_ids[0]).datas
pdf = base64.decodestring(pdf)
save_in_attachment['loaded_documents'][record_id] = pdf
_logger.info('The PDF document %s was loaded from the database' % filename)
continue # Do not save this document as we already ignore it
# FIX START (commenting out below lines and indenting the else clause one level down)
# If the user has checked 'Save as Attachment Prefix'
#~ if filename is False:
#~ # May be false if, for instance, the 'attachment' field contains a condition
#~ # preventing to save the file.
#~ continue
else:
save_in_attachment[record_id] = filename # Mark current document to be saved
# FIX END
return save_in_attachment
关于report - 如何在 qWeb 报告、Odoo 中设置 PDF 名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30485464/