yaltik_odoo_custom/hy_base/odoo.hy

61 lines
2.4 KiB
Hy

;; -*- 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 macros "
(require [odoo.addons.hy_base.xml [*]])
(defmacro odoo [&rest args] `(xmlr "odoo" ~@args))
(defmacro data [&rest args] `(xmlnc "data" ~@args))
(defmacro record [&rest args] `(xmlnc "record" ~@args))
(defmacro view [xmlid children] `(record {"id" ~xmlid "model" "ir.ui.view"} ~children))
(defmacro view-def [xmlid name model &rest body]
"View and first fields simplification with record xmlid, name, targeted model"
`(do
(view ~xmlid
[(field-name ~name)
(field-model ~model)
(field-arch ~@body)])))
(defmacro view-inherit [name model inherit &rest body]
"Inherited View simplification with name of the record, xmlid for model and
inherited view"
(setv module (get (.split __name__ ".") 2)
inherited (get (.split inherit ".") 1)
xmlid f"{inherited}_inherit_{module}")
`(do
(view ~xmlid
[(field-name ~name)
(field-model ~model)
(field-inherit ~inherit)
(field-arch ~@body)])))
(defmacro field [&rest args] `(xmlna "field" ~@args))
(defmacro field-name [name] `(field {"name" "name"} [~name]))
(defmacro field-model [model] `(field {"name" "model"} [~model]))
(defmacro field-inherit [xmlid] `(field {"name" "inherit_id" "ref" ~xmlid} []))
(defmacro field-arch [&rest args] `(field {"name" "arch" "type" "xml"} ~@args))
(defmacro xml-write [filename tree]
"Write XML file according to filename and given tree"
`(do
(import [os [path]])
(setv output-xml (.tostring ET tree)
output-path (.dirname path (.abspath path __file__))
fpath f"{output-path}/{filename}")
(with [f (open fpath "w")] (.write f output-xml))))