yaltik_odoo_custom/xml_dsl/odoo.py

82 lines
3.0 KiB
Python

# -*- coding: utf-8 -*-
#
# Copyright 2019 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 . import base as x
def odoo(*args):
""" odoo tag shortcut """
return x.xmlroot(x.xmln('odoo', {}, *args))
def data(*args):
""" data tag shortcut """
return x.xmln('data', {}, *args)
def record(*args):
""" record tag shortcut """
return x.xmln('record', *args)
def view(xmlid, children):
""" view tag shortcut """
return record({'id': xmlid, 'model': 'ir.ui.view'}, children)
def view_def(xmlid, name, model, arch):
""" View and first fields simplification with record xmlid, name, targeted model """
return view(xmlid, [field_name(name), field_model(model), field_arch(arch)])
def view_inherit(name, model, inherit, arch):
""" Inherited View simplification with name of the record, xmlid for model
and inherited view """
module = __name__.split('.')[2]
inherited = inherit.split('.')[1]
xmlid = '%s_inherit_%s' % (inherited, module)
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 field(*args):
""" field tag shortcut """
return x.xmln('field', *args)
def field_name(name):
""" field name tag shortcut """
return field({'name': 'name'}, [name])
def field_model(model):
""" field model tag shortcut """
return field({'name': 'model'}, [model])
def field_inherit(xmlid):
""" field inherit tag shortcut """
return field({'name': 'inherit_id', 'ref': xmlid})
def field_arch(*args):
""" field arch tag shortcut """
return field({'name': 'arch', 'type': 'xml'}, *args)
def xml_write(mpath, filename, tree):
""" Write XML file according to filename and given tree """
import os.path
from xml.etree import ElementTree as ET
output_xml = ET.tostring(tree)
output_path = os.path.dirname(os.path.abspath(mpath))
fpath = u'%s/%s' % (output_path, filename)
with open(fpath, 'w') as xml_file:
xml_file.write(output_xml)