Files
reporting-engine/board_eval_context/models/board_board.py
Alexandre Fayolle 9a46a1323d [ADD] module board_eval_context
This module adds some useful keys in the evaluation context of board.board
records, which can be useful when creating generic boards for all the users of
an instance.

At the moment it allows using the following values in the domains of a board.board record:

* datetime: the datetime.datetime class
* date: the datetime.date class
* timedelta: the datetime.timedelta class
* timezone: the datetime.timezone class
* tzinfo: the datetime.tzinfo class
* relativedelta: the dateutil.relativedelta.relativedelta class
* uid: the ID of the current user
2023-02-24 17:03:18 +01:00

49 lines
1.6 KiB
Python

# Copyright 2021-2023 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import datetime
from dateutil.relativedelta import relativedelta
from odoo import api, models
from odoo.tools.safe_eval import safe_eval
class Board(models.AbstractModel):
_inherit = "board.board"
def _get_eval_context(self):
"""Prepare the context used when evaluating python code
:returns: dict -- evaluation context given to safe_eval
"""
return {
"datetime": datetime.datetime,
"date": datetime.date,
"timedelta": datetime.timedelta,
"timezone": datetime.timezone,
"tzinfo": datetime.tzinfo,
"relativedelta": relativedelta,
"uid": self.env.uid,
}
@api.model
def _arch_preprocessing(self, arch):
# keeping it inside the method as in the
# original code, for perf reason at startup
from lxml import etree
arch = super()._arch_preprocessing(arch)
eval_context = self._get_eval_context()
def fix_domain(node):
for child in node.iterchildren():
if child.tag == "action" and child.get("domain"):
domain = safe_eval(child.get("domain"), eval_context)
child.set("domain", str(domain))
else:
fix_domain(child)
return node
archnode = etree.fromstring(arch)
archnode = fix_domain(archnode)
return etree.tostring(archnode, pretty_print=True, encoding="unicode")