From 0e3b590191e4767d4f6ebd07e4236344dddc7a12 Mon Sep 17 00:00:00 2001 From: Stepan Savelyev Date: Wed, 4 Dec 2019 16:48:47 +0500 Subject: [PATCH] [RM] controllers --- controllers/__init__.py | 1 - controllers/controllers.py | 16 ---------------- models/contract_wizard.py | 38 +++++++++++++++++++------------------- 3 files changed, 19 insertions(+), 36 deletions(-) delete mode 100644 controllers/__init__.py delete mode 100644 controllers/controllers.py diff --git a/controllers/__init__.py b/controllers/__init__.py deleted file mode 100644 index e046e49..0000000 --- a/controllers/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from . import controllers diff --git a/controllers/controllers.py b/controllers/controllers.py deleted file mode 100644 index 5f89162..0000000 --- a/controllers/controllers.py +++ /dev/null @@ -1,16 +0,0 @@ -# -*- coding: utf-8 -*- - -from werkzeug import datastructures - -from odoo import http - - -class ResPartnerContractBinary(http.Controller): - - @http.route('/web/binary/get_compiled_contract') - def download_compiled_contract(self, doc_id, doc_name): - contract_wizard = http.request.env['res.partner.contract.wizard'].sudo().browse(int(doc_id)) - file_content = contract_wizard.get_docx_contract_1().read() - headers = datastructures.Headers() - headers.add('Content-Disposition', 'attachment', filename=doc_name) - return http.request.make_response(file_content, headers) diff --git a/models/contract_wizard.py b/models/contract_wizard.py index 8447643..009a07b 100644 --- a/models/contract_wizard.py +++ b/models/contract_wizard.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +import base64 import io import math @@ -10,6 +11,7 @@ from odoo import api, fields, models from odoo.tools.config import config from pytils import numeral +from ..utils.docxtpl import get_document_from_values_stream class ContractWizard(models.TransientModel): _name = 'res.partner.contract.wizard' @@ -505,27 +507,25 @@ class ContractWizard(models.TransientModel): } return context - def get_docx_contract_1(self): - odoo_data_dir = config.get("data_dir") - odoo_bd = config.get("db_name") - filename = self.template.attachment_id.store_fname - full_path = '{}/filestore/{}/{}'.format( - odoo_data_dir, odoo_bd, filename) - context = self._generate_context() - doc = DocxTemplate(full_path) - doc.render(context) - stream = io.BytesIO() - doc.save(stream) - stream.seek(0) - return stream def get_docx_contract(self): - return { - 'type': 'ir.actions.act_url', - 'url': '/web/binary/get_compiled_contract?doc_id={}&doc_name={}.docx'.format(self.id, - self.contract_id.name), - 'target': 'self', - } + + path_to_template = "{}/filestore/{}/{}".format( + config.get("data_dir"), + config.get("db_name"), + self.template.attachment_id.store_fname + ) + fields = self._generate_context() + + binary_data = get_document_from_values_stream(path_to_template, fields).read() + encoded_data = base64.b64encode(binary_data) + + attachment = self.env['ir.attachment'].create({ + "name": "Contract-{}.doc".format(self.contract_id.name), + "type": "binary", + "datas": binary_data, + }) + return attachment class AnnexLine(models.TransientModel):