Drop Sqlmodel and use plain Sqlalchemy

This commit is contained in:
2025-10-11 22:14:04 +02:00
parent a190471b44
commit 494170e2ab
4 changed files with 93 additions and 93 deletions

View File

@@ -1,33 +1,40 @@
import typing
from datetime import datetime
from xmlrpc.client import DateTime
from sqlmodel import Field, Relationship, SQLModel, String
from sqlalchemy import ForeignKey
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
from sqlalchemy.types import Text
class Base(DeclarativeBase):
pass
WorkTypes = typing.Literal["cooking", "dishes", "tables"]
class Event(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
title: str = Field(nullable=False)
event_time: datetime = Field(nullable=False)
registration_deadline: datetime = Field(nullable=False)
description: str
recipe_link: str
class Event(Base):
__tablename__ = "event"
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
title: Mapped[str] = mapped_column(nullable=False)
event_time: Mapped[datetime] = mapped_column(nullable=False)
registration_deadline: Mapped[datetime] = mapped_column(nullable=False)
description: Mapped[str] = mapped_column()
recipe_link: Mapped[str] = mapped_column()
# Min and max number of people needed for cooking, doing the dishes and preparing the tables
team_cooking_min: int = 3
team_cooking_max: int = 5
team_cooking_min: Mapped[int] = mapped_column(default=3, nullable=False)
team_cooking_max: Mapped[int] = mapped_column(default=5, nullable=False)
team_dishes_min: int = 3
team_dishes_max: int = 5
team_dishes_min: Mapped[int] = mapped_column(default=3, nullable=False)
team_dishes_max: Mapped[int] = mapped_column(default=5, nullable=False)
# Todo: Rename to "table"
team_prep_min: int = 1
team_prep_max: int = 1
team_prep_min: Mapped[int] = mapped_column(default=1, nullable=False)
team_prep_max: Mapped[int] = mapped_column(default=1, nullable=False)
registrations: list["Registration"] = Relationship()
team: list["TeamRegistration"] = Relationship()
registrations: Mapped[list["Registration"]] = relationship("Registration")
team: Mapped[list["TeamRegistration"]] = relationship("TeamRegistration")
def team_min_reached(self, work_type: WorkTypes):
threshold = {
@@ -56,52 +63,58 @@ class Event(SQLModel, table=True):
)
class TeamRegistration(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
event_id: int | None = Field(default=None, foreign_key="event.id")
person_name: str = Field(nullable=False)
work_type: WorkTypes = Field(nullable=False, sa_type=String)
comment: str | None
class TeamRegistration(Base):
__tablename__ = "team_registration"
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
event_id: Mapped[int] = mapped_column(ForeignKey("event.id"))
person_name: Mapped[str] = mapped_column(nullable=False)
work_type: Mapped[WorkTypes] = mapped_column(Text, nullable=False)
comment: Mapped[str | None] = mapped_column()
class Household(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
name: str = Field(nullable=False)
class Household(Base):
__tablename__ = "household"
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
name: Mapped[str] = mapped_column(nullable=False)
class Registration(SQLModel, table=True):
event_id: int | None = Field(default=None, foreign_key="event.id", primary_key=True)
household_id: int | None = Field(
default=None, foreign_key="household.id", primary_key=True
class Registration(Base):
__tablename__ = "registration"
event_id: Mapped[int] = mapped_column(ForeignKey("event.id"), primary_key=True)
household_id: Mapped[int] = mapped_column(
ForeignKey("household.id"), primary_key=True
)
num_adult_meals: int
num_children_meals: int
num_small_children_meals: int
comment: str | None
num_adult_meals: Mapped[int] = mapped_column(nullable=False)
num_children_meals: Mapped[int] = mapped_column(nullable=False)
num_small_children_meals: Mapped[int] = mapped_column(nullable=False)
comment: Mapped[str | None] = mapped_column()
household: Household = Relationship()
household: Mapped["Household"] = relationship()
class Subscription(SQLModel, table=True):
household_id: int | None = Field(
default=None, foreign_key="household.id", primary_key=True
class Subscription(Base):
__tablename__ = "subscription"
household_id: Mapped[int] = mapped_column(
ForeignKey("household.id"), primary_key=True
)
num_adult_meals: int
num_children_meals: int
num_small_children_meals: int
comment: str | None
num_adult_meals: Mapped[int] = mapped_column(nullable=False)
num_children_meals: Mapped[int] = mapped_column(nullable=False)
num_small_children_meals: Mapped[int] = mapped_column(nullable=False)
comment: Mapped[str | None] = mapped_column()
last_modified: datetime = Field(default_factory=datetime.now, nullable=False)
last_modified: Mapped[datetime] = mapped_column(
default=datetime.now, nullable=False
)
monday: bool = True
tuesday: bool = True
wednesday: bool = True
thursday: bool = True
friday: bool = True
saturday: bool = True
sunday: bool = True
monday: Mapped[bool] = mapped_column(default=True, nullable=False)
tuesday: Mapped[bool] = mapped_column(default=True, nullable=False)
wednesday: Mapped[bool] = mapped_column(default=True, nullable=False)
thursday: Mapped[bool] = mapped_column(default=True, nullable=False)
friday: Mapped[bool] = mapped_column(default=True, nullable=False)
saturday: Mapped[bool] = mapped_column(default=True, nullable=False)
sunday: Mapped[bool] = mapped_column(default=True, nullable=False)
household: Household = Relationship()
household: Mapped["Household"] = relationship()
def day_string_de(self) -> str:
"""