From b027bb50137b877500b23cff101fa5e10ff7fd05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cau=C3=AA=20Faleiros?= Date: Fri, 27 Nov 2026 12:25:23 -0300 Subject: [PATCH] fix: Prova OK approves every part of a film, and "Gerar de novo" names the new files with a version (#114, #128, #130) The proof's approval took the print file's first upload as the final, so the other parts of a film in parts never reached Arte conferida or the hot folder and were downloaded by hand. It now approves every part, and adds the missing ones to a first part already approved. A one-off migration completes the open orders still before Imprimindo. Generated again, the files are named DTF-130-v2-item-1-p2.png, as Marcus asked, so the FlexiPRINT queue and the Hoson list never take them for the first ones. Co-Authored-By: Claude Opus 5.5 (1M context) --- .gitea/workflows/deploy.yml | 2 +- app/api/operator/files.py | 3 +- .../202611271400_print-file-version.sql | 19 ++++++ app/printjobs.py | 14 ++-- app/proof.py | 23 +++++-- tests/print_file_test.py | 2 + tests/test_printfile.py | 3 + tests/test_proof_parts.py | 64 +++++++++++++++++++ 8 files changed, 117 insertions(+), 13 deletions(-) create mode 100644 app/migrations/202611271400_print-file-version.sql create mode 100644 tests/test_proof_parts.py diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index dff217d..cb91db2 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -101,7 +101,7 @@ jobs: # the generator's geometry. The provider suites use a fake transport: they # prove the documented contract, not the integration. - name: Print-file geometry and provider adapters - run: $COMPOSE exec -T api python -m unittest tests.test_printfile tests.test_printpng tests.test_mercadopago tests.test_tiny tests.test_cnpj tests.test_payment_messages tests.test_jadlog tests.test_correios tests.test_quote_review tests.test_moves tests.test_large_files tests.test_grade_check tests.test_schema tests.test_customer_document tests.test_mail_html tests.test_layout_rules tests.test_proof_cut tests.test_proof_render tests.test_sheet_trim tests.test_one_machine tests.test_profiles tests.test_sheet_gap tests.test_reprint tests.test_prumo tests.test_speed tests.test_printpng_validate tests.test_machine_log tests.test_machine_stats tests.test_label tests.test_observability tests.test_worker_drain tests.test_db_outage tests.test_health -v + run: $COMPOSE exec -T api python -m unittest tests.test_printfile tests.test_printpng tests.test_mercadopago tests.test_tiny tests.test_cnpj tests.test_payment_messages tests.test_jadlog tests.test_correios tests.test_quote_review tests.test_moves tests.test_large_files tests.test_grade_check tests.test_schema tests.test_customer_document tests.test_mail_html tests.test_layout_rules tests.test_proof_cut tests.test_proof_render tests.test_sheet_trim tests.test_one_machine tests.test_profiles tests.test_sheet_gap tests.test_reprint tests.test_proof_parts tests.test_prumo tests.test_speed tests.test_printpng_validate tests.test_machine_log tests.test_machine_stats tests.test_label tests.test_observability tests.test_worker_drain tests.test_db_outage tests.test_health -v - name: Runtime and retention regressions run: | diff --git a/app/api/operator/files.py b/app/api/operator/files.py index b2562a7..e0038f3 100644 --- a/app/api/operator/files.py +++ b/app/api/operator/files.py @@ -28,7 +28,8 @@ def named_files(rows): name = row['name'] if name.startswith(older): name = prefix + name[len(older):] - elif not name.startswith(prefix): + # DTF-23-item-1 or, generated again, DTF-23-v2-item-1: already named. + elif not name.startswith(f"DTF-{row['number']}-"): name = f"{prefix}-{name}" stem, dot, ext = name.rpartition('.') copy = 2 diff --git a/app/migrations/202611271400_print-file-version.sql b/app/migrations/202611271400_print-file-version.sql new file mode 100644 index 0000000..decb818 --- /dev/null +++ b/app/migrations/202611271400_print-file-version.sql @@ -0,0 +1,19 @@ +-- "Gerar de novo" names the new files with a version (Marcus, 2026-11-27, +-- #130): DTF-130-v2-item-1-p2.png, so neither the Kanban, the FlexiPRINT +-- queue nor the Hoson list mixes them up with the first ones. +ALTER TABLE dtf_local.print_files ADD COLUMN IF NOT EXISTS generation integer NOT NULL DEFAULT 1; + +-- A Prova OK approved only the first part of a film in parts (the print +-- file's first upload), so the other parts never reached the folders (#114, +-- #128, #130). For the orders still before Imprimindo, the missing parts join +-- the parts the Prumo already approved. +INSERT INTO dtf_local.order_files(id,order_id,upload_id,item_index,kind,note,created_by) +SELECT gen_random_uuid(), p.order_id, p.upload_id, p.item_index, 'final', 'Parte completada (prova)', 'Prumo' +FROM dtf_local.print_parts p JOIN dtf_local.orders o ON o.id=p.order_id +WHERE o.state IN ('rec','tra') AND o.cancelled_at IS NULL AND o.parent_id IS NULL + AND EXISTS (SELECT 1 FROM dtf_local.order_files f JOIN dtf_local.print_parts q + ON q.order_id=f.order_id AND q.item_index=f.item_index AND q.upload_id=f.upload_id + WHERE f.order_id=p.order_id AND f.item_index=p.item_index AND f.kind='final' AND f.active + AND f.created_by='Prumo') + AND NOT EXISTS (SELECT 1 FROM dtf_local.order_files f WHERE f.order_id=p.order_id + AND f.item_index=p.item_index AND f.upload_id=p.upload_id); diff --git a/app/printjobs.py b/app/printjobs.py index 534c257..9ca5432 100644 --- a/app/printjobs.py +++ b/app/printjobs.py @@ -125,7 +125,8 @@ def requeue(c, order_id, items): for index in range(items): c.execute('''INSERT INTO dtf_local.print_files(id,order_id,item_index) VALUES(%s,%s,%s) ON CONFLICT(order_id,item_index) DO UPDATE SET status='pending', attempts=0, - claimed_at=NULL, claim_token=NULL, finished_at=NULL, upload_id=NULL, detail='{}' + claimed_at=NULL, claim_token=NULL, finished_at=NULL, upload_id=NULL, detail='{}', + generation=dtf_local.print_files.generation+1 WHERE dtf_local.print_files.status<>'rendering' ''', (uuid4(), order_id, index)) @@ -323,10 +324,13 @@ def store_tiles(storage, folder, upload_id): storage.store(f'originals/tiles/{upload_id}/{level}/{strip}', os.path.join(folder, level, strip), 'image/png') -def file_prefix(number, item_index): +def file_prefix(number, item_index, generation=1): """How an order's files are named: DTF-23-item-1, the code the Kanban - copies, the footer prints and Tiny keeps (Dropstar, 2026-10-23).""" - return f'DTF-{number}-item-{item_index + 1}' + copies, the footer prints and Tiny keeps (Dropstar, 2026-10-23); after + "Gerar de novo", DTF-23-v2-item-1, so no one takes the new files for the + old ones (Marcus, 2026-11-27, #130).""" + version = f'-v{generation}' if generation and generation > 1 else '' + return f'DTF-{number}{version}-item-{item_index + 1}' def footer(job, item): @@ -401,7 +405,7 @@ def produce(storage, job, item, uploads): proof_of = job['snapshot'].get('proof_of') # The child owns its final file; the class names its proof. prefix = (f'DTF-{job["number"]}-prova-{item.get("stroke_class") or "C3"}' if proof_of - else file_prefix(job['number'], job['item_index'])) + else file_prefix(job['number'], job['item_index'], job.get('generation'))) parts, detail = render(item, files, output, prefix, footer=footer(job, item), tiles=True, limit_cm=PROOF_SHEET_CM if proof_of else None, proof_fraction=item.get('proof_y_fraction') if proof_of else None, diff --git a/app/proof.py b/app/proof.py index e6ceff5..9891751 100644 --- a/app/proof.py +++ b/app/proof.py @@ -123,14 +123,25 @@ def approve_parent(c, parent, operator): if not designer: generated = c.execute('''SELECT p.item_index,p.upload_id FROM dtf_local.print_files p WHERE p.order_id=%s AND p.status='ready' AND p.upload_id IS NOT NULL - AND NOT EXISTS (SELECT 1 FROM dtf_local.order_files f - WHERE f.order_id=p.order_id AND f.item_index=p.item_index AND f.kind='final' AND f.active) ORDER BY p.item_index''', (parent['id'],)).fetchall() for row in generated: - c.execute('''INSERT INTO dtf_local.order_files(id,order_id,upload_id,item_index,kind,note,created_by) - VALUES(%s,%s,%s,%s,'final',%s,'Prumo')''', - (uuid4(), parent['id'], row['upload_id'], row['item_index'], - f'Aprovado pela prova do #{card["number"]}')) + # Every part of a film in parts, not only its first: the others + # never reached the folders (#114, #128, #130). + parts = [r['upload_id'] for r in c.execute('''SELECT upload_id FROM dtf_local.print_parts + WHERE order_id=%s AND item_index=%s ORDER BY part''', (parent['id'], row['item_index'])).fetchall()] + parts = parts or [row['upload_id']] + active = {r['upload_id'] for r in c.execute('''SELECT upload_id FROM dtf_local.order_files + WHERE order_id=%s AND item_index=%s AND kind='final' AND active''', + (parent['id'], row['item_index'])).fetchall()} + # A final someone prepared or approved stays as it is. + if active and not active <= set(parts): + continue + for upload_id in parts: + if upload_id not in active: + c.execute('''INSERT INTO dtf_local.order_files(id,order_id,upload_id,item_index,kind,note,created_by) + VALUES(%s,%s,%s,%s,'final',%s,'Prumo')''', + (uuid4(), parent['id'], upload_id, row['item_index'], + f'Aprovado pela prova do #{card["number"]}')) treatment_clause = f'AND {TREATED}' if designer else '' ready = all(c.execute(f'''SELECT 1 FROM dtf_local.order_files f JOIN dtf_local.orders o ON o.id=f.order_id diff --git a/tests/print_file_test.py b/tests/print_file_test.py index f30aaed..88ce7e2 100644 --- a/tests/print_file_test.py +++ b/tests/print_file_test.py @@ -219,6 +219,8 @@ def run(): assert (state, finals, back['reason'], back['back']) == ('rec', 0, 'Arquivo gerado de novo', True), (state, finals, back) again, remade = wait_print(client, again['id'], 'ready') assert str(remade['upload_id']) != str(made['upload_id']), (made, remade) + # Its name carries the version, so it is not taken for the first (#130). + assert remade['name'] == 'DTF-%d-v2-item-1.png' % again['number'], remade['name'] print('PASS: a ready file is generated again from Arte conferida, back in Arte recebida to approve') # The generated file approved, sent to Correção (its final stops counting) diff --git a/tests/test_printfile.py b/tests/test_printfile.py index 82910e8..e87eb27 100644 --- a/tests/test_printfile.py +++ b/tests/test_printfile.py @@ -145,6 +145,9 @@ class PrintFileTests(unittest.TestCase): self.assertNotIn(b'ESPELHADO', footer_commands(footer(job, item([placement(0, 0, 20, 10)], [source(20, 10)], 10)), (100, 80))) # The files start with the code the Kanban copies. self.assertEqual(file_prefix(7, 1), 'DTF-7-item-2') + # Generated again: the version in the name (Marcus, 2026-11-27, #130). + self.assertEqual(file_prefix(130, 0, 2), 'DTF-130-v2-item-1') + self.assertEqual(file_prefix(130, 0, 1), 'DTF-130-item-1') def test_jpeg_bytes_are_embedded_unchanged(self): path = self.save(quadrants(), 'a.jpg', quality=90) diff --git a/tests/test_proof_parts.py b/tests/test_proof_parts.py new file mode 100644 index 0000000..6a19d51 --- /dev/null +++ b/tests/test_proof_parts.py @@ -0,0 +1,64 @@ +"""Prova OK approves every part of a film in parts (#114, #128, #130): only +the first part became a final, and the others never reached the folders. + +Run from the repository root: python3 -m unittest tests.test_proof_parts -v. +""" +import unittest + +from app.proof import approve_parent + + +class Rows: + def __init__(self, rows): + self.rows = rows + + def fetchone(self): + return self.rows[0] if self.rows else None + + def fetchall(self): + return self.rows + + +class Database: + """Just the reads and writes approve_parent makes.""" + + def __init__(self, parts, active): + self.parts, self.active, self.inserted = parts, active, [] + + def execute(self, sql, params=()): + text = ' '.join(sql.split()) + if 'proof_result' in text: + return Rows([{'number': 131}]) + if 'FROM dtf_local.print_files' in text: + return Rows([{'item_index': 0, 'upload_id': self.parts[0]}]) + if 'FROM dtf_local.print_parts' in text: + return Rows([{'upload_id': u} for u in self.parts]) + if text.startswith('SELECT upload_id FROM dtf_local.order_files'): + return Rows([{'upload_id': u} for u in self.active]) + if text.startswith('INSERT INTO dtf_local.order_files'): + self.inserted.append(params[2]) + return Rows([{'ok': 1}]) + + +ORDER = {'id': 'o', 'state': 'rec', 'cancelled_at': None, 'snapshot': {'items': [{'mode': 'file'}]}} + + +class ProofPartsTests(unittest.TestCase): + def test_every_part_becomes_a_final(self): + db = Database(['p1', 'p2', 'p3'], set()) + approve_parent(db, ORDER, 'Prumo') + self.assertEqual(db.inserted, ['p1', 'p2', 'p3']) + + def test_the_missing_parts_join_a_first_part_already_approved(self): + db = Database(['p1', 'p2'], {'p1'}) + approve_parent(db, ORDER, 'Prumo') + self.assertEqual(db.inserted, ['p2']) + + def test_a_final_someone_prepared_stays(self): + db = Database(['p1', 'p2'], {'treated'}) + approve_parent(db, ORDER, 'Prumo') + self.assertEqual(db.inserted, []) + + +if __name__ == '__main__': + unittest.main()