Faircot process
This commit is contained in:
@@ -10,6 +10,7 @@ import io
|
||||
import logging
|
||||
import json
|
||||
import re
|
||||
import traceback
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -62,25 +63,57 @@ class AutomationDocument(ModelSQL, ModelView, Workflow):
|
||||
@ModelView.button
|
||||
def run_ocr(cls, docs):
|
||||
for doc in docs:
|
||||
logger.info(
|
||||
"RUN_OCR_START doc=%s type=%s state=%s incoming=%s",
|
||||
getattr(doc, 'id', None), getattr(doc, 'type', None),
|
||||
getattr(doc, 'state', None),
|
||||
getattr(getattr(doc, 'document', None), 'id', None))
|
||||
try:
|
||||
if doc.type == 'weight_report':
|
||||
# Décoder le fichier depuis le champ Binary
|
||||
file_data = doc.document.data or b""
|
||||
logger.info(f"File size: {len(file_data)} bytes")
|
||||
logger.info(f"First 20 bytes: {file_data[:20]}")
|
||||
logger.info(f"Last 20 bytes: {file_data[-20:]}")
|
||||
if not doc.document:
|
||||
raise ValueError("No incoming document linked")
|
||||
|
||||
file_data = doc.document.data or b""
|
||||
file_name = doc.document.name or "document"
|
||||
url = "http://automation-service:8006/ocr"
|
||||
logger.info(
|
||||
"RUN_OCR_FILE doc=%s incoming=%s name=%s size=%s "
|
||||
"first_20=%r last_20=%r",
|
||||
getattr(doc, 'id', None), doc.document.id,
|
||||
file_name, len(file_data), file_data[:20],
|
||||
file_data[-20:])
|
||||
|
||||
if not file_data:
|
||||
raise ValueError("Incoming document has no binary data")
|
||||
|
||||
# Envoyer le fichier au service OCR
|
||||
logger.info(
|
||||
"RUN_OCR_HTTP_REQUEST doc=%s url=%s file_name=%s "
|
||||
"size=%s",
|
||||
getattr(doc, 'id', None), url, file_name,
|
||||
len(file_data))
|
||||
response = requests.post(
|
||||
"http://automation-service:8006/ocr",
|
||||
files={"file": (file_name, io.BytesIO(file_data))}
|
||||
url,
|
||||
files={"file": (file_name, io.BytesIO(file_data))},
|
||||
timeout=120,
|
||||
)
|
||||
logger.info(
|
||||
"RUN_OCR_HTTP_RESPONSE doc=%s status=%s reason=%s "
|
||||
"elapsed=%s content_type=%s body_start=%r",
|
||||
getattr(doc, 'id', None), response.status_code,
|
||||
response.reason, response.elapsed,
|
||||
response.headers.get('content-type'),
|
||||
response.text[:1000])
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
logger.info("RUN_OCR_RESPONSE:%s",data)
|
||||
doc.ocr_text = data.get("ocr_text", "")
|
||||
ocr_text = data.get("ocr_text", "") or ""
|
||||
logger.info(
|
||||
"RUN_OCR_JSON doc=%s keys=%s ocr_text_len=%s "
|
||||
"ocr_text_start=%r",
|
||||
getattr(doc, 'id', None), list(data.keys()),
|
||||
len(ocr_text), ocr_text[:500])
|
||||
doc.ocr_text = ocr_text
|
||||
doc.state = "ocr_done"
|
||||
doc.notes = (doc.notes or "") + "OCR done\n"
|
||||
else:
|
||||
@@ -112,9 +145,17 @@ class AutomationDocument(ModelSQL, ModelView, Workflow):
|
||||
doc.notes = (doc.notes or "") + " SO updated"
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"RUN_OCR_FAILED doc=%s type=%s error=%s traceback=%s",
|
||||
getattr(doc, 'id', None), getattr(doc, 'type', None),
|
||||
e, traceback.format_exc())
|
||||
doc.state = "error"
|
||||
doc.notes = (doc.notes or "") + f"OCR error: {e}\n"
|
||||
doc.save()
|
||||
logger.info(
|
||||
"RUN_OCR_END doc=%s state=%s ocr_text_len=%s",
|
||||
getattr(doc, 'id', None), getattr(doc, 'state', None),
|
||||
len(getattr(doc, 'ocr_text', None) or ""))
|
||||
# -------------------------------------------------------
|
||||
# STRUCTURE (doctr)
|
||||
# -------------------------------------------------------
|
||||
|
||||
@@ -7,6 +7,9 @@ from trytond.modules.document_incoming.exceptions import (
|
||||
DocumentIncomingProcessError)
|
||||
from trytond.pool import Pool, PoolMeta
|
||||
import json
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class IncomingConfiguration(metaclass=PoolMeta):
|
||||
@@ -39,12 +42,22 @@ class Incoming(metaclass=PoolMeta):
|
||||
return super()._get_results() | {'automation.document'}
|
||||
|
||||
def _process_weight_report(self):
|
||||
logger.info(
|
||||
"DOCUMENT_INCOMING_WR_START incoming=%s name=%s type=%s "
|
||||
"data_size=%s",
|
||||
getattr(self, 'id', None), getattr(self, 'name', None),
|
||||
getattr(self, 'type', None),
|
||||
len(getattr(self, 'data', None) or b""))
|
||||
WR = Pool().get('automation.document')
|
||||
wr = WR()
|
||||
wr.document = self.id
|
||||
wr.type = 'weight_report'
|
||||
wr.state = 'draft'
|
||||
WR.save([wr])
|
||||
logger.info(
|
||||
"DOCUMENT_INCOMING_WR_AUTOMATION_CREATED incoming=%s "
|
||||
"automation_document=%s",
|
||||
getattr(self, 'id', None), getattr(wr, 'id', None))
|
||||
WR.run_ocr([wr])
|
||||
WR.run_metadata([wr])
|
||||
WR.run_pipeline([wr])
|
||||
|
||||
Reference in New Issue
Block a user