Example #1
0
    def _set_website_snippet_static_javascript_file(self, module):
        """
        Function to set the module hook file
        :param module:
        :return:
        """
        cw = CodeWriter()
        cw.cur_indent = 4 * cw.default_dent

        if module.generate_website_snippet_generic_model:
            lst_model_search = (
                module.generate_website_snippet_generic_model.split(";")
            )
            lst_model_id_search = []
            for s_model in lst_model_search:
                model_id = self.env["ir.model"].search(
                    [("model", "=", s_model)]
                )
                if model_id:
                    lst_model_id_search.append(model_id[0])
                else:
                    _logger.warning(f"Model not existing : {s_model}")
            for model_id in lst_model_id_search:
                for field_id in model_id.field_id:
                    if field_id.name not in MAGIC_FIELDS:
                        cw.emit(f'if (data["{field_id.name}"]) {{')
                        with cw.indent():
                            with cw.indent():
                                cw.emit(
                                    f'self.$(".{field_id.name}_value").text(data["{field_id.name}"]);'
                                )
                            cw.emit("}")
        else:
            cw.emit("var data_json = data;")
            cw.emit('var hello = data_json["hello"];')
            cw.emit(f'self.$(".{module.name}_value").text(hello);')

        code = cw.render()

        content = (
            f"odoo.define('{module.name}.animation', function (require)"
            """ {
    'use strict';

    var sAnimation = require('website.content.snippets.animation');

    sAnimation.registry."""
            f"{module.name}"
            """ = sAnimation.Class.extend({
        """
            f"selector: '.o_{module.name}',"
            """

        start: function () {
            var self = this;
            var def = this._rpc({route: '"""
            f"/{module.name}/helloworld"
            """'}).then(function (data) {

                if (data.error) {
                    return;
                }

                if (_.isEmpty(data)) {
                    return;
                }

"""
            + code
            + """    
            });

            return $.when(this._super.apply(this, arguments), def);
        }
    })
});
        """
        )

        file_path = os.path.join(
            "static", "src", "js", f"website.{module.name}.animation.js"
        )
        self.code_generator_data.write_file_str(file_path, content)