From 37293f964a217944afe0c5386f7de624d41b8abb Mon Sep 17 00:00:00 2001 From: cyfraeviolae Date: Wed, 3 Apr 2024 03:02:22 -0400 Subject: more --- app.py | 58 +++++++++++++++++++++++++++++++--------------------- templates/event.html | 6 +++++- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/app.py b/app.py index cffe64c..b5b67a0 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,5 @@ from pathlib import Path +import json import hmac from typing import Annotated import secrets @@ -27,11 +28,8 @@ from litestar.params import Body from litestar import Request from litestar.datastructures import State -# link location to gmaps # automake calendar invite ics -# persistence -# use url_fors -# timezones? +# use url_fors, timezones? class Base(DeclarativeBase): pass @@ -56,6 +54,9 @@ class Event(Base): description: Mapped[str] invites: Mapped[str] + def get_invites(self): + return json.loads(self.invites) + @asynccontextmanager async def db_connection(app: Litestar) -> AsyncGenerator[None, None]: engine = getattr(app.state, "engine", None) @@ -131,19 +132,23 @@ async def create(state: State, data: Annotated[EditRequest, Body(media_type=Requ return Redirect(path="/symposium/event/" + iden + "?password=" + event.password) @post("/event/{iden:str}/edit") -async def edit(request: Request, iden: str, password: str, data: Annotated[EditRequest, Body(media_type=RequestEncodingType.URL_ENCODED)]) -> Redirect: #-> Template: - event = EVENTS[iden] +async def edit(state: State, request: Request, iden: str, password: str, data: Annotated[EditRequest, Body(media_type=RequestEncodingType.URL_ENCODED)]) -> Redirect: #-> Template: + async with sessionmaker(bind=state.engine) as session: + async with session.begin(): + query = select(Event).where(Event.iden == iden) + result = await session.execute(query) + event = result.scalar_one() - manage = False - if password and hmac.compare_digest(event.password, password): - manage = True - if not manage: - raise ValueError("no auth") + manage = False + if password and hmac.compare_digest(event.password, password): + manage = True + if not manage: + raise ValueError("no auth") - event.title = data.title - event.time = data.when - event.location = data.where - event.description = data.what + event.title = data.title + event.time = datetime.datetime.fromisoformat(data.when) + event.location = data.where + event.description = data.what return Redirect(path="/symposium/event/" + iden + "?password=" + event.password) @@ -152,14 +157,21 @@ class JoinRequest: name: str @post("/event/{iden:str}/join") -async def join(request: Request, iden: str, data: Annotated[JoinRequest, Body(media_type=RequestEncodingType.URL_ENCODED)]) -> Redirect: #-> Template: - event = EVENTS[iden] - name = data.name - # TODO if name exists reject - invites = json.loads(event.invites) - invites.append(name) - event.invites = json.dumps(invites) - return Redirect(path="/symposium/event/" + iden) +async def join(state: State, request: Request, iden: str, data: Annotated[JoinRequest, Body(media_type=RequestEncodingType.URL_ENCODED)], password: str = "") -> Redirect: #-> Template: + async with sessionmaker(bind=state.engine) as session: + async with session.begin(): + query = select(Event).where(Event.iden == iden) + result = await session.execute(query) + event = result.scalar_one() + name = data.name + # TODO if name exists reject + invites = json.loads(event.invites) + invites.append(name) + event.invites = json.dumps(invites) + url = "/symposium/event/" + iden + if password: + url += "?password=" + password + return Redirect(path=url) app = Litestar( route_handlers=[ diff --git a/templates/event.html b/templates/event.html index 2440f42..207ac87 100644 --- a/templates/event.html +++ b/templates/event.html @@ -83,11 +83,15 @@ Who?
- {% for name in event.invites %} + {% for name in event.get_invites() %} — {{ name }}
{% endfor %} + {% if manage %} +
+ {% else %} + {% endif %}
-- cgit v1.2.3