Skip to Content
Menu
This question has been flagged
2 Replies
4105 Zobrazenia

Hi have a Image field setup for compute, and it depends on 2 fields, however I noticed when one of the 2 fields is edited it triggers compute but doesn't pass the value of other field properly and hence the compute logic fails, wanted to know if this is a known issue?


from odoo import api, models, fields
from PIL import Image
import io
import base64
import qrcode
import lnurl

class custom_info(models.Model):

_inherit = 'hr.employee'
employee_ln_address = fields.Char(string="Employee lightning address : ", required=False)
employee_ln_qr_image = fields.Image(compute="_compute_ln_qr_image",string="Employee lightning QR code")


@api.depends('image_1920','employee_ln_address')
def _compute_ln_qr_image(self):
    ​for record in self:
        record.employee_ln_qr_image=False
        if record.image_1920:
            logo = Image.open(io.BytesIO(base64.b64decode(record.image_1920)))
            basewidth = 200
            wpercent = (basewidth/float(logo.size[0]))
            hsize = int((float(logo.size[1])*float(wpercent)))
            logo = logo.resize((basewidth, hsize), Image.ANTIALIAS)
            QRcode =qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_H)
            if record.employee_ln_address:
                lnusr,lndomain = record.employee_ln_address.split('@')
                qrmessage = 'lightning:'+lnurl.encode('https://'+lndomain+'/.well-known/lnurlp/'+lnusr)
                QRcode.add_data(qrmessage)
                QRcode.make()
                QRcolor = 'Black'
                QRimg = QRcode.make_image(fill_color=QRcolor, back_color="white").convert('RGB')# adding color to QR code
                pos = ((QRimg.size[0] - logo.size[0]) // 2,(QRimg.size[1] - logo.size[1]) // 2)
                QRimg.paste(logo, pos)
                QRinMem = io.BytesIO()
                QRimg.save(QRinMem,"JPEG")
                QRinMem.seek(0)
                  record.employee_ln_qr_image=base64.b64encode(QRinMem.read())

Avatar
Zrušiť

I would post the code for "logic for compute" to give people more clues.

Autor

have updated the full code for compute now

Best Answer

I have done something similar for a project where I created QR codes to be printed by a label printer. What worked for me was the following:

Change the field from field.Image(compute='_compute_ln_qr_image')​ to field.Binary(compute='_compute_ln_qr_image')

The remainder of your code is similar like for my project code. Try to add logging (e.g., _logger.info(Image Size:{QRimg.size}')​ so you can check whether the QR code generated is a valid image.

In your view you should use the image widget:

[field name="employee_ln_qr_image" widget="image" readonly="True"/]

I hope this helps!

Avatar
Zrušiť
Autor

Thanks tried it but didn't work. The problem is one of the fields that the QR field depends on is a image field, I'm trying to embed the employee Avatar in the QR code, and that seems to be the problem. If I edit the Avatar and it trigger compute it works fine, but if I edit the other field on which the compute is dependent it tirggers compute method but for some reason doesn't pass the value of the other image field that it depends on

Best Answer

try this way:

you can use the @api.onchange decorator instead of @api.depends to trigger the compute method when the employee_ln_address field is modified. The @api.onchange decorator ensures that the compute method receives the correct value of the modified field.

@api.onchange('employee_ln_address')
def _onchange_employee_ln_address(self):
for record in self:
record.employee_ln_qr_image = False
if record.image_1920:
# Your compute logic goes here
# ...

By using the @api.onchange decorator, the _onchange_employee_ln_address method will be triggered whenever the employee_ln_address field is modified, ensuring that the compute logic receives the correct value.

Make sure to remove the @api.depends decorator from the _compute_ln_qr_image method.

Avatar
Zrušiť
Autor

Thanks, i tried onchange as well but same behavior, I am not sure but something specific to image fields, other fields i don't see any issue

Related Posts Replies Zobrazenia Aktivita
2
máj 20
6021
2
jan 24
13185
1
mar 15
8129
2
okt 23
3426
0
máj 21
1640