78 lines
2.5 KiB
Python
78 lines
2.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""One-off: re-parse RTF on CoC-30045 + populate new metadata fields
|
|
+ regenerate the cert PDF. Run on entech after deploying the parser
|
|
extensions.
|
|
|
|
Run with:
|
|
odoo shell -c /etc/odoo/odoo.conf -d admin --no-http < this_file
|
|
"""
|
|
import base64
|
|
from datetime import datetime
|
|
|
|
from odoo.addons.fusion_plating_jobs.wizards.fp_cert_issue_wizard import (
|
|
_fp_parse_fischerscope_rtf,
|
|
)
|
|
|
|
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)
|
|
parsed = _fp_parse_fischerscope_rtf(raw)
|
|
print('parsed metadata:', {
|
|
k: parsed[k] for k in (
|
|
'operator', 'product', 'application', 'directory',
|
|
'equipment', 'measuring_time_sec', 'date_str', 'time_str',
|
|
'calibration',
|
|
)
|
|
})
|
|
|
|
vals = {
|
|
'x_fc_thickness_operator': parsed['operator'],
|
|
'x_fc_thickness_product': parsed['product'],
|
|
'x_fc_thickness_application': parsed['application'],
|
|
'x_fc_thickness_directory': parsed['directory'],
|
|
'x_fc_thickness_equipment': parsed['equipment'] or 'Fischerscope XDAL 600',
|
|
'x_fc_thickness_measuring_time_sec': parsed['measuring_time_sec'] or 0,
|
|
'x_fc_thickness_source_filename': att.name,
|
|
}
|
|
|
|
date_str = (parsed.get('date_str') or '').strip()
|
|
time_str = (parsed.get('time_str') or '').strip()
|
|
if date_str:
|
|
combined = ('%s %s' % (date_str, time_str)).strip()
|
|
for fmt in (
|
|
'%m/%d/%Y %I:%M:%S %p', '%m/%d/%Y %I:%M %p',
|
|
'%m/%d/%Y %H:%M:%S', '%m/%d/%Y %H:%M',
|
|
'%m/%d/%Y',
|
|
):
|
|
try:
|
|
vals['x_fc_thickness_datetime'] = datetime.strptime(combined, fmt)
|
|
break
|
|
except ValueError:
|
|
continue
|
|
|
|
cert.write(vals)
|
|
print('wrote vals:', list(vals.keys()))
|
|
|
|
# Backfill calibration on existing readings (created earlier).
|
|
calib = parsed.get('calibration') or ''
|
|
if calib:
|
|
cert.thickness_reading_ids.write({'calibration_std_ref': calib})
|
|
print('backfilled calibration on %d readings' % len(cert.thickness_reading_ids))
|
|
|
|
# Regenerate the cert PDF so the new layout takes effect.
|
|
if cert.attachment_id:
|
|
cert.attachment_id.unlink()
|
|
cert.invalidate_recordset()
|
|
new_att = cert._fp_render_and_attach_pdf()
|
|
env.cr.commit()
|
|
print('done · readings=%d · new PDF=%s · size=%d bytes' % (
|
|
len(cert.thickness_reading_ids),
|
|
new_att.name if new_att else 'NONE',
|
|
new_att.file_size if new_att else 0,
|
|
))
|