Scaffold SQLAlchemy models, routers, and Alembic migrations
- app/models/: Printer, Filament, PrintJob, Todo, PricingConfig with full SQLAlchemy 2.x mapped columns and relationships - app/routers/: list + get-by-id endpoints for all five resources - app/db.py: SQLAlchemy engine + SessionLocal + get_db dependency - alembic/: env.py reads MYSQL_* env vars; 0001_initial_schema mirrors db.sql - alembic.ini: script_location configured, URL injected at runtime Since db.sql already created the tables, stamp the DB before running future migrations: alembic stamp head Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
8
app/models/__init__.py
Normal file
8
app/models/__init__.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from app.models.base import Base
|
||||
from app.models.filament import Filament
|
||||
from app.models.pricing import PricingConfig
|
||||
from app.models.print_job import PrintJob
|
||||
from app.models.printer import Printer
|
||||
from app.models.todo import Todo
|
||||
|
||||
__all__ = ["Base", "Printer", "Filament", "PrintJob", "Todo", "PricingConfig"]
|
||||
6
app/models/base.py
Normal file
6
app/models/base.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
def to_dict(self) -> dict:
|
||||
return {c.name: getattr(self, c.name) for c in self.__table__.columns}
|
||||
38
app/models/filament.py
Normal file
38
app/models/filament.py
Normal file
@@ -0,0 +1,38 @@
|
||||
import enum
|
||||
from datetime import datetime
|
||||
from decimal import Decimal
|
||||
|
||||
from sqlalchemy import DateTime, Enum, Numeric, String, func
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.models.base import Base
|
||||
|
||||
|
||||
class Material(enum.Enum):
|
||||
PLA = "PLA"
|
||||
PETG = "PETG"
|
||||
ABS = "ABS"
|
||||
ASA = "ASA"
|
||||
TPU = "TPU"
|
||||
Nylon = "Nylon"
|
||||
PC = "PC"
|
||||
Other = "Other"
|
||||
|
||||
|
||||
class Filament(Base):
|
||||
__tablename__ = "filaments"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
brand: Mapped[str | None] = mapped_column(String(100))
|
||||
material: Mapped[Material] = mapped_column(Enum(Material), default=Material.PLA)
|
||||
color: Mapped[str | None] = mapped_column(String(100))
|
||||
color_hex: Mapped[str | None] = mapped_column(String(7))
|
||||
weight_total_g: Mapped[Decimal] = mapped_column(Numeric(8, 2))
|
||||
weight_remaining_g: Mapped[Decimal] = mapped_column(Numeric(8, 2))
|
||||
price_per_kg_eur: Mapped[Decimal | None] = mapped_column(Numeric(8, 2))
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime, server_default=func.now(), onupdate=func.now()
|
||||
)
|
||||
|
||||
jobs: Mapped[list["PrintJob"]] = relationship(back_populates="filament")
|
||||
20
app/models/pricing.py
Normal file
20
app/models/pricing.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from datetime import date, datetime
|
||||
from decimal import Decimal
|
||||
|
||||
from sqlalchemy import Date, DateTime, Numeric, String, func
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from app.models.base import Base
|
||||
|
||||
|
||||
class PricingConfig(Base):
|
||||
__tablename__ = "pricing_config"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
name: Mapped[str] = mapped_column(String(100), default="default")
|
||||
electricity_kwh_eur: Mapped[Decimal] = mapped_column(Numeric(6, 4), default=Decimal("0.30"))
|
||||
printer_watt: Mapped[Decimal] = mapped_column(Numeric(8, 2), default=Decimal("200.00"))
|
||||
hourly_rate_eur: Mapped[Decimal] = mapped_column(Numeric(8, 2), default=Decimal("0.00"))
|
||||
markup_percent: Mapped[Decimal] = mapped_column(Numeric(5, 2), default=Decimal("20.00"))
|
||||
valid_from: Mapped[date] = mapped_column(Date, server_default=func.current_date())
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
|
||||
41
app/models/print_job.py
Normal file
41
app/models/print_job.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import enum
|
||||
from datetime import datetime
|
||||
from decimal import Decimal
|
||||
|
||||
from sqlalchemy import DateTime, Enum, ForeignKey, Integer, Numeric, String, Text, func
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.models.base import Base
|
||||
|
||||
|
||||
class JobStatus(enum.Enum):
|
||||
queued = "queued"
|
||||
printing = "printing"
|
||||
done = "done"
|
||||
failed = "failed"
|
||||
cancelled = "cancelled"
|
||||
|
||||
|
||||
class PrintJob(Base):
|
||||
__tablename__ = "print_jobs"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
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))
|
||||
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)
|
||||
started_at: Mapped[datetime | None] = mapped_column(DateTime)
|
||||
finished_at: Mapped[datetime | None] = mapped_column(DateTime)
|
||||
duration_minutes: Mapped[int | None] = mapped_column(Integer)
|
||||
filament_used_g: Mapped[Decimal | None] = mapped_column(Numeric(8, 2))
|
||||
cost_eur: Mapped[Decimal | None] = mapped_column(Numeric(8, 2))
|
||||
notes: Mapped[str | None] = mapped_column(Text)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime, server_default=func.now(), onupdate=func.now()
|
||||
)
|
||||
|
||||
printer: Mapped["Printer"] = relationship(back_populates="jobs")
|
||||
filament: Mapped["Filament | None"] = relationship(back_populates="jobs")
|
||||
33
app/models/printer.py
Normal file
33
app/models/printer.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import enum
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import DateTime, Enum, String, func
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.models.base import Base
|
||||
|
||||
|
||||
class PrinterStatus(enum.Enum):
|
||||
idle = "idle"
|
||||
printing = "printing"
|
||||
error = "error"
|
||||
offline = "offline"
|
||||
|
||||
|
||||
class Printer(Base):
|
||||
__tablename__ = "printers"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
name: Mapped[str] = mapped_column(String(100))
|
||||
model: Mapped[str | None] = mapped_column(String(100))
|
||||
location: Mapped[str | None] = mapped_column(String(100))
|
||||
status: Mapped[PrinterStatus] = mapped_column(
|
||||
Enum(PrinterStatus), default=PrinterStatus.offline
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime, server_default=func.now(), onupdate=func.now()
|
||||
)
|
||||
|
||||
jobs: Mapped[list["PrintJob"]] = relationship(back_populates="printer")
|
||||
todos: Mapped[list["Todo"]] = relationship(back_populates="printer")
|
||||
21
app/models/todo.py
Normal file
21
app/models/todo.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import Boolean, DateTime, ForeignKey, String, func
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.models.base import Base
|
||||
|
||||
|
||||
class Todo(Base):
|
||||
__tablename__ = "todos"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
title: Mapped[str] = mapped_column(String(255))
|
||||
done: Mapped[bool] = mapped_column(Boolean, default=False)
|
||||
printer_id: Mapped[int | None] = mapped_column(ForeignKey("printers.id"))
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime, server_default=func.now(), onupdate=func.now()
|
||||
)
|
||||
|
||||
printer: Mapped["Printer | None"] = relationship(back_populates="todos")
|
||||
Reference in New Issue
Block a user