diff --git a/new-registration-app/main.py b/new-registration-app/main.py index b164275..af5d904 100644 --- a/new-registration-app/main.py +++ b/new-registration-app/main.py @@ -10,7 +10,8 @@ from fastapi.staticfiles import StaticFiles from fastapi.templating import Jinja2Templates from sqlmodel import Session, SQLModel, create_engine, select -from models import Event, Household, Registration, TeamRegistration +from models import (Event, Household, Registration, Subscription, + TeamRegistration) sqlite_file_name = "database.db" sqlite_url = f"sqlite:///{sqlite_file_name}" @@ -79,6 +80,75 @@ async def past_events(request: Request, session: SessionDep): ) +@app.get("/subscribe") +async def subscribe(request: Request, session: SessionDep): + statement = select(Household) + households = session.exec(statement).all() + + # filter out households with existing registrations + # households = [ + # h + # for h in households + # if h.id not in [reg.household_id for reg in event.registrations] + # ] + + subscriptions = session.exec(select(Subscription)).all() + + return templates.TemplateResponse( + request=request, + name="subscribe.html", + context={"households": households, "subscriptions": subscriptions}, + ) + + +@app.post("/subscribe") +async def add_subscribe(request: Request, session: SessionDep): + form_data = await request.form() + # TODO: Make this return a nicer error message + try: + num_adult_meals = int(form_data["numAdults"]) if form_data["numAdults"] else 0 + num_children_meals = int(form_data["numKids"]) if form_data["numKids"] else 0 + num_small_children_meals = ( + int(form_data["numSmallKids"]) if form_data["numSmallKids"] else 0 + ) + except ValueError: + raise ValueError("All number fields must be integers") + + subscription = Subscription( + household_id=form_data["household"], + num_adult_meals=num_adult_meals, + num_children_meals=num_children_meals, + num_small_children_meals=num_small_children_meals, + ) + + selected_days = form_data.getlist("days") + if selected_days: + subscription.monday = "1" in selected_days + subscription.tuesday = "2" in selected_days + subscription.wednesday = "3" in selected_days + subscription.thursday = "4" in selected_days + subscription.friday = "5" in selected_days + subscription.saturday = "6" in selected_days + subscription.sunday = "7" in selected_days + + session.add(subscription) + session.commit() + + return RedirectResponse(url="/subscribe", status_code=status.HTTP_302_FOUND) + + +@app.get("/subscribe/{household_id}/delete") +async def delete_subscription(request: Request, session: SessionDep, household_id: int): + + statement = select(Subscription).where(Subscription.household_id == household_id) + sub = session.exec(statement).one() + + session.delete(sub) + session.commit() + + return RedirectResponse(url="/subscribe", status_code=status.HTTP_302_FOUND) + + @app.get("/event/add") async def add_event_form(request: Request, session: SessionDep): return templates.TemplateResponse(request=request, name="add_event.html") diff --git a/new-registration-app/models.py b/new-registration-app/models.py index 8efc803..019f2be 100644 --- a/new-registration-app/models.py +++ b/new-registration-app/models.py @@ -1,5 +1,6 @@ import typing from datetime import datetime +from xmlrpc.client import DateTime from sqlmodel import Field, Relationship, SQLModel, String @@ -79,3 +80,51 @@ class Registration(SQLModel, table=True): comment: str | None household: Household = Relationship() + + +class Subscription(SQLModel, table=True): + household_id: int | None = Field( + default=None, foreign_key="household.id", primary_key=True + ) + num_adult_meals: int + num_children_meals: int + num_small_children_meals: int + comment: str | None + + last_modified: datetime = Field(default_factory=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 + + household: Household = Relationship() + + def day_string_de(self) -> str: + """ + Generates a string representation of selected days in German short form. + """ + result = [] + + if self.monday: + result.append("Mo") + if self.tuesday: + result.append("Di") + if self.wednesday: + result.append("Mi") + if self.thursday: + result.append("Do") + if self.friday: + result.append("Fr") + if self.saturday: + result.append("Sa") + if self.sunday: + result.append("So") + + if len(result) < 7: + return ", ".join(result) + else: + return "Alle" diff --git a/new-registration-app/templates/index.html b/new-registration-app/templates/index.html index f20e746..7f67619 100644 --- a/new-registration-app/templates/index.html +++ b/new-registration-app/templates/index.html @@ -8,7 +8,23 @@ +
+
+
+ +
Nie wieder die Anmeldung vergessen
+

+ Die Dauerhafte Anmeldung gilt für alle kommenden Kochabende. +

+ + Jetzt dauerhaft Anmelden + +
+
+
+ + {% for event in events %}
diff --git a/new-registration-app/templates/subscribe.html b/new-registration-app/templates/subscribe.html new file mode 100644 index 0000000..0dbe32a --- /dev/null +++ b/new-registration-app/templates/subscribe.html @@ -0,0 +1,134 @@ +{% extends "base.html" %} + +{% block content %} +
+ +
+
+
+
+
+

Dauerhafte Anmeldung zu allen Kochabenden

+
+ +
+
+

+ Mit einer dauerhaften Anmeldung kannst du dich/euch für alle zukünftigen Kochabende + anmelden. Es ist möglich + diese Anmeldung auf bestimmte Wochentage zu beschränken. +

+

+ Dauerhafte Anmeldungen werden eine Woche vor einem Kochabend als Anmeldungen für diesen + Abend eingetragen. Danach + können sie auch noch gelöscht bzw. bearbeitet werden. +

+ +
+ +
+

+ Wenn dein Haushalt hier nicht auswählbar ist, besteht bereits eine dauerhafte Anmeldung. + Um Änderungen vorzunehmen, lösche die bestehende Anmeldung und lege eine neue an. +

+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +

+ Wenn du nur an bestimmten Tagen teilnehmen möchtest, wähle sie hier aus. +

+
+ {% set days = ["Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", + "Sonntag"] %} + {% for day in days %} +
+ + +
+ {% endfor %} +
+
+
+ + + +
+
+
+
+
+ + +
+

Bestehende dauerhafte Anmeldungen

+ {% if subscriptions | length == 0 %} +

Es gibt noch keine dauerhaften Anmeldungen

+ {% else %} + {% for sub in subscriptions %} +
+
+
+
{{ sub.household.name }}
+ + + +
+
+
+
Erwachsene
+
{{ sub.num_adult_meals }}
+
+
+
Kinder
+
{{ sub.num_children_meals }}
+
+
+
Kleinkinder
+
{{ sub.num_small_children_meals }}
+
+
+
Tage
+
{{ sub.day_string_de() }}
+
+
+
+
+ {% endfor %} +
+ {% endif %} +
+
+ + +{% endblock %}