import json, uuid, requests, os from odoo import models, fields, api from odoo.exceptions import UserError from . import datasource from PyPDF2 import PdfFileWriter, PdfFileReader FIELD_TYPES = [(key, key) for key in sorted(fields.Field.by_type)] import tempfile ################# dir = tempfile.gettempdir() # dir='/opt/odoo' login = 'test' password = 'test' ################## class BaseModelExtend(models.AbstractModel): _name = 'base.model.extend' _description = '继承基类模型' def _register_hook(self): def GetUrl(self): self._cr.execute("select value from ir_config_parameter where key ='web.base.url'") dicts = self._cr.dictfetchall() return str(dicts[0]['value']) def getOdooToken(self): url = GetUrl(self) + "/web/session/authenticate" # 传入需要访问的数据库名称,登录账号与密码 data = { "params": { "db": self._cr.dbname, "login": login, "password": password, } } headers = {'Content-type': 'application/json'} response = requests.post(url, headers=headers, json=data) return response.cookies["session_id"] def report_pdf_prints(self, args, types): ''' PDF打印公共方法 :param self: :return: ''' # 模型数据ID # 模型名称 report_id = args print_type = 'pdf' if types == 0 else 'xlsx' data_id = self._context.get('active_ids', None) report_report = self.env['report.report'].browse(report_id) data_model = self._context.get('active_model', None) data_model_details = report_report.model_detail_id.model if report_report.report_type == 'bill' else '' if data_model is None: raise UserError('获取数据源失败') res_id = self.env[data_model].read_group([('id', 'in', data_id)], [report_report.group_field], [report_report.group_field]) headers = { 'Connection': 'keep-alive', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36', 'Cookie': 'session_id=' + getOdooToken(self) } inFileList = [] for s_code in res_id: s_id = [] wheres_id = self.env[data_model].search( [('id', 'in', data_id), (report_report.group_field, '=', s_code[report_report.group_field])]) for wid in wheres_id: s_id.append(wid.id) dic = {} dic['report'] = json.loads(report_report.report_data) dic['data'] = datasource.wms_datasource.get_data(self, report_id, s_id, data_model, data_model_details, report_report.model_main_key) dic['outputFormat'] = print_type dic['isTestData'] = True res = requests.post(self.GetUrl() + '/api/report/report_server/run', data={'data': str(json.dumps(dic))}, headers=headers, allow_redirects=True) print(res.text) key = str(res.text.split(':')[1]) url = self.GetUrl() + '/api/report/report_server/run?key=' + key + '&outputFormat=' + print_type r = requests.get(url, stream=True, cookies={'session_id': getOdooToken(self)}) pdf = "/%s.pdf" % key # 获取temp临时文件路径 pdf = dir + pdf inFileList.append(pdf) with open(pdf, "wb") as pdf: for chunk in r.iter_content(chunk_size=1024): if chunk: pdf.write(chunk) filename = dir + "/print.pdf" pdfFileWriter = PdfFileWriter() for inFile in inFileList: with open(inFile, 'rb') as f: pdfReader = PdfFileReader(f) numPages = pdfReader.getNumPages() for index in range(0, numPages): pageObj = pdfReader.getPage(index) pdfFileWriter.addPage(pageObj) pdfFileWriter.write(open(filename, 'wb')) os.unlink(inFile) with open(filename, 'rb') as pdf_document: pdf_content = pdf_document.read() os.unlink(filename) key_uid = str(uuid.uuid4()) self.env['wms.print.list'].create({'p_key': key_uid, 'report_file_pdf': pdf_content}) if print_type == 'pdf': # 打开新页面 # return { # 'type': 'ir.actions.act_url', # 'title':'打印预览', # 'name': '打印预览', # 'url': self.GetUrl() + '/api/report/report_server/run?key=' +key_uid+'&outputFormat=' + print_type + '&report_id='+str(args), # 'target': 'new' # # } return { 'type': 'ir.actions.act_window', 'name': '打印预览', 'res_model': 'wms.print.list', 'views': [[False, "form"]], 'target': 'new', 'context': { 'default_name': '