Add job creation with UUID, STL upload/library, search, job log, TZ, and CLAUDE.md
- PrintJob: job_uuid (UUID4), customer, logs relationship - JobLog model + migration 0004 (also includes job_logs table) - POST /jobs: upload STL or select from library, auto-logs 'Job created' - GET /jobs?q=: search by customer or filename across all statuses - app/stl.py: STL_UPLOAD_DIR helper (from env, default /data/stl) - docker-compose: named volume stl_files mounted at /data/stl - .env.install: added STL_UPLOAD_DIR and TZ=Europe/Berlin - CLAUDE.md: full project context for future sessions Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from app.models.base import Base
|
||||
from app.models.filament import Filament
|
||||
from app.models.job_log import JobLog
|
||||
from app.models.pricing import PricingConfig
|
||||
from app.models.print_job import PrintJob
|
||||
from app.models.printer import Printer
|
||||
@@ -7,4 +8,4 @@ from app.models.printer_log import PrinterLog
|
||||
from app.models.printer_type import PrinterType
|
||||
from app.models.todo import Todo
|
||||
|
||||
__all__ = ["Base", "Printer", "PrinterLog", "PrinterType", "Filament", "PrintJob", "Todo", "PricingConfig"]
|
||||
__all__ = ["Base", "Printer", "PrinterLog", "PrinterType", "Filament", "JobLog", "PrintJob", "Todo", "PricingConfig"]
|
||||
|
||||
17
app/models/job_log.py
Normal file
17
app/models/job_log.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import DateTime, ForeignKey, Text, func
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.models.base import Base
|
||||
|
||||
|
||||
class JobLog(Base):
|
||||
__tablename__ = "job_logs"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
job_id: Mapped[int] = mapped_column(ForeignKey("print_jobs.id"))
|
||||
message: Mapped[str] = mapped_column(Text)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
|
||||
|
||||
job: Mapped["PrintJob"] = relationship(back_populates="logs")
|
||||
@@ -1,4 +1,5 @@
|
||||
import enum
|
||||
import uuid as _uuid
|
||||
from datetime import datetime
|
||||
from decimal import Decimal
|
||||
|
||||
@@ -20,9 +21,11 @@ class PrintJob(Base):
|
||||
__tablename__ = "print_jobs"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
job_uuid: Mapped[str | None] = mapped_column(String(36), unique=True, default=lambda: str(_uuid.uuid4()))
|
||||
printer_id: Mapped[int] = mapped_column(ForeignKey("printers.id"))
|
||||
filament_id: Mapped[int | None] = mapped_column(ForeignKey("filaments.id"))
|
||||
name: Mapped[str] = mapped_column(String(200))
|
||||
customer: Mapped[str | None] = mapped_column(String(200))
|
||||
file_name: Mapped[str | None] = mapped_column(String(255))
|
||||
status: Mapped[JobStatus] = mapped_column(Enum(JobStatus), default=JobStatus.queued)
|
||||
queue_position: Mapped[int | None] = mapped_column(Integer)
|
||||
@@ -39,3 +42,6 @@ class PrintJob(Base):
|
||||
|
||||
printer: Mapped["Printer"] = relationship(back_populates="jobs")
|
||||
filament: Mapped["Filament | None"] = relationship(back_populates="jobs")
|
||||
logs: Mapped[list["JobLog"]] = relationship(
|
||||
back_populates="job", order_by="JobLog.created_at.desc()"
|
||||
)
|
||||
|
||||
@@ -1,22 +1,30 @@
|
||||
from fastapi import APIRouter, Depends, Form, Request
|
||||
import uuid as _uuid
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import APIRouter, Depends, File, Form, Request, UploadFile
|
||||
from fastapi.responses import HTMLResponse, RedirectResponse
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from sqlalchemy import or_
|
||||
from sqlalchemy.orm import Session
|
||||
from starlette.status import HTTP_303_SEE_OTHER
|
||||
|
||||
from app.db import get_db
|
||||
from app.models.filament import Filament
|
||||
from app.models.job_log import JobLog
|
||||
from app.models.print_job import JobStatus, PrintJob
|
||||
from app.models.printer import Printer, PrinterStatus
|
||||
from app.models.printer_log import PrinterLog
|
||||
from app.models.printer_type import PrinterType
|
||||
from app.models.pricing import PricingConfig
|
||||
from app.models.todo import Todo
|
||||
from app.stl import get_stl_dir, list_stl_files
|
||||
|
||||
router = APIRouter()
|
||||
templates = Jinja2Templates(directory="app/templates")
|
||||
|
||||
|
||||
# ── Dashboard ──────────────────────────────────────────────────────────────────
|
||||
|
||||
@router.get("/", response_class=HTMLResponse)
|
||||
def dashboard(request: Request, db: Session = Depends(get_db)):
|
||||
printers = db.query(Printer).order_by(Printer.name).all()
|
||||
@@ -34,6 +42,8 @@ def dashboard(request: Request, db: Session = Depends(get_db)):
|
||||
})
|
||||
|
||||
|
||||
# ── Printers ───────────────────────────────────────────────────────────────────
|
||||
|
||||
@router.get("/printers", response_class=HTMLResponse)
|
||||
def printers_page(request: Request, db: Session = Depends(get_db)):
|
||||
return templates.TemplateResponse("printers.html", {
|
||||
@@ -66,9 +76,9 @@ def create_printer(
|
||||
|
||||
@router.get("/printers/{printer_id}", response_class=HTMLResponse)
|
||||
def printer_detail(printer_id: int, request: Request, db: Session = Depends(get_db)):
|
||||
from fastapi import HTTPException
|
||||
printer = db.query(Printer).filter(Printer.id == printer_id).first()
|
||||
if not printer:
|
||||
from fastapi import HTTPException
|
||||
raise HTTPException(status_code=404, detail="Printer not found")
|
||||
return templates.TemplateResponse("printer_detail.html", {
|
||||
"request": request,
|
||||
@@ -79,12 +89,13 @@ def printer_detail(printer_id: int, request: Request, db: Session = Depends(get_
|
||||
|
||||
@router.post("/printers/{printer_id}/log")
|
||||
def add_printer_log(printer_id: int, message: str = Form(...), db: Session = Depends(get_db)):
|
||||
entry = PrinterLog(printer_id=printer_id, message=message.strip())
|
||||
db.add(entry)
|
||||
db.add(PrinterLog(printer_id=printer_id, message=message.strip()))
|
||||
db.commit()
|
||||
return RedirectResponse(url=f"/printers/{printer_id}", status_code=HTTP_303_SEE_OTHER)
|
||||
|
||||
|
||||
# ── Printer types ──────────────────────────────────────────────────────────────
|
||||
|
||||
@router.get("/printer-types", response_class=HTMLResponse)
|
||||
def printer_types_page(request: Request, db: Session = Depends(get_db)):
|
||||
return templates.TemplateResponse("printer_types.html", {
|
||||
@@ -102,21 +113,67 @@ def create_printer_type(
|
||||
bed_z_mm: int = Form(...),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
pt = PrinterType(name=name.strip(), bed_x_mm=bed_x_mm, bed_y_mm=bed_y_mm, bed_z_mm=bed_z_mm)
|
||||
db.add(pt)
|
||||
db.add(PrinterType(name=name.strip(), bed_x_mm=bed_x_mm, bed_y_mm=bed_y_mm, bed_z_mm=bed_z_mm))
|
||||
db.commit()
|
||||
return RedirectResponse(url="/printer-types", status_code=HTTP_303_SEE_OTHER)
|
||||
|
||||
|
||||
# ── Jobs ───────────────────────────────────────────────────────────────────────
|
||||
|
||||
@router.get("/jobs", response_class=HTMLResponse)
|
||||
def jobs_page(request: Request, db: Session = Depends(get_db)):
|
||||
def jobs_page(request: Request, q: str = "", db: Session = Depends(get_db)):
|
||||
query = db.query(PrintJob)
|
||||
if q:
|
||||
like = f"%{q}%"
|
||||
query = query.filter(
|
||||
or_(PrintJob.customer.ilike(like), PrintJob.file_name.ilike(like))
|
||||
)
|
||||
return templates.TemplateResponse("jobs.html", {
|
||||
"request": request,
|
||||
"active": "jobs",
|
||||
"jobs": db.query(PrintJob).order_by(PrintJob.created_at.desc()).all(),
|
||||
"jobs": query.order_by(PrintJob.created_at.desc()).all(),
|
||||
"q": q,
|
||||
"printers": db.query(Printer).order_by(Printer.name).all(),
|
||||
"filaments": db.query(Filament).order_by(Filament.material, Filament.color).all(),
|
||||
"stl_files": list_stl_files(),
|
||||
})
|
||||
|
||||
|
||||
@router.post("/jobs")
|
||||
async def create_job(
|
||||
printer_id: int = Form(...),
|
||||
customer: str = Form(""),
|
||||
filament_id: str = Form(""),
|
||||
notes: str = Form(""),
|
||||
stl_file: UploadFile = File(None),
|
||||
stl_select: str = Form(""),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
filename = None
|
||||
if stl_file and stl_file.filename:
|
||||
filename = stl_file.filename
|
||||
(get_stl_dir() / filename).write_bytes(await stl_file.read())
|
||||
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=printer_id,
|
||||
filament_id=int(filament_id) if filament_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"))
|
||||
db.commit()
|
||||
return RedirectResponse(url="/jobs", status_code=HTTP_303_SEE_OTHER)
|
||||
|
||||
|
||||
# ── Filaments ──────────────────────────────────────────────────────────────────
|
||||
|
||||
@router.get("/filaments", response_class=HTMLResponse)
|
||||
def filaments_page(request: Request, db: Session = Depends(get_db)):
|
||||
return templates.TemplateResponse("filaments.html", {
|
||||
@@ -126,6 +183,8 @@ def filaments_page(request: Request, db: Session = Depends(get_db)):
|
||||
})
|
||||
|
||||
|
||||
# ── To-Do ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
@router.get("/todos", response_class=HTMLResponse)
|
||||
def todos_page(request: Request, db: Session = Depends(get_db)):
|
||||
return templates.TemplateResponse("todos.html", {
|
||||
@@ -135,6 +194,8 @@ def todos_page(request: Request, db: Session = Depends(get_db)):
|
||||
})
|
||||
|
||||
|
||||
# ── Pricing ────────────────────────────────────────────────────────────────────
|
||||
|
||||
@router.get("/pricing", response_class=HTMLResponse)
|
||||
def pricing_page(request: Request, db: Session = Depends(get_db)):
|
||||
return templates.TemplateResponse("pricing.html", {
|
||||
|
||||
20
app/stl.py
Normal file
20
app/stl.py
Normal file
@@ -0,0 +1,20 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
_SUPPORTED = {".stl", ".3mf", ".gcode"}
|
||||
|
||||
|
||||
def get_stl_dir() -> Path:
|
||||
d = Path(os.environ.get("STL_UPLOAD_DIR", "/data/stl"))
|
||||
d.mkdir(parents=True, exist_ok=True)
|
||||
return d
|
||||
|
||||
|
||||
def list_stl_files() -> list[str]:
|
||||
try:
|
||||
return sorted(
|
||||
f.name for f in get_stl_dir().iterdir()
|
||||
if f.suffix.lower() in _SUPPORTED
|
||||
)
|
||||
except FileNotFoundError:
|
||||
return []
|
||||
@@ -2,28 +2,54 @@
|
||||
{% block title %}Jobs — POPS{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h5 class="text-white mb-4">Print Jobs</h5>
|
||||
<div class="d-flex align-items-center justify-content-between mb-4">
|
||||
<h5 class="text-white mb-0">Print Jobs</h5>
|
||||
<button class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#addJobModal">
|
||||
<i class="bi bi-plus-lg me-1"></i>Add Job
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{# ── Search bar ── #}
|
||||
<form method="GET" action="/jobs" class="mb-3">
|
||||
<div class="input-group input-group-sm" style="max-width:360px">
|
||||
<span class="input-group-text bg-dark border-secondary text-secondary"><i class="bi bi-search"></i></span>
|
||||
<input type="text" name="q" value="{{ q }}"
|
||||
class="form-control bg-dark border-secondary text-white"
|
||||
placeholder="Search by customer or filename…">
|
||||
{% if q %}
|
||||
<a href="/jobs" class="btn btn-outline-secondary">×</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
</form>
|
||||
{% if q %}
|
||||
<p class="text-secondary small mb-3">Results for <strong class="text-white">{{ q }}</strong></p>
|
||||
{% endif %}
|
||||
|
||||
<div class="card">
|
||||
{% if jobs %}
|
||||
<table class="table table-dark table-hover mb-0 align-middle">
|
||||
<table class="table table-dark table-hover mb-0 align-middle" style="font-size:.875rem">
|
||||
<thead>
|
||||
<tr class="text-secondary small">
|
||||
<th class="fw-normal">Name</th>
|
||||
<th class="fw-normal">UUID</th>
|
||||
<th class="fw-normal">Customer</th>
|
||||
<th class="fw-normal">File</th>
|
||||
<th class="fw-normal">Printer</th>
|
||||
<th class="fw-normal">Filament</th>
|
||||
<th class="fw-normal">Material</th>
|
||||
<th class="fw-normal">Status</th>
|
||||
<th class="fw-normal">Duration</th>
|
||||
<th class="fw-normal text-end">Cost</th>
|
||||
<th class="fw-normal text-end">Created</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for j in jobs %}
|
||||
<tr>
|
||||
<td class="fw-medium text-white">{{ j.name }}</td>
|
||||
<td class="text-secondary small">{{ j.printer.name if j.printer else '—' }}</td>
|
||||
<td class="text-secondary small">
|
||||
{% if j.filament %}{{ j.filament.material.value }} {{ j.filament.color or '' }}{% else %}—{% endif %}
|
||||
<td class="font-monospace text-secondary" style="font-size:.75rem">
|
||||
{{ j.job_uuid[:8] if j.job_uuid else '—' }}
|
||||
</td>
|
||||
<td class="text-white">{{ j.customer or '—' }}</td>
|
||||
<td class="text-secondary">{{ j.file_name or '—' }}</td>
|
||||
<td class="text-secondary">{{ j.printer.name if j.printer else '—' }}</td>
|
||||
<td class="text-secondary">
|
||||
{% if j.filament %}{{ j.filament.material.value }}{% if j.filament.color %} · {{ j.filament.color }}{% endif %}{% else %}—{% endif %}
|
||||
</td>
|
||||
<td>
|
||||
{% set s = j.status.value %}
|
||||
@@ -34,19 +60,111 @@
|
||||
{% else %}<span class="badge bg-secondary">queued</span>
|
||||
{% endif %}
|
||||
</td>
|
||||
<td class="text-secondary small">
|
||||
{% if j.duration_minutes %}{{ j.duration_minutes }} min{% else %}—{% endif %}
|
||||
</td>
|
||||
<td class="text-secondary small text-end">
|
||||
{% if j.cost_eur %}€ {{ "%.2f"|format(j.cost_eur) }}{% else %}—{% endif %}
|
||||
</td>
|
||||
<td class="text-secondary small text-end">{{ j.created_at.strftime('%d.%m %H:%M') }}</td>
|
||||
<td class="text-secondary text-end">{{ j.created_at.strftime('%d.%m.%Y %H:%M') }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
{% else %}
|
||||
<div class="card-body text-secondary">No jobs yet.</div>
|
||||
<div class="card-body text-secondary">
|
||||
{% if q %}No jobs match "{{ q }}".{% else %}No jobs yet.{% endif %}
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
{# ── Add Job Modal ── #}
|
||||
<div class="modal fade" id="addJobModal" tabindex="-1">
|
||||
<div class="modal-dialog modal-lg">
|
||||
<div class="modal-content bg-dark border-secondary">
|
||||
<form method="POST" action="/jobs" enctype="multipart/form-data">
|
||||
<div class="modal-header border-secondary">
|
||||
<h6 class="modal-title text-white">Add Print Job</h6>
|
||||
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
|
||||
</div>
|
||||
<div class="modal-body d-flex flex-column gap-3">
|
||||
|
||||
<div class="row g-3">
|
||||
<div class="col-md-6">
|
||||
<label class="form-label text-secondary small">Printer <span class="text-danger">*</span></label>
|
||||
<select name="printer_id" class="form-select bg-dark border-secondary text-white" required>
|
||||
<option value="">— select —</option>
|
||||
{% for p in printers %}
|
||||
<option value="{{ p.id }}">{{ p.name }}{% if p.location %} ({{ p.location }}){% endif %}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<label class="form-label text-secondary small">Customer</label>
|
||||
<input type="text" name="customer" class="form-control bg-dark border-secondary text-white"
|
||||
placeholder="Customer name or reference">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{# ── STL file ── #}
|
||||
<div>
|
||||
<label class="form-label text-secondary small">STL / File</label>
|
||||
<ul class="nav nav-pills mb-2" id="stlTabs" role="tablist">
|
||||
<li class="nav-item">
|
||||
<button class="nav-link active py-1 px-3" style="font-size:.8rem"
|
||||
data-bs-toggle="pill" data-bs-target="#uploadPane" type="button">
|
||||
<i class="bi bi-upload me-1"></i>Upload
|
||||
</button>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<button class="nav-link py-1 px-3" style="font-size:.8rem"
|
||||
data-bs-toggle="pill" data-bs-target="#libraryPane" type="button">
|
||||
<i class="bi bi-folder2-open me-1"></i>Library
|
||||
<span class="badge bg-secondary ms-1">{{ stl_files | length }}</span>
|
||||
</button>
|
||||
</li>
|
||||
</ul>
|
||||
<div class="tab-content">
|
||||
<div class="tab-pane fade show active" id="uploadPane">
|
||||
<input type="file" name="stl_file"
|
||||
class="form-control bg-dark border-secondary text-white"
|
||||
accept=".stl,.3mf,.gcode">
|
||||
</div>
|
||||
<div class="tab-pane fade" id="libraryPane">
|
||||
{% if stl_files %}
|
||||
<select name="stl_select" class="form-select bg-dark border-secondary text-white">
|
||||
<option value="">— select file —</option>
|
||||
{% for f in stl_files %}
|
||||
<option value="{{ f }}">{{ f }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
{% else %}
|
||||
<p class="text-secondary small mb-0">No files in library yet. Upload one first.</p>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label class="form-label text-secondary small">Material & Color</label>
|
||||
<select name="filament_id" class="form-select bg-dark border-secondary text-white">
|
||||
<option value="">— none —</option>
|
||||
{% for f in filaments %}
|
||||
<option value="{{ f.id }}">
|
||||
{{ f.material.value }}{% if f.color %} · {{ f.color }}{% endif %}
|
||||
{% if f.brand %} ({{ f.brand }}){% endif %}
|
||||
</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label class="form-label text-secondary small">Notes</label>
|
||||
<textarea name="notes" class="form-control bg-dark border-secondary text-white"
|
||||
rows="2" placeholder="Optional notes"></textarea>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<div class="modal-footer border-secondary">
|
||||
<button type="button" class="btn btn-secondary btn-sm" data-bs-dismiss="modal">Cancel</button>
|
||||
<button type="submit" class="btn btn-primary btn-sm">Create Job</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
Reference in New Issue
Block a user