2023-01-10 14:30:06 +01:00
|
|
|
# Copyright 2019-2023 Fabien Bourgeois <fabien@yaltik.com>
|
2021-02-22 18:08:11 +01:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2022-12-09 11:12:59 +01:00
|
|
|
""" Odoo XML DSL """
|
2021-02-22 18:08:11 +01:00
|
|
|
|
2022-12-09 11:12:59 +01:00
|
|
|
from .xml_base import xmlroot, xmln
|
2021-02-22 18:08:11 +01:00
|
|
|
|
|
|
|
# XML helpers functions and macros
|
|
|
|
|
|
|
|
# Odoo root XML Node
|
|
|
|
odoo = lambda children: xmlroot({'tag': 'odoo', 'attrs': {}, 'children': children})
|
|
|
|
|
|
|
|
# Special data Node
|
|
|
|
def data(*args):
|
|
|
|
""" Allow optional args on data tag """
|
|
|
|
if len(args) == 1:
|
|
|
|
args = list(args)
|
|
|
|
args.insert(0, {})
|
|
|
|
return xmln('data', *args)
|
|
|
|
|
|
|
|
# Aliases
|
|
|
|
function = lambda *args: xmln('function', *args)
|
|
|
|
record = lambda *args: xmln('record', *args)
|
2022-12-15 16:48:48 +01:00
|
|
|
delete = lambda *args: xmln('delete', *args)
|
2021-02-22 18:08:11 +01:00
|
|
|
form = lambda *args: xmln('form', *args)
|
|
|
|
tree = lambda *args: xmln('tree', *args)
|
|
|
|
search = lambda *args: xmln('search', *args)
|
|
|
|
|
|
|
|
# Actions
|
|
|
|
def act_window_model(model, attrs):
|
|
|
|
""" Build new act_window from model and args """
|
|
|
|
xmlid = '%s_view_action' % (model.replace('.', '_'))
|
|
|
|
name = '%s Action' % ' '.join(map(lambda w: w.capitalize(), model.split('.')))
|
2022-12-09 11:12:59 +01:00
|
|
|
attrs_clone = attrs.copy() # Avoid side-effect
|
2023-06-13 18:43:17 +02:00
|
|
|
attrs_clone.update(
|
|
|
|
{'name': name, 'res_model': model, 'type': 'ir.actions.act_window'}
|
|
|
|
)
|
|
|
|
children = []
|
|
|
|
for key, val in attrs_clone.items():
|
|
|
|
if key.endswith('_id'):
|
|
|
|
children.append(field({'name': key, 'ref': val}))
|
|
|
|
elif key == 'sequence':
|
|
|
|
children.append(field({'name': key, 'eval': val}))
|
|
|
|
else:
|
|
|
|
children.append(field({'name': key}, [val]))
|
|
|
|
return record({'id': xmlid, 'model': 'ir.actions.act_window'}, children)
|
2021-02-22 18:08:11 +01:00
|
|
|
|
|
|
|
def action_server_code(xmlid, name, modelref, code):
|
|
|
|
""" Server actions of type code """
|
2022-12-09 11:12:59 +01:00
|
|
|
children = [field_name(name),
|
|
|
|
field({'name': 'model_id', 'ref': modelref}, []),
|
|
|
|
field({'name': 'state'}, ['code']),
|
|
|
|
field({'name': 'code'}, [code])]
|
2021-02-22 18:08:11 +01:00
|
|
|
return record({'id': xmlid, 'model': 'ir.actions.server'}, children)
|
|
|
|
|
|
|
|
def client_action_multi(xmlid, name, model, action):
|
|
|
|
""" Client action multi (ir.values), with own xmlid, name, targeted model
|
|
|
|
and action """
|
|
|
|
action = "'ir.actions.server,%d'%{}".format(action)
|
2022-12-09 11:12:59 +01:00
|
|
|
children = [field_name(name),
|
|
|
|
field({'name': 'key2', 'eval': "'client_action_multi'"}),
|
|
|
|
field({'name': 'model', 'eval': "'%s'" % model}),
|
|
|
|
field({'name': 'value', 'eval': action})]
|
2021-02-22 18:08:11 +01:00
|
|
|
return record({'id': xmlid, 'model': 'ir.values'}, children)
|
|
|
|
|
|
|
|
# Menus
|
|
|
|
menuitem = lambda *args: xmln('menuitem', *args)
|
|
|
|
def menuitem_model(model, attrs):
|
|
|
|
""" Build new menuitem from model and attrs """
|
|
|
|
model_und = model.replace('.', '_')
|
|
|
|
xmlid = '%s_menu' % model_und
|
|
|
|
actionid = '%s_view_action' % model_und
|
2022-12-09 11:12:59 +01:00
|
|
|
attrs_clone = attrs.copy() # Avoid side-effect
|
2021-02-22 18:08:11 +01:00
|
|
|
attrs_clone.update({'id': xmlid, 'action': actionid})
|
|
|
|
return menuitem(attrs_clone)
|
|
|
|
|
|
|
|
# Form aliases
|
|
|
|
group = lambda *args: xmln('group', *args)
|
|
|
|
header = lambda *args: xmln('header', *args)
|
|
|
|
footer = lambda *args: xmln('footer', *args)
|
|
|
|
sheet = lambda *args: xmln('sheet', *args)
|
|
|
|
button = lambda *args: xmln('button', *args)
|
|
|
|
p = lambda *args: xmln('p', *args)
|
2023-05-24 10:29:55 +02:00
|
|
|
i = lambda *args: xmln('i', *args)
|
|
|
|
t = lambda *args: xmln('t', *args)
|
|
|
|
div = lambda *args: xmln('div', *args)
|
2024-01-12 12:26:51 +01:00
|
|
|
span = lambda *args: xmln('span', *args)
|
2024-01-12 12:34:32 +01:00
|
|
|
br = lambda *args: xmln('br', *args)
|
2024-01-16 17:34:07 +01:00
|
|
|
h1 = lambda *args: xmln('h1', *args)
|
|
|
|
h2 = lambda *args: xmln('h1', *args)
|
|
|
|
h3 = lambda *args: xmln('h1', *args)
|
|
|
|
h4 = lambda *args: xmln('h1', *args)
|
|
|
|
h5 = lambda *args: xmln('h1', *args)
|
2023-01-10 14:30:06 +01:00
|
|
|
notebook = lambda *args: xmln('notebook', *args)
|
|
|
|
page = lambda *args: xmln('page', *args)
|
2021-02-22 18:08:11 +01:00
|
|
|
xpath = lambda *args: xmln('xpath', *args)
|
2022-12-09 11:12:59 +01:00
|
|
|
attribute = lambda name, value: xmln('attribute', {'name': name}, [value])
|
2021-02-22 18:08:11 +01:00
|
|
|
|
|
|
|
# Fields
|
|
|
|
field = lambda *args: xmln('field', *args)
|
2022-12-09 11:12:59 +01:00
|
|
|
field_name = lambda name: field({'name': 'name'}, [name])
|
|
|
|
field_nval = lambda name, value: field({'name': name}, [value])
|
|
|
|
field_model = lambda model: field({'name': 'model'}, [model])
|
2021-02-22 18:08:11 +01:00
|
|
|
field_inherit = lambda xmlid: field({'name': 'inherit_id', 'ref': xmlid}, [])
|
|
|
|
field_arch = lambda *args: field({'name': 'arch', 'type': 'xml'}, *args)
|
|
|
|
|
|
|
|
# Search
|
|
|
|
filter = lambda *args: xmln('filter', *args)
|
2022-11-20 23:41:04 +01:00
|
|
|
separator = lambda *args: xmln('separator', *args)
|
2021-02-22 18:08:11 +01:00
|
|
|
|
2022-11-25 22:54:46 +01:00
|
|
|
def filter_yes_no(field, str_yes=False, str_no=False):
|
|
|
|
""" Double filter for boolean : True and False """
|
|
|
|
res = []
|
|
|
|
if str_yes:
|
2022-12-09 11:12:59 +01:00
|
|
|
res.append(filter({'name': f'{field}_yes', 'string': str_yes,
|
|
|
|
'domain': f"[('{field}', '=', True)]"}))
|
2022-11-25 22:54:46 +01:00
|
|
|
if str_no:
|
2022-12-09 11:12:59 +01:00
|
|
|
res.append(filter({'name': f'{field}_no', 'string': str_no,
|
|
|
|
'domain': f"[('{field}', '=', False)]"}))
|
2022-11-25 22:54:46 +01:00
|
|
|
return res
|
|
|
|
|
2021-02-22 18:08:11 +01:00
|
|
|
# Views
|
|
|
|
view = lambda xmlid, children: record({'id': xmlid, 'model': 'ir.ui.view'}, children)
|
|
|
|
|
|
|
|
def view_def(xmlid, name, model, arch):
|
|
|
|
""" Shortcut for new view """
|
|
|
|
return view(xmlid, [field_name(name), field_model(model), field_arch(arch)])
|
|
|
|
|
|
|
|
def view_new(view_type, model, arch):
|
|
|
|
""" View : new view definition, based on type (form, tree, ...) and model ID """
|
|
|
|
model_und = model.replace('.', '_')
|
|
|
|
model_cap = ' '.join(map(lambda w: w.capitalize(), model.split('.')))
|
|
|
|
xmlid = "%s_view_%s" % (model_und, view_type)
|
|
|
|
name = ' '.join([model_cap, view_type.capitalize()])
|
|
|
|
return view_def(xmlid, name, model, arch)
|
|
|
|
|
|
|
|
def view_inherit(filename, model, inherit, arch):
|
|
|
|
""" Inherited View simplification with name of the record, xmlid for model
|
|
|
|
and inherited view """
|
|
|
|
module = filename.split('.')[2]
|
|
|
|
inherited = inherit.split('.')[1]
|
|
|
|
xmlid = '%s_inherit_%s' % (inherited, module)
|
|
|
|
model_cap = ' '.join(map(lambda w: w.capitalize(), model.split('.')))
|
|
|
|
name = '%s Adaptations' % model_cap
|
2022-12-09 11:12:59 +01:00
|
|
|
return view(xmlid, [field_name(name), field_model(model),
|
|
|
|
field_inherit(inherit), field_arch(arch)])
|