"""Operator artwork handling: final files, and the uploads that carry them. These reuse the customer upload routes with the operator's derived identity, so one transport serves both and there is only one place where parts are signed. """ from uuid import UUID from fastapi import APIRouter, Depends, HTTPException from pydantic import Field from ..core import db from .. import proof from ..artwork import submit_files from ..core.auth import can, can_any, operator from ..core.models import ArtworkSubmission, StrictModel, UploadStart from ..printpng_validate import final_png_problem from ..runtime import file_rows, operator_identity, storage from .uploads import begin_upload, cancel_upload, complete_upload, part_url, upload_status router = APIRouter() @router.post('/api/operator/orders/{oid}/uploads') def begin_final(oid: UUID, body: UploadStart, user=Depends(can('produce'))): if not body.name.lower().endswith('.png'): raise HTTPException(422, 'O arquivo final precisa ser PNG transparente a 300 DPI. Converta PDF/TIFF antes de enviar.') with db.connect() as c: row = c.execute('SELECT id,state,parent_id,proof_ok_at FROM dtf_local.orders WHERE id=%s', (oid,)).fetchone() if not row: raise HTTPException(404, 'Order not found') if row['parent_id']: raise HTTPException(409, 'A prova recebe o PNG final automaticamente.') if not proof.manual_source_needed(c, row): proof.require_unlocked(c, row) if row['state'] not in ('rec','cor'): raise HTTPException(409, 'Order is not in artwork review') return begin_upload(body, session_id=operator_identity(user)) @router.get('/api/operator/uploads/{uid}') def final_status(uid: UUID, user=Depends(operator)): return upload_status(uid,session_id=operator_identity(user)) # The parts of an upload its operator started: a final (produce) or the # photo of a signed pickup receipt (deliver, which includes produce). @router.post('/api/operator/uploads/{uid}/parts/{number}') def final_part(uid: UUID, number: int, user=Depends(can_any('deliver', 'assemble'))): return part_url(uid,number,session_id=operator_identity(user)) @router.post('/api/operator/uploads/{uid}/complete') def final_complete(uid: UUID, user=Depends(can_any('deliver', 'assemble'))): return complete_upload(uid,session_id=operator_identity(user)) @router.delete('/api/operator/uploads/{uid}') def final_cancel(uid: UUID, user=Depends(can_any('deliver', 'assemble'))): return cancel_upload(uid,session_id=operator_identity(user)) @router.get('/api/operator/orders/{oid}/files') def operator_files(oid: UUID, user=Depends(operator)): with db.connect() as c: return file_rows(c,oid) class FinalCheck(StrictModel): uploads: list[UUID] = Field(min_length=1, max_length=400) @router.post('/api/operator/orders/{oid}/final-check') def final_check(oid: UUID, body: FinalCheck, user=Depends(can('produce'))): """Whether these files of the order can be approved as its finals, asked before approving (Sentry DTF-WEB-11): a customer's correction or an original may not be the transparent 300 DPI PNG a final must be. The answer lists the problems with 200, so a file to treat is guidance in the panel, not a refusal. Only the order's own files are read.""" with db.connect() as c: order = c.execute('SELECT snapshot FROM dtf_local.orders WHERE id=%s', (oid,)).fetchone() if not order: raise HTTPException(404, 'Pedido não encontrado.') originals = {uid for item in order['snapshot']['items'] for uid in item['uploads']} rows = c.execute('''SELECT u.id,u.name,u.object_key FROM dtf_local.uploads u WHERE u.id=ANY(%s) AND ( u.id::text=ANY(%s) OR EXISTS (SELECT 1 FROM dtf_local.order_files f WHERE f.order_id=%s AND f.upload_id=u.id) OR EXISTS (SELECT 1 FROM dtf_local.print_parts p WHERE p.order_id=%s AND p.upload_id=u.id))''', (body.uploads, list(originals), oid, oid)).fetchall() problems = [] for row in rows: issue = final_png_problem(storage, row) if issue: problems.append({'upload_id': str(row['id']), 'name': row['name'], 'message': issue}) return {'problems': problems} @router.post('/api/operator/orders/{oid}/final-files') def final_files(oid: UUID, body: ArtworkSubmission, user=Depends(can('produce'))): with db.connect() as c: order = c.execute('SELECT * FROM dtf_local.orders WHERE id=%s FOR UPDATE', (oid,)).fetchone() if not order: raise HTTPException(404, 'Order not found') if order['parent_id']: raise HTTPException(409, 'O arquivo final da prova é gerado automaticamente. Use Gerar de novo se falhar.') if not proof.manual_source_needed(c, order): proof.require_unlocked(c, order) return submit_files(c,order,body,operator_identity(user),'final',user)