Keep tape options across the print redirect, honour saved rotation

Printing redirected back to a bare tape page, so every option - rotation
most visibly - reverted to the default the moment you pressed Print.
Carry the chosen options through the redirect instead.

Also read tape_rotate_default so an untick can be made permanent in
Settings; it only applies to a fresh arrival, never over an explicit
choice on the page.
This commit is contained in:
jessikitty committed 2026-10-12 22:11:09 +11:00
1 parent 5319870a7c
commit 9406de237d
1 file changed
+46 -12
+46 -12
View File
@@ -36,9 +36,38 @@ def printer_config():
or '/ipp/print'), or '/ipp/print'),
'format': (SiteSetting.get('tape_printer_format', '') or '').strip(), 'format': (SiteSetting.get('tape_printer_format', '') or '').strip(),
'tape': SiteSetting.get('tape_default', tape_printer.DEFAULT_TAPE), 'tape': SiteSetting.get('tape_default', tape_printer.DEFAULT_TAPE),
'rotate': SiteSetting.get_bool('tape_rotate_default', True),
} }
def _options_query(options, labels):
"""
The chosen options as query args, so a redirect back to the tape page
comes back exactly as the user left it. Without this, printing bounced
you to a fresh page and every choice - rotation especially - reverted
to the default.
"""
query = {
'ids': ','.join(str(l.id) for l in labels),
tape_printer.FORM_MARKER: '1',
'tape': options.tape,
'copies': options.copies,
'font_scale': options.font_scale,
'image_percent': options.image_percent,
'padding_mm': options.padding_mm,
'align': options.align,
'fields': options.fields,
}
# Checkboxes: only present when on, which is what the parser expects.
if options.rotate:
query['rotate'] = '1'
if options.outline:
query['outline'] = '1'
if options.fixed_length_mm:
query['fixed_length_mm'] = options.fixed_length_mm
return query
def _selected_labels(): def _selected_labels():
"""Resolve the selection from ids= or category=, same as sheet printing.""" """Resolve the selection from ids= or category=, same as sheet printing."""
ids = request.values.getlist('ids') ids = request.values.getlist('ids')
@@ -69,10 +98,14 @@ def index():
flash('Select some labels to print first.', 'warning') flash('Select some labels to print first.', 'warning')
return redirect(url_for('labels.index')) return redirect(url_for('labels.index'))
config = printer_config()
source = request.form if request.method == 'POST' else request.args source = request.form if request.method == 'POST' else request.args
options = tape_printer.options_from_form(source) # rotate_default only applies when the form was not submitted, so an
# explicit untick on the page always wins over the saved preference.
options = tape_printer.options_from_form(
source, rotate_default=config['rotate'])
if not source.get('tape'): if not source.get('tape'):
options.tape = printer_config()['tape'] options.tape = config['tape']
return render_template( return render_template(
'admin/labels/tape.html', 'admin/labels/tape.html',
@@ -88,7 +121,7 @@ def index():
('category_code', 'Category code'), ('location', 'Drawer'), ('category_code', 'Category code'), ('location', 'Drawer'),
('colour', 'Colour name'), ('accent', 'Colour stripe'), ('colour', 'Colour name'), ('accent', 'Colour stripe'),
], ],
printer=printer_config(), printer=config,
) )
@@ -100,7 +133,8 @@ def preview():
if not label_id: if not label_id:
abort(400, 'preview.png needs an ?id= of a label.') abort(400, 'preview.png needs an ?id= of a label.')
label = PartLabel.query.get_or_404(label_id) label = PartLabel.query.get_or_404(label_id)
options = tape_printer.options_from_form(request.args) options = tape_printer.options_from_form(
request.args, rotate_default=printer_config()['rotate'])
buffer = tape_printer.preview_png( buffer = tape_printer.preview_png(
label, options, current_app.config['UPLOAD_FOLDER']) label, options, current_app.config['UPLOAD_FOLDER'])
return send_file(buffer, mimetype='image/png') return send_file(buffer, mimetype='image/png')
@@ -151,8 +185,9 @@ def print_tape():
flash('Nothing selected to print.', 'warning') flash('Nothing selected to print.', 'warning')
return redirect(url_for('labels.index')) return redirect(url_for('labels.index'))
options = tape_printer.options_from_form(request.form)
config = printer_config() config = printer_config()
options = tape_printer.options_from_form(
request.form, rotate_default=config['rotate'])
host = config['host'] host = config['host']
if not host: if not host:
flash('No tape printer address configured. Set it in Label ' flash('No tape printer address configured. Set it in Label '
@@ -169,13 +204,13 @@ def print_tape():
mime_type = info.get('usable_format') mime_type = info.get('usable_format')
except ipp_client.IPPError as exc: except ipp_client.IPPError as exc:
flash(f'Could not reach the printer: {exc}', 'danger') flash(f'Could not reach the printer: {exc}', 'danger')
return redirect(url_for('tape.index', ids=','.join( return redirect(url_for('tape.index',
str(l.id) for l in labels))) **_options_query(options, labels)))
if not mime_type: if not mime_type:
flash('The printer did not offer a format LIM can produce. Use ' flash('The printer did not offer a format LIM can produce. Use '
'Download PDF instead.', 'warning') 'Download PDF instead.', 'warning')
return redirect(url_for('tape.index', return redirect(url_for('tape.index',
ids=','.join(str(l.id) for l in labels))) **_options_query(options, labels)))
upload_folder = current_app.config['UPLOAD_FOLDER'] upload_folder = current_app.config['UPLOAD_FOLDER']
sent = 0 sent = 0
@@ -205,9 +240,7 @@ def print_tape():
for problem in failures[:5]: for problem in failures[:5]:
flash(problem, 'danger') flash(problem, 'danger')
return redirect(url_for('tape.index', return redirect(url_for('tape.index', **_options_query(options, labels)))
ids=','.join(str(l.id) for l in labels),
tape=options.tape))
@tape_bp.route('/download.pdf', methods=['GET', 'POST']) @tape_bp.route('/download.pdf', methods=['GET', 'POST'])
@@ -221,7 +254,8 @@ def download_pdf():
return redirect(url_for('labels.index')) return redirect(url_for('labels.index'))
source = request.form if request.method == 'POST' else request.args source = request.form if request.method == 'POST' else request.args
options = tape_printer.options_from_form(source) options = tape_printer.options_from_form(
source, rotate_default=printer_config()['rotate'])
buffer = tape_printer.render_batch_pdf( buffer = tape_printer.render_batch_pdf(
labels, options, current_app.config['UPLOAD_FOLDER']) labels, options, current_app.config['UPLOAD_FOLDER'])
if buffer is None: if buffer is None: