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)