diff --git a/odex25_ensan/odex_benefit/controllers/__init__.py b/odex25_ensan/odex_benefit/controllers/__init__.py index 5610f61db..80c563f51 100644 --- a/odex25_ensan/odex_benefit/controllers/__init__.py +++ b/odex25_ensan/odex_benefit/controllers/__init__.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- from . import benefit from . import benefit_services +from . import attchment_preview diff --git a/odex25_ensan/odex_benefit/controllers/attchment_preview.py b/odex25_ensan/odex_benefit/controllers/attchment_preview.py new file mode 100644 index 000000000..0d5517fb3 --- /dev/null +++ b/odex25_ensan/odex_benefit/controllers/attchment_preview.py @@ -0,0 +1,30 @@ +import base64 +from odoo import http +from odoo.http import request +import urllib.parse # For URL encoding + + +class MyAttachmentController(http.Controller): + @http.route('/browse/document/', type='http', auth="public") + def browse_document(self, id): + model = 'ir.attachment' + attachment = request.env[model].sudo().browse(id) + + if not attachment.exists() or not attachment.datas: + return request.not_found() + + # Decode the file content + file_data = base64.b64decode(attachment.datas) + file_mimetype = attachment.mimetype or 'application/octet-stream' + + # URL-encode the filename for non-ASCII characters + filename = urllib.parse.quote(attachment.name) + + # Set HTTP headers with the URL-encoded filename + http_headers = [ + ('Content-Type', file_mimetype), + ('Content-Length', str(len(file_data))), + ('Content-Disposition', f'inline; filename*=UTF-8\'\'{filename}') + ] + + return request.make_response(file_data, headers=http_headers) diff --git a/odex25_ensan/odex_benefit/models/ir_attachment_inherit.py b/odex25_ensan/odex_benefit/models/ir_attachment_inherit.py index 6fcf89cc2..db157a02c 100644 --- a/odex25_ensan/odex_benefit/models/ir_attachment_inherit.py +++ b/odex25_ensan/odex_benefit/models/ir_attachment_inherit.py @@ -22,6 +22,14 @@ class BenefitAttachment(models.Model): is_required = fields.Boolean(string='Is Required?') is_default = fields.Boolean(string='Is Default?') + def action_preview_attachment(self): + # Custom function to open the preview + return { + 'type': 'ir.actions.act_url', + 'url': f'/browse/document/{self.id}', + 'target': 'new', # Opens in a new tab + } + @api.depends('expiration_date') def get_status(self): for rec in self: @@ -61,4 +69,5 @@ class BenefitAttachment(models.Model): def onchange_disabilities_id(self): for rec in self: if rec.disabilities_id: - rec.name = rec.disabilities_id.name \ No newline at end of file + rec.name = rec.disabilities_id.name + diff --git a/odex25_ensan/odex_benefit/views/benefit_view.xml b/odex25_ensan/odex_benefit/views/benefit_view.xml index 7c359014c..fe4be3ae5 100644 --- a/odex25_ensan/odex_benefit/views/benefit_view.xml +++ b/odex25_ensan/odex_benefit/views/benefit_view.xml @@ -572,7 +572,8 @@ - + +