# -*- coding: utf-8 -*- # # Copyright 2019-2020 Fabien Bourgeois # # 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 . """ Odoo XML DSL """ from typing import Text, Dict from xml_base import XMLAttrs, XMLDictElement, xmlroot, xmln # XML helpers functions and macros # Odoo root XML Node def odoo(children: Dict) -> ET.Element = {'tag': 'odoo', 'attrs': {}, 'children': children} |> xmlroot # Special data Node def data(*args): """ Allow optional args on data tag """ if args |> len == 1: {} |> args.insert$(0) where: args = args |> list return xmln('data', *args) # Aliases def function(*args) -> XMLDictElement = xmln('function', *args) def record(*args) -> XMLDictElement = 'record' |> xmln$(?, *args) def form(*args) -> XMLDictElement = 'form' |> xmln$ <*| args def tree(*args) -> XMLDictElement = 'tree' |> xmln$ <*| args def search(*args) -> XMLDictElement = 'search' |> xmln$ <*| args # Actions def act_window(*args) -> XMLDictElement = 'act_window' |> xmln$ <*| args def act_window_model(model: Text, attrs: XMLAttrs) -> XMLDictElement: """ Build new act_window from model and args """ xmlid = '%s_view_action' % (('.', '_') |*> model.replace) name = '%s Action' % ('.' |> model.split |> map$(-> _.capitalize()) |> list |> ' '.join) {'id': xmlid, 'name': name, 'res_model': model} |> attrs.update return act_window(attrs) # Menus def menuitem(*args) -> XMLDictElement = 'menuitem' |> xmln$ <*| args def menuitem_model(model: Text, attrs: XMLAttrs) -> XMLDictElement: """ Build new menuitem from model and attrs """ model_und = ('.', '_') |*> model.replace xmlid = '%s_menu' % model_und actionid = '%s_view_action' % model_und {'id': xmlid, 'action': actionid} |> attrs.update return menuitem(attrs) # Form aliases def group(*args) -> XMLDictElement = 'group' |> xmln$ <*| args def header(*args) -> XMLDictElement = 'header' |> xmln$ <*| args def footer(*args) -> XMLDictElement = 'footer' |> xmln$ <*| args def sheet(*args) -> XMLDictElement = 'sheet' |> xmln$ <*| args def button(*args) -> XMLDictElement = 'button' |> xmln$ <*| args def p(*args) -> XMLDictElement = 'p' |> xmln$ <*| args def xpath(*args) -> XMLDictElement = 'xpath' |> xmln$ <*| args def attribute(name: Text, value: Text) -> XMLDictElement: return ('attribute', {'name': name}, [value]) |*> xmln # Fields def field(*args) -> XMLDictElement = 'field' |> xmln$ <*| args def field_name(name: Text) -> XMLDictElement = ({'name': 'name'}, [name]) |*> field def field_model(model: Text) -> XMLDictElement = ({'name': 'model'}, [model]) |*> field def field_inherit(xmlid: Text) -> XMLDictElement: return ({'name': 'inherit_id', 'ref': xmlid}, []) |*> field def field_arch(*args) -> XMLDictElement = {'name': 'arch', 'type': 'xml'} |> field$ <*| args # Search def filter(*args) -> XMLDictElement = 'filter' |> xmln$ <*| args # Views def view(xmlid: Text, children: List) -> XMLDictElement: return ({'id': xmlid, 'model': 'ir.ui.view'}, children) |*> record def view_def(xmlid: Text, name: Text, model: Text, arch: List) -> XMLDictElement: return (xmlid, [field_name(name), field_model(model), field_arch(arch)]) |*> view 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([w.capitalize() for w in model.split('.')]) xmlid = '{:{}}'.format(model_und, '') + '_view_' + '{:{}}'.format(view_type, '') name = '{:{}}'.format(model_cap, '') + ' ' + '{:{}}'.format(view_type.capitalize(), '') return view_def(xmlid=xmlid, name=name, model=model, arch=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 = '{:{}}'.format(inherited, '') + '_inherit_' + '{:{}}'.format( module, '') model_cap = ' '.join([w.capitalize() for w in model.split('.')]) name = '{:{}}'.format(model_cap, '') + ' Adaptations' return view(xmlid, [field_name(name), field_model(model), field_inherit(inherit), field_arch(arch)]) def actions_server_code(xmlid, name, modelref, code): """ Server actions of type code """ return record({'id': xmlid, 'model': 'ir.actions.server'}, [ field_name(name), field({'name': 'model_id', 'ref': modelref}, [ ]), field({'name': 'state'}, ['code']), field({'name': 'code'}, [code])]) def client_action_multi(xmlid, name, model, action): """ Client action multi (ir.values), with own xmlid, name, targeted model and action """ action = u"'ir.actions.server,%d'%" + '{:{}}'.format(action, '') return record({'id': xmlid, 'model': 'ir.values'}, [field_name(name), field({'name': 'key2', 'eval': u"'client_action_multi'"}, []), field({'name': 'model', 'eval': u"'" + model + u"'"}, []), field({'name': 'value', 'eval': action})])