from .. import controller from odoo import http from odoo.http import request import base64 class Download(controller.Controller): PREFIX = '/api/v1/' def _get_attachment(self, model, field, id): result = request.env['ir.attachment'].sudo().search_read([ ('res_model', '=', model), ('res_field', '=', field), ('res_id', '=', id), ], ['datas', 'mimetype']) return result if len(result) > 0 else None @http.route(PREFIX + 'download/invoice//', auth='none', method=['GET']) def download_invoice(self, id, token): id = int(id) md5_valid = request.env['rest.api'].md5_salt_valid(id, 'account.move', token) if not md5_valid: return self.response('Unauthorized') pdf, type = request.env['ir.actions.report'].sudo().search([('report_name', '=', 'account.report_invoice')])._render_qweb_pdf([id]) return request.make_response(pdf, [('Content-Type', 'application/pdf')]) @http.route(PREFIX + 'download/tax-invoice//', auth='none', method=['GET']) def download_tax_invoice(self, id, token): id = int(id) md5_valid = request.env['rest.api'].md5_salt_valid(id, 'account.move', token) if not md5_valid: return self.response('Unauthorized') attachment = self._get_attachment('account.move', 'efaktur_document', id) if attachment: attachment = attachment[0] return request.make_response(base64.b64decode(attachment['datas']), [('Content-Type', attachment['mimetype'])]) return self.response('Dokumen tidak ditemukan', code=404)