summaryrefslogtreecommitdiff
path: root/app.py
diff options
context:
space:
mode:
authorcyfraeviolae <cyfraeviolae>2024-04-03 03:02:22 -0400
committercyfraeviolae <cyfraeviolae>2024-04-03 03:02:22 -0400
commit37293f964a217944afe0c5386f7de624d41b8abb (patch)
treec777d3c39e45406647513653e005f37b754b8dfd /app.py
parent5b620cacf2cdbfd1f720ffe68affe9d429031d8a (diff)
more
Diffstat (limited to 'app.py')
-rw-r--r--app.py58
1 files changed, 35 insertions, 23 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=[