[ADD]web_well_known: manage web .well-known paths

h14437
This commit is contained in:
Sam Hasan
2024-07-17 17:05:05 +00:00
parent 4f2049e976
commit 1e88b3f2bc
8 changed files with 130 additions and 0 deletions

View File

@@ -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

View File

@@ -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. <hello@hibou.io>',
'depends': [
'base'
],
'category': 'Tools',
'data': [
'security/ir.model.access.csv',
'views/views.xml',
],
'installable': True,
'auto_install': True,
}

View File

@@ -0,0 +1,3 @@
# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details.
from . import controllers

View File

@@ -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/<path:path>', 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

View File

@@ -0,0 +1,3 @@
# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details.
from . import models

View File

@@ -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.")

View File

@@ -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
1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
2 manage_well_known_path manage_well_known_path model_well_known_path base.group_system 1 1 1 1

View File

@@ -0,0 +1,37 @@
<odoo>
<record id="view_well_known_path_tree" model="ir.ui.view">
<field name="name">well.known.path.tree</field>
<field name="model">well.known.path</field>
<field name="arch" type="xml">
<tree>
<field name="path"/>
</tree>
</field>
</record>
<record id="view_well_known_path_form" model="ir.ui.view">
<field name="name">well.known.path.form</field>
<field name="model">well.known.path</field>
<field name="arch" type="xml">
<form>
<sheet>
<group>
<field name="path"/>
<field name="value"/>
<field name="file"/>
</group>
</sheet>
</form>
</field>
</record>
<record id="action_well_known_path" model="ir.actions.act_window">
<field name="name">Well Known Paths</field>
<field name="res_model">well.known.path</field>
<field name="view_mode">tree,form</field>
</record>
<menuitem id="well_know_paths_menu" name="Well-Known Paths" parent="base.menu_ir_property" action="action_well_known_path"/>
</odoo>