diff --git a/sf_tool_management/models/maintenance_equipment.py b/sf_tool_management/models/maintenance_equipment.py index 4b5fe89e..eb2f5990 100644 --- a/sf_tool_management/models/maintenance_equipment.py +++ b/sf_tool_management/models/maintenance_equipment.py @@ -1,5 +1,8 @@ +import qrcode +import base64 from odoo import models, api, fields from odoo.exceptions import ValidationError +from io import BytesIO class SfMaintenanceEquipmentTool(models.Model): @@ -79,6 +82,34 @@ class StockLot(models.Model): record.tool_material_search_id = tool_material_search return records + qr_code_image = fields.Binary(string='二维码', compute='_generate_qr_code') + + @api.depends('name') + def _generate_qr_code(self): + for record in self: + # Generate QR code + qr = qrcode.QRCode( + version=1, + error_correction=qrcode.constants.ERROR_CORRECT_L, + box_size=10, + border=4, + ) + qr.add_data(record.name) + qr.make(fit=True) + + # Create an image from the QR Code instance + qr_image = qr.make_image(fill_color="black", back_color="white") + + # Save the image to a BytesIO object + image_stream = BytesIO() + qr_image.save(image_stream, format="PNG") + + # Store the BytesIO object in the binary field + record.qr_code_image = base64.b64decode(image_stream.getvalue()) + # record.qr_code_image = image_stream.getvalue() + + print(record.qr_code_image) + class ProductProduct(models.Model): _inherit = 'product.product' diff --git a/sf_tool_management/views/tool_material_search.xml b/sf_tool_management/views/tool_material_search.xml index 158e83b9..85b3fd47 100644 --- a/sf_tool_management/views/tool_material_search.xml +++ b/sf_tool_management/views/tool_material_search.xml @@ -59,6 +59,7 @@ +