Skip to Content
Menu
This question has been flagged
1 Reply
6417 Views
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
Author 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
Related Posts Replies Views Activity
2
Apr 19
2641
2
Aug 20
3141
1
Aug 24
264
1
Jul 24
563
2
Aug 24
2554