Skip to Content
Menu
This question has been flagged

I want to share local directory on my apps. Found load_addons definition in http.Root class, append in statics['/shared_folder'] = '/home/shared_folder' that is work but, override load_addons function with monkey_patch like this

# -- coding: utf-8 --

import ast

import logging

import odoo

import os

import werkzeug

from odoo import http, tools

from odoo.modules.module import module_manifest

from odoo.tools import ustr, consteq, frozendict, pycompat, unique, date_utils

from os.path import join as opj

import mock

logger = logging.getLogger(name_)

STATIC_CACHE = 60 * 60 * 24 * 7

addons_manifest = {}

def load_addons():

    """ Load all addons from addons path containing static files and

    controllers and configure them.  """

    # TODO should we move this to ir.http so that only configured modules are served ?

    statics = {}

    for addons_path in odoo.modules.module.ad_paths:

        for module in sorted(os.listdir(str(addons_path))):

            if module not in addons_manifest:

                mod_path = opj(addons_path, module)

                manifest_path = module_manifest(mod_path)

                path_static = opj(addons_path, module, 'static')

                if manifest_path and os.path.isdir(path_static):

                    manifest_data = open(manifest_path, 'rb').read()

                    manifest = ast.literal_eval(pycompat.to_native(manifest_data))

                    if not manifest.get('installable', True):

                        continue

                    manifest['addons_path'] = addons_path

                    _logger.debug("Loading %s", module)

                    addons_manifest[module] = manifest

                    statics['/%s/static' % module] = path_static

    statics['/shared_files'] = opj(odoo.tools.config.options['data_dir'], 'shared_files')

    if statics:

        _logger.info("HTTP Configuring static files")

    app = werkzeug.wsgi.SharedDataMiddleware(http.Root.dispatch, statics, cache_timeout=STATIC_CACHE)

    http.Root.dispatch = http.DisableCacheMiddleware(app)

with mock.patch.object(http.Root, 'load_addons', load_addons):

    http.root


but doesnt work. How to override this function in my apps.

Avatar
Discard
Best Answer

Hi, 

since you did not specify which version you're working on, i'll take V11 in this example.
This is how we apply a monkey patch on function load_addons:  

from odoo.http import Root

def load_addons(self):

      # HERE you add your code. 

     # End of function

Root.load_addons = load_addons


Regards, 

upvote if this helps. If not, you can write back. 

Avatar
Discard
Author

V12 use but load_addons function run before started the thread,

ı try this method but not work for want to me

if 'werkzeug.wsgi.SharedDataMiddleware' append my file after running the server my problem is gone

Then you may want to check again which function you need to modify. Maybe it's not load_addons but another one where your changes should be added...

Related Posts Replies Views Activity
2
Jun 23
970
1
Jun 20
33182
1
Nov 17
2126
1
Jun 16
3532
2
Aug 15
4221