Skip to content

Commit

Permalink
[ADD] product_product_qr_code: Create field qr_code in product.product
Browse files Browse the repository at this point in the history
  • Loading branch information
unaiberis committed Jan 23, 2025
1 parent 65e5c6d commit f9137a5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions product_product_qr_code/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
14 changes: 14 additions & 0 deletions product_product_qr_code/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
'name': 'Product QR Code',
'version': '16.0.1.0.0',
'category': 'Product',
'author': 'Avanzosc',
'license': 'LGPL-3',
'depends': ['product'],
'data': [
'views/product_product_views.xml',
],
'installable': True,
'application': False,
'website': 'https://avanzosc.es',
}
30 changes: 30 additions & 0 deletions product_product_qr_code/models/product_product_qr_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from odoo import models, fields, api
import base64
import qrcode
from io import BytesIO

class ProductProduct(models.Model):
_inherit = "product.product"

qr_code = fields.Binary("QR Code", compute="_compute_qr_code", store=True)

@api.depends('barcode')
def _compute_qr_code(self):
for product in self:
if product.barcode:
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(product.barcode)
qr.make(fit=True)

img = qr.make_image(fill_color="black", back_color="white")
buffer = BytesIO()
img.save(buffer, format="PNG")
qr_image = base64.b64encode(buffer.getvalue())
product.qr_code = qr_image
else:
product.qr_code = False
12 changes: 12 additions & 0 deletions product_product_qr_code/views/product_product_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<odoo>
<record id="view_product_form_inherit" model="ir.ui.view">
<field name="name">product.product.form.inherit.qr</field>
<field name="model">product.product</field>
<field name="inherit_id" ref="product.product_normal_form_view"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='barcode']" position="after">
<field name="qr_code" widget="image" class="oe_avatar" options="{'preview_image': 'qr_code'}"/>
</xpath>
</field>
</record>
</odoo>

0 comments on commit f9137a5

Please sign in to comment.