yaltik_odoo_custom/yaltik_dsl/odoo_dsl.coco

128 lines
5.9 KiB
Plaintext

# -*- coding: utf-8 -*-
#
# Copyright 2019-2020 Fabien Bourgeois <fabien@yaltik.com>
#
# 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/>.
""" 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)
attrs_clone = attrs.copy() # Avoid side-effect
{'id': xmlid, 'name': name, 'res_model': model} |> attrs_clone.update
return act_window(attrs_clone)
# 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
attrs_clone = attrs.copy() # Avoid side-effect
{'id': xmlid, 'action': actionid} |> attrs_clone.update
return menuitem(attrs_clone)
# 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: Text, model: Text, arch: List) -> XMLDictElement:
""" View : new view definition, based on type (form, tree, ...) and model ID """
model_und = ('.', '_') |*> model.replace
model_cap = ('.' |> model.split |> map$(-> _.capitalize()) |> list |> ' '.join)
xmlid = "%s_view_%s" % (model_und, view_type)
name = (model_cap, view_type.capitalize()) |> ' '.join
return (xmlid, name, model, arch) |*> view_def
def view_inherit(filename: Text, model: Text, inherit: Text, arch: List) -> XMLDictElement:
""" 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 = ('.' |> model.split |> map$(-> _.capitalize()) |> list |> ' '.join)
name = '%s Adaptations' % model_cap
return view(xmlid, [name |> field_name, model |> field_model,
inherit |> field_inherit, arch |> field_arch])
def action_server_code(xmlid: Text, name: Text, modelref: Text, code: Text) -> XMLDictElement:
""" Server actions of type code """
children = [name |> field_name,
({'name': 'model_id', 'ref': modelref}, []) |*> field,
({'name': 'state'}, ['code']) |*> field,
({'name': 'code'}, [code]) |*> field]
return ({'id': xmlid, 'model': 'ir.actions.server'}, children) |*> record
def client_action_multi(xmlid: Text, name: Text, model: Text, action: Text) -> XMLDictElement:
""" Client action multi (ir.values), with own xmlid, name, targeted model
and action """
action = action |> "'ir.actions.server,%d'%{}".format
children = [name |> field_name,
{'name': 'key2', 'eval': "'client_action_multi'"} |> field,
{'name': 'model', 'eval': "'%s'" % model} |> field,
{'name': 'value', 'eval': action} |> field]
return ({'id': xmlid, 'model': 'ir.values'}, children) |*> record