From 1e88b3f2bc4667205b970103a02ede6dd7b7e292 Mon Sep 17 00:00:00 2001 From: Sam Hasan Date: Wed, 17 Jul 2024 17:05:05 +0000 Subject: [PATCH] [ADD]web_well_known: manage web .well-known paths h14437 --- web_well_known/__init__.py | 4 +++ web_well_known/__manifest__.py | 18 ++++++++++ web_well_known/controllers/__init__.py | 3 ++ web_well_known/controllers/controllers.py | 24 +++++++++++++ web_well_known/models/__init__.py | 3 ++ web_well_known/models/models.py | 39 +++++++++++++++++++++ web_well_known/security/ir.model.access.csv | 2 ++ web_well_known/views/views.xml | 37 +++++++++++++++++++ 8 files changed, 130 insertions(+) create mode 100644 web_well_known/__init__.py create mode 100644 web_well_known/__manifest__.py create mode 100644 web_well_known/controllers/__init__.py create mode 100644 web_well_known/controllers/controllers.py create mode 100644 web_well_known/models/__init__.py create mode 100644 web_well_known/models/models.py create mode 100644 web_well_known/security/ir.model.access.csv create mode 100644 web_well_known/views/views.xml diff --git a/web_well_known/__init__.py b/web_well_known/__init__.py new file mode 100644 index 00000000..54f7525e --- /dev/null +++ b/web_well_known/__init__.py @@ -0,0 +1,4 @@ +# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details. + +from . import controllers +from . import models \ No newline at end of file diff --git a/web_well_known/__manifest__.py b/web_well_known/__manifest__.py new file mode 100644 index 00000000..6779c9bf --- /dev/null +++ b/web_well_known/__manifest__.py @@ -0,0 +1,18 @@ +# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details. + +{ + 'name': 'Well-known Paths', + 'description': 'Manage well-known paths', + 'version': '1.0', + 'author': 'Hibou Corp. ', + 'depends': [ + 'base' + ], + 'category': 'Tools', + 'data': [ + 'security/ir.model.access.csv', + 'views/views.xml', + ], + 'installable': True, + 'auto_install': True, +} diff --git a/web_well_known/controllers/__init__.py b/web_well_known/controllers/__init__.py new file mode 100644 index 00000000..28341811 --- /dev/null +++ b/web_well_known/controllers/__init__.py @@ -0,0 +1,3 @@ +# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details. + +from . import controllers diff --git a/web_well_known/controllers/controllers.py b/web_well_known/controllers/controllers.py new file mode 100644 index 00000000..558f5ebe --- /dev/null +++ b/web_well_known/controllers/controllers.py @@ -0,0 +1,24 @@ +# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details. + +from odoo import http +from odoo.http import request, content_disposition + +class WebWellKnown(http.Controller): + @http.route('/.well-known/', type='http', auth='public', methods=['GET'], csrf=False) + def well_known_path(self, path, **kwargs): + well_known_paths = request.env['well.known.path'].sudo() + well_known_record = well_known_paths.search([('path', '=', path)], limit=1) + + if not well_known_record: + return http.Response(status=404) # Return a 404 Not Found if the path does not exist + + if well_known_record.file: + text_content = well_known_record.file.decode('utf-8') + + return request.make_response( + text_content, # Decode binary content to a string + headers=[('Content-Type', 'text/plain; charset=utf-8')] + ) + + + return well_known_record.value or '' # Return the value if no binary content is present, or empty string if value is None diff --git a/web_well_known/models/__init__.py b/web_well_known/models/__init__.py new file mode 100644 index 00000000..ac585d19 --- /dev/null +++ b/web_well_known/models/__init__.py @@ -0,0 +1,3 @@ +# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details. + +from . import models \ No newline at end of file diff --git a/web_well_known/models/models.py b/web_well_known/models/models.py new file mode 100644 index 00000000..f7b74826 --- /dev/null +++ b/web_well_known/models/models.py @@ -0,0 +1,39 @@ +# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details. + +import mimetypes +from odoo import models, fields, api +from odoo.exceptions import ValidationError + +class WellKnownPath(models.Model): + _name = 'well.known.path' + _description = 'Well Known Path' + + path = fields.Char('Path', required=True, unique=True) + value = fields.Text('Value') + file = fields.Binary('Binary Content') + filename = fields.Char('File Name') + + + @api.onchange('file') + def _onchange_file(self): + for record in self: + if record.file: + if not record.filename: + record.filename = "well-known.txt" # Set a default filename if none is provided + if not record._is_text_file(): + raise ValidationError("Only text files are allowed.") + + def _is_text_file(self): + text_mime_types = [ + 'text/plain', + ] + file_mimetype = mimetypes.guess_type(self.filename)[0] + if file_mimetype in text_mime_types: + return True + return False + + @api.constrains('file', 'mimetype') + def _check_text_file(self): + for record in self: + if record.file and not record._is_text_file(): + raise ValidationError("Only text files are allowed.") \ No newline at end of file diff --git a/web_well_known/security/ir.model.access.csv b/web_well_known/security/ir.model.access.csv new file mode 100644 index 00000000..88a09e47 --- /dev/null +++ b/web_well_known/security/ir.model.access.csv @@ -0,0 +1,2 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +manage_well_known_path,manage_well_known_path,model_well_known_path,base.group_system,1,1,1,1 diff --git a/web_well_known/views/views.xml b/web_well_known/views/views.xml new file mode 100644 index 00000000..a1a2dabd --- /dev/null +++ b/web_well_known/views/views.xml @@ -0,0 +1,37 @@ + + + + well.known.path.tree + well.known.path + + + + + + + + + well.known.path.form + well.known.path + +
+ + + + + + + +
+
+
+ + + Well Known Paths + well.known.path + tree,form + + + + +