Help

0

[Odoo 10] How to get the static files into a model file ?

Avatar
Robin Hède
Hi,

I use the "auth_ldap" module that allows you to sync users between an LDAP server and Odoo.

I override the method to create the user to set automatically an image to an user by its login.

I can define an image to a user when I set directly a string encoded in base64.

But now, I would like to open an image stored in the static files of the module and convert it to base64 string.

I use the following code to search the image but it does not work.
import os
import base64

class Users(object):
    def __init__(self, login, image):
        self.login = login
        self.image = image

img = os.path.join('name_of_the_module', 'static','src', 'img', login+'.jpg')
values = Users("test.test", base64.b64encode(img))
self.env['res.users'].sudo().create(values)
Can you help me?

Best regards,
Robin Hède.
Avatar
Discard
1 Answer
0
Avatar
Robin Hède
Best Answer

I finally found a solution to my problem.

Here is the code:

from odoo import modules, tools
login = "test.test"
class Users(object):
    def __init__(self, login, image):
        self.login = login
        self.image = image
image_path = modules.module.get_resource_path(
    'name_of_the_module',
    'static/img',
    login+'.jpg'
)
if image_path:
    image = tools.image_resize_image_big(
        open(
            image_path,
            'rb'
                ).read().encode('base64')
    )
else:
    image = ''
values = Users(login, image)
self.env['res.users'].sudo().create(values)

Best regards,
Robin Hède.
Avatar
Discard