Add amount field to job creation — auto-creates bracket when > 1

Amount > 1 with no bracket selected: auto-creates a bracket named
'{customer} – {file} ×{amount}', assigns all jobs to it, and redirects
to the new bracket page. Amount = 1 behaves as before.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Martin Hohenberg
2026-06-18 22:06:35 +02:00
parent 4c7acb1c52
commit dc73c9a8c1
2 changed files with 54 additions and 21 deletions

View File

@@ -149,6 +149,7 @@ async def create_job(
customer: str = Form(""),
filament_id: str = Form(""),
bracket_id: str = Form(""),
amount: int = Form(1),
notes: str = Form(""),
stl_file: UploadFile = File(None),
stl_select: str = Form(""),
@@ -162,20 +163,40 @@ async def create_job(
elif stl_select:
filename = stl_select
job = PrintJob(
job_uuid=str(_uuid.uuid4()),
name=Path(filename).stem if filename else (customer.strip() or "Unnamed"),
file_name=filename,
printer_id=int(printer_id) if printer_id else None,
filament_id=int(filament_id) if filament_id else None,
bracket_id=int(bracket_id) if bracket_id else None,
customer=customer.strip() or None,
notes=notes.strip() or None,
)
db.add(job)
db.flush()
db.add(JobLog(job_id=job.id, message="Job created"))
amount = max(1, amount)
customer_str = customer.strip() or None
job_name = Path(filename).stem if filename else (customer_str or "Unnamed")
# Resolve bracket — auto-create one when printing multiple copies
effective_bracket_id = int(bracket_id) if bracket_id else None
if amount > 1 and not effective_bracket_id:
bracket_name = f"{job_name} ×{amount}"
if customer_str:
bracket_name = f"{customer_str} {bracket_name}"
new_bracket = JobBracket(name=bracket_name, customer=customer_str)
db.add(new_bracket)
db.flush()
effective_bracket_id = new_bracket.id
for _ in range(amount):
job = PrintJob(
job_uuid=str(_uuid.uuid4()),
name=job_name,
file_name=filename,
printer_id=int(printer_id) if printer_id else None,
filament_id=int(filament_id) if filament_id else None,
bracket_id=effective_bracket_id,
customer=customer_str,
notes=notes.strip() or None,
)
db.add(job)
db.flush()
db.add(JobLog(job_id=job.id, message="Job created"))
db.commit()
if effective_bracket_id and amount > 1:
return RedirectResponse(url=f"/brackets/{effective_bracket_id}", status_code=HTTP_303_SEE_OTHER)
return RedirectResponse(url="/jobs", status_code=HTTP_303_SEE_OTHER)

View File

@@ -153,14 +153,26 @@
</select>
</div>
<div>
<label class="form-label text-secondary small">Bracket <span class="text-secondary fw-normal">(optional)</span></label>
<select name="bracket_id" class="form-select bg-dark border-secondary text-white">
<option value="">— none —</option>
{% for b in brackets %}
<option value="{{ b.id }}">{{ b.name }}{% if b.customer %} · {{ b.customer }}{% endif %}</option>
{% endfor %}
</select>
<div class="row g-3">
<div class="col-md-4">
<label class="form-label text-secondary small">Amount</label>
<input type="number" name="amount"
class="form-control bg-dark border-secondary text-white"
value="1" min="1" max="500"
title="&gt;1 auto-creates a bracket">
</div>
<div class="col-md-8">
<label class="form-label text-secondary small">
Bracket
<span class="text-secondary fw-normal">(auto-created when amount &gt; 1)</span>
</label>
<select name="bracket_id" class="form-select bg-dark border-secondary text-white">
<option value="">— none / auto —</option>
{% for b in brackets %}
<option value="{{ b.id }}">{{ b.name }}{% if b.customer %} · {{ b.customer }}{% endif %}</option>
{% endfor %}
</select>
</div>
</div>
<div>