odoo 响应下载文件
odoo中如何实现点击按钮下载文件报告、报表到页面左下角
models.py
# -*- coding: utf-8 -*-
from openerp import models, fields, api
import logging
try:
import xlwt
except ImportError:
xlwt = None
import json
import urllib2
_logger = logging.getLogger(__name__)
class test_module_template(models.Model):
_name = 'test.module.template'
_description = u'测试模块'
def request_pdf_from(self, data, response):
""" 从服务器请求pdf """
server_url = "https://zhaichangyuan.jsreportonline.net/api/report"
server_data = {
"template": {"name": "template-main"},
'Headers': 'Content-Type: application/json',
# 'subject': '测试jsreport接口',
# 'BODY': {
# # "name": "invoice-main",
# # "recipe": "html-to-xlsx",
# # "engine": "handlebars",
# # "content": "<h1>sample content</h1>",
# },
"data": {
"number": "FP201904120003",
"seller": {
"name": u"北京百星电子系统有限公司",
"road": u"北京·枢密院",
"country": u"中国.北京"
},
"buyer": {
"name": u"中国建筑研究设计院",
"road": u"北京市海淀区中关村创业大厦",
"country": u"中国.北京"
},
"items": [
{
"name": u"网页设计",
"price": 400.00,
"city": "Beijing"
},
{
"name": u"程序开发",
"price": 1500.00,
"city": "Shanghai"
},
{
"name": u"系统运维",
"price": 600.00,
"city": "Beijing"
},
{
"name": u"硬件维护",
"price": 700.00,
"city": "Shenzhen"
},
{
"name": u"AI智能",
"price": 300.00,
"city": "Shanghai"
},
{
"name": u"软件开发",
"price": 100.00,
"city": "Wuhan"
},
{
"name": u"程序测试",
"price": 800.00,
"city": "Shanghai"
}
]
},
"options": {"timeout": 60000}
}
headers = {
# "Accept": "application/pdf",
"Content-Type": "application/json",
"Authorization": "Basic d29ybWVyQHdvcm1lLmNuOklPUGlvcCooKTg5MA==",
}
request = urllib2.Request(server_url, data=json.dumps(server_data), headers=headers)
res = urllib2.urlopen(request)
#print res.read()
# file_Handle = open('report_test111.xlsx', 'w')
# file_Handle.write(response.read())
# file_Handle.close()
response.set_data(res.read())
return response
@api.multi
def action_print(self):
return {
'type': 'ir_actions_jsreport_xls_download',
'data': {
'model': '',
'options': '',
'report_name': ''
}
}
controllers/mains.py
# -*- coding: utf-8 -*-
import logging
import simplejson
from openerp import http
from openerp.http import request, Response
_logger = logging.getLogger(__name__)
class JsreportPrint(http.Controller):
@http.route('/jsreport/xls/download', type='http', auth='public', methods=['POST'], csrf=False)
def jsreport_xls_download(self, action, token, **kw):
_logger.info(action)
data = simplejson.loads(action)
options = data.get('data').get('options')
report_name = u"jsreport_report"
uid = request.session.uid
response = request.make_response(None,
headers=[('Content-Type', 'application/vnd.ms-excel'),
('Content-Disposition', 'attachment; filename=' + report_name + '.xlsx;')],
cookies={'fileToken': token})
obj = request.env['test.module.template'].sudo(uid)
return obj.request_pdf_from(options, response)
static/src/js/jsreport_xls_print.js
openerp.test_module_template = function (instance) {
instance.web.ActionManager = instance.web.ActionManager.extend({
ir_actions_jsreport_xls_download: function (action, options) {
var self = this;
instance.web.blockUI();
action = _.clone(action);
var c = instance.webclient.crashmanager;
console.log(action);
return $.Deferred(function (d) {
self.session.get_file({
url: '/jsreport/xls/download',
data: {action: JSON.stringify(action)},
complete: instance.web.unblockUI,
success: function () {
if (!self.dialog) {
options.on_close();
}
self.dialog_stop();
d.resolve();
},
error: function () {
c.rpc_error.apply(c, arguments);
d.reject();
}
});
});
}
})
}
views/template.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- vim:fdn=3:
-->
<openerp>
<data>
<template id="jsreport_xls_print" name="jsreport_xls_print" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<script type="text/javascript" src="/test_module_template/static/src/js/jsreport_xls_print.js"></script>
</xpath>
</template>
</data>
</openerp>
views/views.xml
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record id="test_module_template_form" model="ir.ui.view">
<field name="name">test.module.template.form</field>
<field name="model">test.module.template</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form>
<header>
<button name="action_print" class="oe_highlight" string="打印" type="object"/>
</header>
</form>
</field>
</record>
<record id="test_module_template_action" model="ir.actions.act_window">
<field name="name">测试</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">test.module.template</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="help" type="html">
<p class="oe_view_nocontent_create">测试单</p>
</field>
</record>
<menuitem name="主测试按钮" id="test_module_template_1" sequence="10"/>
<menuitem name="主测试按钮1" id="test_module_template_2" parent="test_module_template_1" sequence="10"/>
<menuitem name="测试按钮" id="test_module_template" sequence="10" parent="test_module_template_2"
action="test_module_template_action"/>
</data>
</openerp>