def html_report(self,req,action,mods = None): action = simplejson.loads(action) report_srv = req.session.proxy("report") context = dict(req.context) context.update(action["context"]) report_data = {} report_ids = context["active_ids"] if 'report_type' in action: report_data['report_type'] = action['report_type'] if 'datas' in action: if 'ids' in action['datas']: report_ids = action['datas'].pop('ids') report_data.update(action['datas']) report_id = report_srv.report( req.session._db, req.session._uid, req.session._password, action["report_name"], report_ids, report_data, context) report_struct = None while True: report_struct = report_srv.report_get( req.session._db, req.session._uid, req.session._password, report_id) if report_struct["state"]: break time.sleep(self.POLLING_DELAY) report = base64.b64decode(report_struct['result']) if report_struct.get('code') == 'zlib': report = zlib.decompress(report) report_mimetype = self.TYPES_MAPPING.get( report_struct['format'], 'octet-stream') file_name = action.get('name', 'report') if 'name' not in action: reports = req.session.model('ir.actions.report.xml') res_id = reports.search([('report_name', '=', action['report_name']),], 0, False, False, context) if len(res_id) > 0: file_name = reports.read(res_id[0], ['name'], context)['name'] else: file_name = action['report_name'] file_name = '%s.%s' % (file_name, report_struct['format']) #html报表直接打印 html_header = [('Content-Type', report_mimetype),('Content-Length', len(report))] if report_struct['format'] not in ['html','html2html','mako2html']: html_header.append(('Content-Disposition', content_disposition(file_name, req))) return {'result' : report}
def index(self, req, data, token): model, fields, ids, domain, import_compat, context = \ operator.itemgetter('model', 'fields', 'ids', 'domain', 'import_compat', 'context')( simplejson.loads(data)) Model = req.session.model(model) ids = ids or Model.search(domain, 0, False, False, context) field_names = map(operator.itemgetter('name'), fields) import_data = Model.export_data(ids, field_names, context).get('datas',[]) if import_compat: columns_headers = field_names else: columns_headers = [val['label'].strip() for val in fields] return req.make_response(self.from_data(columns_headers, import_data), headers=[('Content-Disposition', content_disposition(self.filename(model), req)), ('Content-Type', self.content_type)], cookies={'fileToken': token})
def index(self, req, data, token): model, fields, ids, domain, import_compat, context = operator.itemgetter( "model", "fields", "ids", "domain", "import_compat", "context" )(simplejson.loads(data)) Model = req.session.model(model) ids = ids or Model.search(domain, 0, False, False, context) field_names = map(operator.itemgetter("name"), fields) import_data = Model.export_data(ids, field_names, context).get("datas", []) if import_compat: columns_headers = field_names else: columns_headers = [val["label"].strip() for val in fields] return req.make_response( self.from_data(columns_headers, import_data), headers=[ ("Content-Disposition", content_disposition(self.filename(model), req)), ("Content-Type", self.content_type), ], cookies={"fileToken": token}, )
def report_index(self, req, action, token): ''' 重写报表的方法 ''' action = simplejson.loads(action) report_srv = req.session.proxy("report") context = dict(req.context) context.update(action["context"]) report_data = {} report_ids = context["active_ids"] if 'report_type' in action: report_data['report_type'] = action['report_type'] if 'datas' in action: if 'ids' in action['datas']: report_ids = action['datas'].pop('ids') report_data.update(action['datas']) report_id = report_srv.report( req.session._db, req.session._uid, req.session._password, action["report_name"], report_ids, report_data, context) report_struct = None while True: report_struct = report_srv.report_get( req.session._db, req.session._uid, req.session._password, report_id) if report_struct["state"]: break time.sleep(self.POLLING_DELAY) report = base64.b64decode(report_struct['result']) if report_struct.get('code') == 'zlib': report = zlib.decompress(report) report_mimetype = self.TYPES_MAPPING.get( report_struct['format'], 'octet-stream') file_name = None #判断是否是导出到excel if action.get('report_name').endswith('xls') and action['report_type'] in ['html','mako2html','html2html']: file_name = action.get('report_name', 'report') _logger.debug("report = %s" % report) etree_obj = etree.HTML(report) #获取要导出的table export_excels = [etree.tostring(t) for t in etree_obj.xpath(u"//table")] _logger.debug("export excels = %s" % export_excels) report = "".join(export_excels) else: if 'name' not in action: reports = req.session.model('ir.actions.report.xml') res_id = reports.search([('report_name', '=', action['report_name']),], 0, False, False, context) if len(res_id) > 0: file_name = reports.read(res_id[0], ['name'], context)['name'] else: file_name = action['report_name'] file_name = '%s.%s' % (file_name, report_struct['format']) return req.make_response(report, headers=[ ('Content-Disposition', content_disposition(file_name, req)), ('Content-Type', report_mimetype), ('Content-Length', len(report))], cookies={'fileToken': int(token)})