first push message
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# from . import models
|
||||
from . import res_config_settings
|
||||
from . import ir_attachment
|
||||
from . import res_users
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,102 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# ir_attachment.py
|
||||
from odoo import models, _
|
||||
from odoo.exceptions import UserError
|
||||
import base64
|
||||
|
||||
|
||||
def human_file_size(size_bytes):
|
||||
"""Convert bytes to human readable format"""
|
||||
if size_bytes == 0:
|
||||
return "0 B"
|
||||
size_name = ("B", "KB", "MB", "GB", "TB")
|
||||
import math
|
||||
i = int(math.floor(math.log(size_bytes, 1024)))
|
||||
p = math.pow(1024, i)
|
||||
s = round(size_bytes / p, 2)
|
||||
return "%s %s" % (s, size_name[i])
|
||||
|
||||
|
||||
class IrHttp(models.AbstractModel):
|
||||
_inherit = "ir.http"
|
||||
|
||||
def session_info(self):
|
||||
print("session_info called ✅")
|
||||
|
||||
session_info = super().session_info()
|
||||
|
||||
user = self.env.user
|
||||
current_user_id = user.id
|
||||
|
||||
user_id = self.env['res.users'].browse(current_user_id)
|
||||
|
||||
max_attachment_size_bytes = None
|
||||
|
||||
if user_id and user_id.user_select_attachment_size_unit and user_id.users_general_size_limit_attachment > 0:
|
||||
unit = user_id.user_select_attachment_size_unit
|
||||
size = user_id.users_general_size_limit_attachment
|
||||
|
||||
if unit == 'kb':
|
||||
max_attachment_size_bytes = size * 1024
|
||||
elif unit == 'mb':
|
||||
max_attachment_size_bytes = size * 1024 * 1024
|
||||
else:
|
||||
max_attachment_size_bytes = size * 1024 * 1024
|
||||
|
||||
else:
|
||||
enable_general_limit = self.env['ir.config_parameter'].sudo().get_param(
|
||||
'pys_attachment_size_limitation.enable_general_size_limit_attachment',
|
||||
False
|
||||
)
|
||||
|
||||
if enable_general_limit:
|
||||
unit = self.env["ir.config_parameter"].sudo().get_param(
|
||||
"pys_attachment_size_limitation.select_attachment_size_unit",
|
||||
)
|
||||
|
||||
size = int(
|
||||
self.env["ir.config_parameter"].sudo().get_param(
|
||||
"pys_attachment_size_limitation.general_size_limit_attachment",
|
||||
0
|
||||
)
|
||||
)
|
||||
|
||||
if unit and size > 0:
|
||||
if unit == 'kb':
|
||||
max_attachment_size_bytes = size * 1024
|
||||
elif unit == 'mb':
|
||||
max_attachment_size_bytes = size * 1024 * 1024
|
||||
else:
|
||||
max_attachment_size_bytes = size * 1024 * 1024
|
||||
|
||||
# Ensure it's an integer
|
||||
session_info["max_attachment_size"] = int(max_attachment_size_bytes) if max_attachment_size_bytes else None
|
||||
|
||||
print("max_attachment_size (in bytes):", session_info["max_attachment_size"])
|
||||
return session_info
|
||||
|
||||
|
||||
class IrAttachment(models.Model):
|
||||
_inherit = 'ir.attachment'
|
||||
|
||||
def create(self, vals_list):
|
||||
# Check size before creation
|
||||
for vals in vals_list:
|
||||
if vals.get('datas'):
|
||||
try:
|
||||
decoded_data = base64.b64decode(vals['datas'])
|
||||
max_size = self.env['ir.http'].session_info().get('max_attachment_size')
|
||||
|
||||
if max_size and max_size > 0 and len(decoded_data) > max_size:
|
||||
raise UserError(_(
|
||||
"Attachment size (%s) exceeds the maximum allowed size (%s)."
|
||||
) % (
|
||||
human_file_size(len(decoded_data)),
|
||||
human_file_size(max_size)
|
||||
))
|
||||
except Exception as e:
|
||||
if isinstance(e, UserError):
|
||||
raise
|
||||
# If base64 decoding fails, continue
|
||||
|
||||
return super().create(vals_list)
|
||||
@@ -0,0 +1,62 @@
|
||||
from odoo import models, _, api, fields
|
||||
from odoo.exceptions import UserError
|
||||
|
||||
|
||||
class IrAttachment(models.Model):
|
||||
_inherit = 'ir.attachment'
|
||||
|
||||
def create(self, vals_list):
|
||||
# 1. Get the limit from system parameters (set via res.config.settings)
|
||||
# Default to 2048 MB (2 GB) if not set in settings
|
||||
max_size_mb = float(self.env['ir.config_parameter'].sudo().get_param(
|
||||
'attachment_size.max_attachment_size', default=2048.0
|
||||
))
|
||||
|
||||
# 2. Convert MB to bytes
|
||||
max_size_bytes = max_size_mb * 1024 * 1024
|
||||
|
||||
# 3. Validate file size
|
||||
for vals in vals_list:
|
||||
if vals.get('datas'):
|
||||
# Base64 increases size by ~33%, so we check against max_size_bytes * 4 / 3
|
||||
if len(vals['datas']) > (max_size_bytes * 4 / 3):
|
||||
raise UserError(_(
|
||||
"File size exceeds the maximum allowed limit of %(max_size)s MB.",
|
||||
max_size=max_size_mb
|
||||
))
|
||||
|
||||
return super().create(vals_list)
|
||||
|
||||
|
||||
class FileSizeConfigSetting(models.TransientModel):
|
||||
_inherit = 'res.config.settings'
|
||||
|
||||
max_attachment_size = fields.Float(
|
||||
string="Maximum Attachment Size (MB)",
|
||||
config_parameter='attachment_size.max_attachment_size',
|
||||
help="Set the maximum size for attachments in MB. Default is 2048 MB (2 GB)."
|
||||
)
|
||||
|
||||
|
||||
class Http(models.AbstractModel):
|
||||
_inherit = "ir.http"
|
||||
|
||||
@api.model
|
||||
def session_info(self):
|
||||
session_info = super().session_info()
|
||||
|
||||
# Get the limit from system parameters, default to 2048 MB (2 GB)
|
||||
max_size_mb = float(self.env['ir.config_parameter'].sudo().get_param(
|
||||
'attachment_size.max_attachment_size', default=2048.0
|
||||
))
|
||||
|
||||
max_attachment_size_bytes = max_size_mb * 1024 * 1024 # Convert MB to Bytes
|
||||
|
||||
# Always update session_info so JS always has a value (no more undefined)
|
||||
session_info.update({
|
||||
"max_attachment_size": max_attachment_size_bytes,
|
||||
"max_size": max_size_mb,
|
||||
"max_file_upload_size": max_attachment_size_bytes,
|
||||
})
|
||||
|
||||
return session_info
|
||||
@@ -0,0 +1,42 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#res_config_settings.py
|
||||
from odoo import api, fields, models, _
|
||||
from odoo.exceptions import ValidationError
|
||||
|
||||
|
||||
class ResConfigSettings(models.TransientModel):
|
||||
_inherit = 'res.config.settings'
|
||||
|
||||
enable_general_size_limit_attachment = fields.Boolean(string="Enable Attachment Size Limitation",
|
||||
help='Enable Attachment Size Limitation',
|
||||
store=True,
|
||||
config_parameter="pys_attachment_size_limitation.enable_general_size_limit_attachment"
|
||||
)
|
||||
|
||||
general_size_limit_attachment = fields.Integer(string="Attachment Size Limit",
|
||||
help="Maximum allowed attachment size",
|
||||
store=True,
|
||||
config_parameter="pys_attachment_size_limitation.general_size_limit_attachment"
|
||||
)
|
||||
|
||||
select_attachment_size_unit = fields.Selection([('mb', 'MB'), ('kb', 'KB')],
|
||||
string="Attachment Size Unit",
|
||||
help="Select the unit used for attachment size limitation",
|
||||
config_parameter="pys_attachment_size_limitation.select_attachment_size_unit",
|
||||
store=True, default='kb'
|
||||
)
|
||||
|
||||
# In res_config_settings.py
|
||||
@api.constrains('general_size_limit_attachment')
|
||||
def _check_positive_attachment_size(self):
|
||||
for rec in self:
|
||||
if rec.general_size_limit_attachment is not None:
|
||||
if rec.general_size_limit_attachment < 0:
|
||||
raise ValidationError(
|
||||
_("Attachment size limit must be a positive value.")
|
||||
)
|
||||
# Optional: Add a reasonable maximum limit (e.g., 10GB)
|
||||
if rec.general_size_limit_attachment > 10485760: # 10GB in KB
|
||||
raise ValidationError(
|
||||
_("Attachment size limit cannot exceed 10 GB.")
|
||||
)
|
||||
@@ -0,0 +1,27 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#res_users.py
|
||||
from odoo import api, fields, models, _
|
||||
from odoo.exceptions import ValidationError
|
||||
|
||||
|
||||
class ResUsers(models.Model):
|
||||
_inherit = 'res.users'
|
||||
|
||||
users_general_size_limit_attachment = fields.Integer(string="Attachment Size Limit",
|
||||
help="Maximum allowed attachment size",
|
||||
store=True,
|
||||
config_parameter="pys_attachment_size_limitation.users_general_size_limit_attachment"
|
||||
)
|
||||
|
||||
user_select_attachment_size_unit = fields.Selection([('mb', 'MB'), ('kb', 'KB')],
|
||||
string="Attachment Size Unit", store=True,
|
||||
help="Select the unit used for attachment size limitation",
|
||||
default='kb')
|
||||
|
||||
@api.constrains('users_general_size_limit_attachment')
|
||||
def _check_positive_attachment_size(self):
|
||||
for rec in self:
|
||||
if rec.users_general_size_limit_attachment is not None and rec.users_general_size_limit_attachment < 0:
|
||||
raise ValidationError(
|
||||
_("Attachment size limit must be a positive value.")
|
||||
)
|
||||
Reference in New Issue
Block a user