2020-04-30 03:01:37 +02:00
|
|
|
# -*- 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 """
|
|
|
|
|
2020-04-30 23:41:39 +02:00
|
|
|
from typing import Text, Dict
|
|
|
|
from xml_base import XMLAttrs, XMLDictElement, xmlroot, xmln
|
2020-04-30 03:01:37 +02:00
|
|
|
|
|
|
|
# XML helpers functions and macros
|
|
|
|
|
2020-04-30 23:41:39 +02:00
|
|
|
# Odoo root XML Node
|
|
|
|
def odoo(children: Dict) -> ET.Element = {'tag': 'odoo', 'attrs': {}, 'children': children} |> xmlroot
|
2020-04-30 03:01:37 +02:00
|
|
|
|
2020-04-30 23:41:39 +02:00
|
|
|
# Special data Node
|
2020-04-30 03:01:37 +02:00
|
|
|
def data(*args):
|
|
|
|
""" Allow optional args on data tag """
|
2020-04-30 23:41:39 +02:00
|
|
|
if args |> len == 1:
|
|
|
|
{} |> args.insert$(0) where: args = args |> list
|
2020-04-30 03:01:37 +02:00
|
|
|
return xmln('data', *args)
|
|
|
|
|
2020-04-30 23:41:39 +02:00
|
|
|
# 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
|
2020-04-30 03:01:37 +02:00
|
|
|
|
2020-04-30 23:41:39 +02:00
|
|
|
# Actions
|
|
|
|
def act_window(*args) -> XMLDictElement = 'act_window' |> xmln$ <*| args
|
|
|
|
def act_window_model(model: Text, attrs: XMLAttrs) -> XMLDictElement:
|
2020-04-30 03:01:37 +02:00
|
|
|
""" Build new act_window from model and args """
|
2020-04-30 23:41:39 +02:00
|
|
|
xmlid = '%s_view_action' % (('.', '_') |*> model.replace)
|
|
|
|
name = '%s Action' % ('.' |> model.split |> map$(-> _.capitalize()) |> list |> ' '.join)
|
2020-05-01 00:07:01 +02:00
|
|
|
attrs_clone = attrs.copy() # Avoid side-effect
|
|
|
|
{'id': xmlid, 'name': name, 'res_model': model} |> attrs_clone.update
|
|
|
|
return act_window(attrs_clone)
|
2020-04-30 03:01:37 +02:00
|
|
|
|
2020-04-30 23:41:39 +02:00
|
|
|
# Menus
|
|
|
|
def menuitem(*args) -> XMLDictElement = 'menuitem' |> xmln$ <*| args
|
|
|
|
def menuitem_model(model: Text, attrs: XMLAttrs) -> XMLDictElement:
|
2020-04-30 03:01:37 +02:00
|
|
|
""" Build new menuitem from model and attrs """
|
2020-04-30 23:41:39 +02:00
|
|
|
model_und = ('.', '_') |*> model.replace
|
|
|
|
xmlid = '%s_menu' % model_und
|
|
|
|
actionid = '%s_view_action' % model_und
|
2020-05-01 00:07:01 +02:00
|
|
|
attrs_clone = attrs.copy() # Avoid side-effect
|
|
|
|
{'id': xmlid, 'action': actionid} |> attrs_clone.update
|
|
|
|
return menuitem(attrs_clone)
|
2020-04-30 03:01:37 +02:00
|
|
|
|
2020-04-30 23:41:39 +02:00
|
|
|
# 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
|
2020-04-30 03:01:37 +02:00
|
|
|
|
2020-05-01 09:45:58 +02:00
|
|
|
def view_new(view_type: Text, model: Text, arch: List) -> XMLDictElement:
|
2020-04-30 03:01:37 +02:00
|
|
|
""" View : new view definition, based on type (form, tree, ...) and model ID """
|
2020-05-01 09:45:58 +02:00
|
|
|
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
|
2020-04-30 03:01:37 +02:00
|
|
|
|
2020-05-01 09:45:58 +02:00
|
|
|
def view_inherit(filename: Text, model: Text, inherit: Text, arch: List) -> XMLDictElement:
|
2020-04-30 03:01:37 +02:00
|
|
|
""" Inherited View simplification with name of the record, xmlid for model
|
|
|
|
and inherited view """
|
2020-05-01 09:45:58 +02:00
|
|
|
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:
|
2020-04-30 03:01:37 +02:00
|
|
|
""" Server actions of type code """
|
2020-05-01 09:45:58 +02:00
|
|
|
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
|
2020-04-30 03:01:37 +02:00
|
|
|
|
2020-05-01 09:45:58 +02:00
|
|
|
def client_action_multi(xmlid: Text, name: Text, model: Text, action: Text) -> XMLDictElement:
|
2020-04-30 03:01:37 +02:00
|
|
|
""" Client action multi (ir.values), with own xmlid, name, targeted model
|
|
|
|
and action """
|
2020-05-01 09:45:58 +02:00
|
|
|
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
|