Public release from ruodoo-project: 19.0 - 2026-05-31 21:19:12 UTC
This commit is contained in:
2
custom_report_field/models/__init__.py
Normal file
2
custom_report_field/models/__init__.py
Normal file
@ -0,0 +1,2 @@
|
||||
from . import custom_report_field
|
||||
from . import ir_actions_report
|
||||
106
custom_report_field/models/custom_report_field.py
Normal file
106
custom_report_field/models/custom_report_field.py
Normal file
@ -0,0 +1,106 @@
|
||||
# import logging
|
||||
|
||||
from odoo import api, exceptions, fields, models
|
||||
from odoo.exceptions import ValidationError
|
||||
from odoo.tools.safe_eval import safe_eval, test_python_expr
|
||||
|
||||
from odoo.addons.report_monetary_helpers.utils.format_number import format_number
|
||||
|
||||
# _logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class CustomReportField(models.Model):
|
||||
_name = "custom.report.field"
|
||||
_description = "Custom report field"
|
||||
_order = "sequence"
|
||||
|
||||
ir_actions_report_id = fields.Many2one(
|
||||
"ir.actions.report",
|
||||
string="Report",
|
||||
)
|
||||
sequence = fields.Integer(
|
||||
string="Sequence",
|
||||
)
|
||||
name = fields.Char(
|
||||
string="Name",
|
||||
required=True,
|
||||
translate=True,
|
||||
)
|
||||
technical_name = fields.Char(
|
||||
string="Technical Name",
|
||||
help="Name for using in templates",
|
||||
required=True,
|
||||
)
|
||||
default_value = fields.Text(
|
||||
string="Code",
|
||||
help="This field contains a Python code which can use the same variables as"
|
||||
" the 'code' field in the server action, except moment that the computation"
|
||||
" result should be assigned into the 'value' variable."
|
||||
"Also available method 'format_number' for formatting numbers representation in report. Returns number as string.",
|
||||
required=True,
|
||||
default="# Write a code for computing value for this field\n# and assign it into 'value' variable.",
|
||||
)
|
||||
description = fields.Char(
|
||||
string="Description",
|
||||
help="Description for this field to be showed in fields list in verify wizard.",
|
||||
translate=True,
|
||||
default="",
|
||||
)
|
||||
required = fields.Boolean(
|
||||
string="Required",
|
||||
help="If checked, it will not be possible to generate a document without a default value.",
|
||||
default=False,
|
||||
)
|
||||
visible = fields.Boolean(
|
||||
string="Show in wizard",
|
||||
help="To show this field in fields list in verification wizard.",
|
||||
default=True,
|
||||
)
|
||||
|
||||
@api.constrains("default_value")
|
||||
def _check_default_value(self):
|
||||
for rec in self.filtered("default_value"):
|
||||
msg = test_python_expr(expr=rec.default_value.strip(), mode="exec")
|
||||
if msg:
|
||||
raise ValidationError(msg)
|
||||
|
||||
def _get_eval_context(self, action=None):
|
||||
eval_context = self.env["ir.actions.actions"]._get_eval_context(action=action)
|
||||
eval_context.update(self.env.context)
|
||||
model_name = action.model_id.sudo().model
|
||||
model = self.env[model_name]
|
||||
record = None
|
||||
records = None
|
||||
if self._context.get("active_model") == model_name and self._context.get(
|
||||
"active_id"
|
||||
):
|
||||
record = model.browse(self._context["active_id"])
|
||||
if self._context.get("active_model") == model_name and self._context.get(
|
||||
"active_ids"
|
||||
):
|
||||
records = model.browse(self._context["active_ids"])
|
||||
eval_context.update(
|
||||
{
|
||||
# orm
|
||||
"env": self.env,
|
||||
"model": model,
|
||||
# Exceptions
|
||||
"Warning": exceptions.Warning,
|
||||
"UserError": exceptions.UserError,
|
||||
# record
|
||||
"record": record,
|
||||
"records": records,
|
||||
# utils
|
||||
"format_number": format_number,
|
||||
}
|
||||
)
|
||||
return eval_context
|
||||
|
||||
def compute_value(self, report_rec):
|
||||
_eval_context = self._get_eval_context(report_rec)
|
||||
safe_eval(self.default_value.strip(), _eval_context, mode="exec", nocopy=True)
|
||||
return (
|
||||
_eval_context["value"]
|
||||
if _eval_context.get("value", "key absent") != "key absent"
|
||||
else ""
|
||||
)
|
||||
59
custom_report_field/models/ir_actions_report.py
Normal file
59
custom_report_field/models/ir_actions_report.py
Normal file
@ -0,0 +1,59 @@
|
||||
from odoo import api, fields, models
|
||||
from odoo.exceptions import UserError
|
||||
|
||||
|
||||
class IrActionsReport(models.Model):
|
||||
_inherit = "ir.actions.report"
|
||||
|
||||
custom_report_field_ids = fields.One2many(
|
||||
comodel_name="custom.report.field",
|
||||
inverse_name="ir_actions_report_id",
|
||||
string="Custom report fields",
|
||||
)
|
||||
validate_custom_report_field = fields.Boolean(
|
||||
compute="_compute_validate_custom_report_field"
|
||||
)
|
||||
|
||||
@api.depends("custom_report_field_ids")
|
||||
def _compute_validate_custom_report_field(self):
|
||||
self.validate_custom_report_field = bool(
|
||||
self.custom_report_field_ids.filtered("visible")
|
||||
)
|
||||
|
||||
def _get_readable_fields(self):
|
||||
return super()._get_readable_fields() | {
|
||||
"validate_custom_report_field",
|
||||
}
|
||||
|
||||
def get_custom_report_field_values(self):
|
||||
"""
|
||||
Returns: dict with computed custom fields values.
|
||||
"""
|
||||
# Bunch reports creation not supported for a while.
|
||||
self.ensure_one()
|
||||
report_custom_field_ids = self.env["custom.report.field"].search(
|
||||
[("ir_actions_report_id", "=", self.id)]
|
||||
)
|
||||
custom_report_field_values = {}
|
||||
for field_rec in report_custom_field_ids:
|
||||
field_value = field_rec.compute_value(self)
|
||||
if field_rec.required and not field_value:
|
||||
raise UserError(
|
||||
"""Required custom field %s of the report is not filled. It's value: %s.
|
||||
Fill it or remove "required" attribute if this value is ok."""
|
||||
% (field_rec.name, field_value)
|
||||
)
|
||||
custom_report_field_values[field_rec.technical_name] = field_value
|
||||
return custom_report_field_values
|
||||
|
||||
@api.model
|
||||
def _get_rendering_context(self, report, docids, data):
|
||||
"""
|
||||
Redefined here to add custom report fields in context.
|
||||
:param docids: source documents IDs list
|
||||
:param data: dict containing current context
|
||||
:return: dict, updated context.
|
||||
"""
|
||||
data = super()._get_rendering_context(report, docids, data)
|
||||
data.update(report.get_custom_report_field_values())
|
||||
return data
|
||||
Reference in New Issue
Block a user