[ADD]Hy Base : new macros
* For computed fields ; * For migrations.
This commit is contained in:
parent
d0a7b6a74e
commit
ebef43ad8a
@ -15,8 +15,9 @@
|
||||
;; 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 "
|
||||
" Odoo macros "
|
||||
|
||||
(import [os [path]])
|
||||
(import logging [odoo.addons.hy_base.xml [*]])
|
||||
(setv logger (.getLogger logging __name__))
|
||||
|
||||
@ -27,6 +28,8 @@
|
||||
python2-form
|
||||
python3-form))
|
||||
|
||||
; XML helpers functions and macros
|
||||
|
||||
(defn ox-odoo [&rest args] (xmlroot (xmln "odoo" {} #*args)))
|
||||
(defn ox-data [&rest args] (xmln "data" {} #*args))
|
||||
(defn ox-record [&rest args] (xmln "record" #*args))
|
||||
@ -95,3 +98,27 @@
|
||||
~g!fpath (+ ~g!output-path "/" ~filename))
|
||||
(.debug logger (+ "Hy XML DSL : compiling " ~filename " to " ~g!output-path))
|
||||
(with [f (open ~g!fpath "w")] (.write f ~g!output-xml))))
|
||||
|
||||
; Odoo Backend macros
|
||||
|
||||
(defmacro/g! compute-fn [field dependencies body]
|
||||
"Macro to make computed definition smoother"
|
||||
(setv fname f"_compute_{(mangle field)}" descr f"Computes {field}")
|
||||
`(do
|
||||
(import [hy.models [HySymbol]])
|
||||
(with-decorator (.depends api ~@dependencies)
|
||||
(defn ~(HySymbol fname) [self]
|
||||
~descr
|
||||
~body))))
|
||||
|
||||
(defmacro compute-field [fname body]
|
||||
(setv fn-name f"_compute_{(mangle fname)}")
|
||||
`(setv ~fname (~@body :compute ~fn-name)))
|
||||
|
||||
; Migrations
|
||||
|
||||
(defn generate-fn-name [filepath]
|
||||
"Generate function name from filepath"
|
||||
(setv version (.replace (get (.split (.dirname path filepath) "/") -1) "." "_")
|
||||
pre-post (get (.split (.basename path filepath) "-") 0))
|
||||
f"{pre-post}_{version}")
|
||||
|
Loading…
x
Reference in New Issue
Block a user