diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2023-02-10 10:36:16 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2023-02-10 10:36:16 +0700 |
| commit | bbf176b0ce51ade22b74d0df2023025a4cef3efa (patch) | |
| tree | 24d2d3c7e3fed5c0672a9063a09e3c4d33ba3dac /indoteknik_api/controllers/api_v1/download.py | |
| parent | bd01d7a842c8b6e4aea6a2fc3615a9d57fbcd470 (diff) | |
| parent | b0de64ae769148a009d0a08a957c5c35dee174a9 (diff) | |
Merge branch 'release' into line_no_sales_order
Diffstat (limited to 'indoteknik_api/controllers/api_v1/download.py')
| -rw-r--r-- | indoteknik_api/controllers/api_v1/download.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/indoteknik_api/controllers/api_v1/download.py b/indoteknik_api/controllers/api_v1/download.py new file mode 100644 index 00000000..d9353896 --- /dev/null +++ b/indoteknik_api/controllers/api_v1/download.py @@ -0,0 +1,52 @@ +from .. import controller +from odoo import http +from odoo.http import request + + +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[0] if len(result) > 0 else None + + @http.route(PREFIX + 'download/invoice/<id>/<token>', auth='none', method=['GET']) + def download_invoice(self, id, token): + id = int(id) + + rest_api = request.env['rest.api'] + md5_valid = rest_api.md5_salt_valid(id, 'account.move', token) + if not md5_valid: + return self.response('Unauthorized') + + account_move = request.env['account.move'].sudo().search_read([('id', '=', id)], ['name']) + pdf, type = request.env['ir.actions.report'].sudo().search([('report_name', '=', 'account.report_invoice')])._render_qweb_pdf([id]) + return rest_api.response_attachment({ + 'content': pdf, + 'mimetype': 'application/pdf', + 'filename': account_move[0]['name'] + }) + + @http.route(PREFIX + 'download/tax-invoice/<id>/<token>', auth='none', method=['GET']) + def download_tax_invoice(self, id, token): + id = int(id) + + rest_api = request.env['rest.api'] + md5_valid = rest_api.md5_salt_valid(id, 'account.move', token) + if not md5_valid: + return self.response('Unauthorized') + + account_move = request.env['account.move'].sudo().search_read([('id', '=', id)], ['name']) + attachment = self._get_attachment('account.move', 'efaktur_document', id) + if attachment and len(account_move) > 0: + return rest_api.response_attachment({ + 'content': attachment['datas'], + 'decode_content': True, + 'mimetype': attachment['mimetype'], + 'filename': account_move[0]['name'], + }) + return self.response('Dokumen tidak ditemukan', code=404) |
