in-c4/main.py

67 lines
1.7 KiB
Python
Raw Normal View History

2024-01-14 18:56:43 +01:00
import re
from calendar import timegm
from datetime import date
2024-01-14 18:56:43 +01:00
2022-08-04 05:41:51 +02:00
from fastapi import Depends, FastAPI, Response
from fastapi.staticfiles import StaticFiles
from sqlalchemy import event
from sqlalchemy.engine import Engine
2024-01-14 18:56:43 +01:00
from sqlalchemy.orm import Session
2022-08-04 05:41:51 +02:00
import crud
from database import SessionLocal, engine
from models import Base
from schemas import Item
Base.metadata.create_all(bind=engine)
app = FastAPI()
2024-01-14 18:56:43 +01:00
2022-08-04 05:41:51 +02:00
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
2024-01-14 18:56:43 +01:00
2022-08-04 05:41:51 +02:00
@event.listens_for(Engine, "connect")
def _set_sqlite_pragma(conn, _):
cursor = conn.cursor()
cursor.execute("PRAGMA foreign_keys=ON;")
cursor.close()
@app.get("/api/items", response_model=dict[str, Item])
async def list_items(db: Session = Depends(get_db)):
2022-08-11 03:40:26 +02:00
# natural sort by id
2024-01-14 18:56:43 +01:00
natsort = lambda item: [
int(t) if t.isdigit() else t.lower() for t in re.split("(\d+)", item.id)
]
2022-08-04 05:41:51 +02:00
items = crud.get_items(db)
2022-08-06 02:38:47 +02:00
items = sorted(items, key=natsort)
2024-01-14 18:56:43 +01:00
return {i.id: i for i in items}
2022-08-04 05:41:51 +02:00
@app.put("/api/items/{id}")
async def put_item(id: str, item: Item, db: Session = Depends(get_db)):
if item.last_updated is None:
item.last_updated = month_timestamp()
if crud.put_item(db, id, item) == crud.PutItemResult.UPDATED:
2024-01-14 18:56:43 +01:00
return Response(b"", status_code=204)
return Response(b"", status_code=201)
2022-08-04 05:41:51 +02:00
@app.delete("/api/items/{id}", status_code=204)
async def delete_item(id: str, db: Session = Depends(get_db)):
crud.delete_item(db, id)
2024-01-14 18:56:43 +01:00
def month_timestamp() -> int:
"""Provides the timestamp of the current month's beginning (for improved privacy)"""
return timegm(date.today().replace(day=1).timetuple())
2022-08-04 05:41:51 +02:00
app.mount("/", StaticFiles(directory="static", html=True), name="static")