Files
suite/web_well_known/models/models.py
2024-07-17 17:05:05 +00:00

39 lines
1.3 KiB
Python

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