Files
Odoo-Modules/fusion_plating/scripts/fp_retro_image_30045.py
gsinghpal 1314f4581d changes
2026-05-21 03:37:25 -04:00

53 lines
1.7 KiB
Python

# -*- coding: utf-8 -*-
"""Retro-extract WMF image from CoC-30045's attached RTF, attach as
PNG, link to x_fc_thickness_image_id, regenerate cert PDF.
Run with:
odoo shell -c /etc/odoo/odoo.conf -d admin --no-http < this_file
"""
import base64
from odoo.addons.fusion_plating_jobs.wizards.fp_cert_issue_wizard import (
_fp_extract_rtf_images,
_fp_pick_microscope_image,
)
cert = env['fp.certificate'].browse(501)
att = env['ir.attachment'].search([
('res_model', '=', 'fp.certificate'),
('res_id', '=', 501),
('name', 'ilike', 'XRF'),
], limit=1)
raw = base64.b64decode(att.datas)
pngs = _fp_extract_rtf_images(raw)
print('extracted PNG blocks:', len(pngs),
'sizes:', [len(p) for p in pngs])
img_bytes, w, h = _fp_pick_microscope_image(pngs)
if not img_bytes:
print('no microscope image found (all blocks below area threshold)')
else:
print('picked microscope image: %dx%d, %d bytes' % (w, h, len(img_bytes)))
img_att = env['ir.attachment'].sudo().create({
'name': 'CoC-30045-microscope.png',
'type': 'binary',
'datas': base64.b64encode(img_bytes),
'mimetype': 'image/png',
'res_model': 'fp.certificate',
'res_id': cert.id,
})
cert.write({'x_fc_thickness_image_id': img_att.id})
print('attached as ir.attachment id=%d' % img_att.id)
# Regenerate the cert PDF so the layout includes the image.
if cert.attachment_id:
cert.attachment_id.unlink()
cert.invalidate_recordset()
new_att = cert._fp_render_and_attach_pdf()
env.cr.commit()
print('regen done · cert PDF=%s · size=%d bytes' % (
new_att.name if new_att else 'NONE',
new_att.file_size if new_att else 0,
))