Ir al contenido
Menú
Se marcó esta pregunta

When we declare any field with a "module_" prefix and add a module name after that in the res.config.settings model as a boolean field, on making it True or False, it will install or uninstall the module. I am trying to find out which function or method is being called from the back-end side to install or uninstall the module. Can anyone tell me? I am using Odoo 16.
Thank you.

Avatar
Descartar
Mejor respuesta

Hi,


path of function : addons/base/models/res_config.py


You can check the following function


def execute(self):

        """

        Called when settings are saved.


        This method will call `set_values` and will install/uninstall any modules defined by

        `module_` Boolean fields and then trigger a web client reload.


        .. warning::


            This method **SHOULD NOT** be overridden, in most cases what you want to override is

            `~set_values()` since `~execute()` does little more than simply call `~set_values()`.


            The part that installs/uninstalls modules **MUST ALWAYS** be at the end of the

            transaction, otherwise there's a big risk of registry database desynchronisation.

        """

        self.ensure_one()

        if not self.env.is_admin():

            raise AccessError(_("Only administrators can change the settings"))


        self = self.with_context(active_test=False)

        classified = self._get_classified_fields()


        self.set_values()


        # module fields: install/uninstall the selected modules

        to_install = classified['module'].filtered(

            lambda m: self[f'module_{m.name}'] and m.state != 'installed')

        to_uninstall = classified['module'].filtered(

            lambda m: not self[f'module_{m.name}'] and m.state in ('installed', 'to upgrade'))


        if to_install or to_uninstall:

            self.env.flush_all()


        if to_uninstall:

            to_uninstall.button_immediate_uninstall()


        installation_status = self._install_modules(to_install)


        if installation_status or to_uninstall:

            # After the uninstall/install calls, the registry and environments

            # are no longer valid. So we reset the environment.

            self.env.reset()

            self = self.env()[self._name]


        # pylint: disable=next-method-called

        config = self.env['res.config'].next() or {}

        if config.get('type') not in ('ir.actions.act_window_close',):

            return config


        # force client-side reload (update user menu and current view)

        return {

            'type': 'ir.actions.client',

            'tag': 'reload',

        }


Regards

Avatar
Descartar
Mejor respuesta

I employ the 'pre_init_hook' method during installation, and automatically uninstall and install certain modules. Give this approach a try.

#uninstall module

def _pre_init_uninstall_modules(cr):
env = api.Environment(cr, SUPERUSER_ID, {})
modules = env['ir.module.module'].search([('name', 'in', ['your_module_name']),('state','=','installed')])

if modules:
[module.sudo().module_uninstall() for module in modules]

#install module

from odoo import api, SUPERUSER_ID


def _pre_init_install_modules(cr):
env = api.Environment(cr, SUPERUSER_ID, {})
modules = env['ir.module.module'].search([('name', 'in', ['your_module_name',
]),('state','=','uninstalled')])

if modules:
[module.sudo().button_install() for module in modules]


Avatar
Descartar
Mejor respuesta

Hi,
You can check out  def execute(self): function in res.config.settings model.

Relevant code lines:

# module fields: install/uninstall the selected modules
to_install = classified['module'].filtered(
lambda m: self[f'module_{m.name}'] and m.state != 'installed')
to_uninstall = classified['module'].filtered(
lambda m: not self[f'module_{m.name}'] and m.state in ('installed', 'to upgrade'))

if to_install or to_uninstall:
self.env.flush_all()

if to_uninstall:
to_uninstall.button_immediate_uninstall()

installation_status = self._install_modules(to_install)



Thanks

Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
2
jul 24
1880
0
feb 24
1294
0
may 23
2200
1
abr 21
5755
2
abr 15
4604