From 742ea5f5bdb72893a1b02fe4b4200b562bb3c75d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cau=C3=AA=20Faleiros?= Date: Fri, 27 Nov 2026 14:19:37 -0300 Subject: [PATCH] fix: the choice of items to reprint ends with the print, a new final or "Gerar de novo", so a later correction always goes to the machine (#105) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Kept until Finalizado, an old choice would leave out an item corrected after it (an order back from Revisão, or a second correction). It is now cleared when the pass to Revisão records it, when a final changes and when the files are generated again; the next entry to the queue works it out anew. An integration test follows the whole reprint: first print, one item corrected, the queue, the hot folder with that item only, Revisão. Co-Authored-By: Claude Opus 5.5 (1M context) --- app/api/operator/orders.py | 12 +++++--- app/artwork.py | 3 ++ tests/print_file_test.py | 62 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 4 deletions(-) diff --git a/app/api/operator/orders.py b/app/api/operator/orders.py index 9fdfdc1..43f2c75 100644 --- a/app/api/operator/orders.py +++ b/app/api/operator/orders.py @@ -519,8 +519,10 @@ def move(uid: UUID, body: Move, request: Request, user=Depends(operator)): default = hotfolder.reprint_default(finals, taken, len(row['snapshot']['items'])) if default: c.execute('UPDATE dtf_local.orders SET print_items=%s WHERE id=%s', (default, uid)) - # Finished: the next time it goes back, the choice starts over. - if body.state == 'fin' and not back: + # Printed (its items kept on the movement above) or finished: the + # choice is done with, and the next pass to the queue works it out + # again from what changed since. + if printed_items is not None or (body.state == 'fin' and not back): c.execute('UPDATE dtf_local.orders SET print_items=NULL WHERE id=%s', (uid,)) # Pulled to Imprimindo, its files go to its machine's hot folder; out # of it, what Flexi did not take leaves (item 10). @@ -632,8 +634,10 @@ def regenerate_ready(uid: UUID, user=Depends(can('produce'))): EXISTS (SELECT 1 FROM dtf_local.print_parts p WHERE p.order_id=f.order_id AND p.upload_id=f.upload_id) OR EXISTS (SELECT 1 FROM dtf_local.print_files p WHERE p.order_id=f.order_id AND p.upload_id=f.upload_id))''', (uid,)) - # The new version tells the board and the folders the files changed. - c.execute("UPDATE dtf_local.orders SET state='rec', version=version+1, updated_at=now() WHERE id=%s", (uid,)) + # The new version tells the board and the folders the files changed; + # every file is new, so any choice of items to reprint is over. + c.execute('''UPDATE dtf_local.orders SET state='rec', print_items=NULL, version=version+1, updated_at=now() + WHERE id=%s''', (uid,)) if row['state'] == 'imp': hotfolder.stop(c, uid) if row['state'] != 'rec': diff --git a/app/artwork.py b/app/artwork.py index 932cf6c..b07079c 100644 --- a/app/artwork.py +++ b/app/artwork.py @@ -106,6 +106,9 @@ def submit_files(c, order, body, identity, kind, actor): c.execute("UPDATE dtf_local.order_files SET active=false WHERE order_id=%s AND kind IN ('correction','final')", (order['id'],)) else: c.execute('UPDATE dtf_local.order_files SET active=false WHERE order_id=%s AND kind=%s', (order['id'],kind)) + # A final changed: which items go to the machine again is worked out + # anew when the order enters the queue (#105). + c.execute('UPDATE dtf_local.orders SET print_items=NULL WHERE id=%s', (order['id'],)) for ref in body.files: if ref.upload_id in kept or ref.upload_id in revived: c.execute("UPDATE dtf_local.order_files SET active=true WHERE order_id=%s AND upload_id=%s AND kind='final'", diff --git a/tests/print_file_test.py b/tests/print_file_test.py index 88ce7e2..d301719 100644 --- a/tests/print_file_test.py +++ b/tests/print_file_test.py @@ -435,6 +435,68 @@ def run(): assert (moved['operator'], moved['reason']) == ('Prumo', 'Prova OK'), moved print('PASS: a file finished after Prova OK becomes the final and queues the order') + # Reprinting some items (Marcus, 2026-11-26, #105): an order the machine + # already printed, back in the queue after one item's final changed, sends + # only that item; the pass to Revisão keeps it for the machine's metres and + # the choice is then over. + first_art = upload_bytes(client, artwork(), name='LOCAL-REPRINT-A.png') + second_art = upload_bytes(client, artwork(), name='LOCAL-REPRINT-B.png') + two = [loose_item(first_art), loose_item(second_art)] + quote = client.call('/quotes', {'request_key': str(uuid4()), 'customer': CUSTOMER, + 'items': two, 'freight': {'service': 'pickup'}}) + approval(client, quote, two) + reprint = client.call('/orders/dev-paid', {'quote_id': quote['id']}) + rid = reprint['id'] + deadline = time.monotonic() + 120 + while True: + board_order = next(o for o in client.call('/operator/board', operator=True)['orders'] if o['id'] == rid) + made = {r['item_index']: r for r in board_order['print_files']} + if len(made) == 2 and all(r['status'] == 'ready' for r in made.values()): + break + assert time.monotonic() < deadline, made + time.sleep(1) + generated = {i: str(made[i]['upload_id']) for i in (0, 1)} + v = client.call('/operator/orders/' + rid + '/final-files', + {'version': board_order['version'], 'note': 'ok', + 'files': [{'item_index': i, 'upload_id': generated[i]} for i in (0, 1)]}, operator=True)['version'] + with db.connect() as c: + # The machine took both files on a first print. + for i in (0, 1): + c.execute('''INSERT INTO dtf_local.hot_parts(order_id,round,seq,item_index,upload_id,name,state) + VALUES(%s,1,%s,%s,%s,%s,'consumed')''', (rid, i + 1, i, generated[i], f'item-{i + 1}.png')) + # Item 2 is corrected; item 1 keeps its file ("Manter o arquivo atual"). + corrected = upload_bytes(client, artwork(), name='LOCAL-REPRINT-B2.png', order_id=rid) + v = client.call('/operator/orders/' + rid + '/final-files', + {'version': v, 'note': 'arte corrigida', + 'files': [{'item_index': 0, 'upload_id': generated[0]}, + {'item_index': 1, 'upload_id': corrected}]}, operator=True)['version'] + board_order = next(o for o in client.call('/operator/board', operator=True)['orders'] if o['id'] == rid) + assert board_order['printed_before'] and board_order['print_suggested'] == [1], board_order.get('print_suggested') + client.call('/operator/orders/' + rid + '/move', {'state': 'tra', 'version': v}, operator=True) + from app import hotfolder + with db.connect() as c: + assert c.execute('SELECT print_items FROM dtf_local.orders WHERE id=%s', (rid,)).fetchone()['print_items'] == [1] + hotfolder.start(c, rid) + queued = c.execute('SELECT item_index,upload_id FROM dtf_local.hot_parts WHERE order_id=%s AND round=2', + (rid,)).fetchall() + assert [(q['item_index'], str(q['upload_id'])) for q in queued] == [(1, corrected)], queued + c.execute("UPDATE dtf_local.hot_parts SET state='consumed' WHERE order_id=%s AND round=2", (rid,)) + c.execute("UPDATE dtf_local.orders SET state='imp' WHERE id=%s", (rid,)) + v = c.execute('SELECT version FROM dtf_local.orders WHERE id=%s', (rid,)).fetchone()['version'] + client.call('/operator/orders/' + rid + '/move', {'state': 'rev', 'version': v}, operator=True) + with db.connect() as c: + moved = c.execute('''SELECT items FROM dtf_local.movements WHERE order_id=%s AND to_state='rev' + ORDER BY id DESC LIMIT 1''', (rid,)).fetchone() + left = c.execute('SELECT print_items FROM dtf_local.orders WHERE id=%s', (rid,)).fetchone()['print_items'] + assert moved['items'] == [1] and left is None, (moved, left) + # Chosen by hand: every item means all, as on a first print. + with db.connect() as c: + c.execute("UPDATE dtf_local.orders SET state='rec' WHERE id=%s", (rid,)) + assert client.call('/operator/orders/' + rid + '/print-items', {'items': [0, 1]}, operator=True)['print_items'] is None + assert client.call('/operator/orders/' + rid + '/print-items', {'items': [0]}, operator=True)['print_items'] == [0] + client.call('/operator/orders/' + rid + '/print-items', {'items': [5]}, operator=True, expected=422) + print('PASS: a reprint sends only the item whose final changed, and the choice ends with the print') + # A CDR cannot be decoded by the worker. The designer prepares the full # parent PNG, then the worker automatically cuts the child's proof from it. manual_source = upload_bytes(client, b'LOCAL PRINT TEST - NOT AN IMAGE', name='MANUAL-PROOF.cdr')