跳至內容
選單
此問題已被標幟
2 回覆
4107 瀏覽次數

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())

頭像
捨棄

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

作者

have updated the full code for compute now

最佳答案

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!

頭像
捨棄
作者

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

最佳答案

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.

頭像
捨棄
作者

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

相關帖文 回覆 瀏覽次數 活動
2
5月 20
6029
2
1月 24
13186
1
3月 15
8129
2
10月 23
3427
0
5月 21
1640