Fetch fresh calibration on every print

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-10-05 14:08:11 +03:30
parent 9f38a02797
commit 13135b5999
4 changed files with 76 additions and 34 deletions

View File

@@ -1,6 +1,6 @@
function printOrder(id) {
// Open the print page in a new window/tab
const win = window.open(`/print-page/${id}/`, '_blank');
// Cache-bust so a second print of the same order is not a stale HTML document
const win = window.open(`/print-page/${id}/?t=${Date.now()}`, '_blank');
if (win) {
win.focus();
} else {

View File

@@ -6,7 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Print Order #{{ order.id }}</title>
<link rel="stylesheet" href="{% static 'slips/css/label_print.css' %}">
<style>
<style id="calibration-vars">
{{ calibration_css|safe }}
</style>
<style>
@@ -48,10 +48,15 @@
font-weight: bold;
}
.preview-controls button:hover {
.preview-controls button:hover:not(:disabled) {
background: #2563eb;
}
.preview-controls button:disabled {
opacity: 0.55;
cursor: wait;
}
.page-wrapper {
margin-top: 80px; /* Space for controls */
display: flex;
@@ -126,8 +131,8 @@
{{ order.canister_count }} Canisters ({{ order.canister_pages }} Pages) |
{{ order.box_count }} Boxes ({{ order.box_pages }} Pages)
</p>
<button onclick="window.print()">🖨️ Print Now</button>
<button onclick="window.close()" style="background:#64748b; margin-left: 10px;">Close</button>
<button id="btn-print" type="button" disabled>🖨️ Print Now</button>
<button type="button" onclick="window.close()" style="background:#64748b; margin-left: 10px;">Close</button>
</div>
<div class="page-wrapper">
@@ -148,8 +153,48 @@
</div>
<script>
// Optional: auto-trigger print dialog when opened
// window.onload = function() { window.print(); }
(function () {
const FIELDS = ['batch', 'date', 'next', 'weight'];
const varsEl = document.getElementById('calibration-vars');
const btn = document.getElementById('btn-print');
function cssFromCal(cal) {
const lines = [':root {'];
for (const field of FIELDS) {
const v = cal[field];
lines.push(' --' + field + '-top: ' + v.top + 'mm;');
lines.push(' --' + field + '-left: ' + v.left + 'mm;');
lines.push(' --' + field + '-font: ' + v.font + 'pt;');
lines.push(' --' + field + '-bottom-top: ' + v.bottom_top + 'mm;');
if (v.bottom_left != null) {
lines.push(' --' + field + '-bottom-left: ' + v.bottom_left + 'mm;');
}
if (v.right_left != null) {
lines.push(' --' + field + '-right-left: ' + v.right_left + 'mm;');
}
}
lines.push('}');
return lines.join('\n');
}
function enablePrint() {
btn.disabled = false;
btn.onclick = function () { window.print(); };
}
fetch('/api/calibration', { cache: 'no-store' })
.then(function (res) {
if (!res.ok) throw new Error('HTTP ' + res.status);
return res.json();
})
.then(function (cal) {
varsEl.textContent = cssFromCal(cal);
})
.catch(function () {
// Keep server-rendered vars; still allow print
})
.finally(enablePrint);
})();
</script>
</body>
</html>

View File

@@ -1,4 +1,5 @@
from django.shortcuts import render, redirect, get_object_or_404
from django.views.decorators.cache import never_cache
from .calibration import css_vars, load_calibration
from .models import Product, Order
from .print_layout import build_print_pages
@@ -26,6 +27,7 @@ def print_queue(request):
})
@never_cache
def print_page(request, order_id):
order = get_object_or_404(Order.objects.select_related('product'), id=order_id)
pages = build_print_pages(order)
@@ -40,6 +42,7 @@ def print_page(request, order_id):
})
@never_cache
def calibrate(request):
cal = load_calibration()
return render(request, 'slips/calibrate.html', {