From eba9b410c6da6ad5ba83197b58144c77cbc300a8 Mon Sep 17 00:00:00 2001 From: clonejo Date: Sun, 14 Jan 2024 18:56:43 +0100 Subject: [PATCH] autoformat by black+isort --- crud.py | 13 +++++++++---- main.py | 18 ++++++++++++------ models.py | 3 ++- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/crud.py b/crud.py index f5e1f39..6f453f2 100644 --- a/crud.py +++ b/crud.py @@ -1,14 +1,19 @@ -from sqlalchemy.orm import Session -from sqlalchemy import DECIMAL, cast -import models, schemas +from sqlalchemy import DECIMAL, cast +from sqlalchemy.orm import Session + +import models +import schemas + def get_items(db: Session) -> list[models.Item]: return db.query(models.Item).all() def put_item(db: Session, id: str, item: schemas.Item): - updated = bool(db.query(models.Item).filter(models.Item.id == id).update(item.dict())) + updated = bool( + db.query(models.Item).filter(models.Item.id == id).update(item.dict()) + ) if not updated: db.add(models.Item(**item.dict())) db.commit() diff --git a/main.py b/main.py index ecaeea6..dbc0409 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,10 @@ +import re + from fastapi import Depends, FastAPI, Response from fastapi.staticfiles import StaticFiles from sqlalchemy import event -from sqlalchemy.orm import Session from sqlalchemy.engine import Engine -import re +from sqlalchemy.orm import Session import crud from database import SessionLocal, engine @@ -14,6 +15,7 @@ Base.metadata.create_all(bind=engine) app = FastAPI() + def get_db(): db = SessionLocal() try: @@ -21,6 +23,7 @@ def get_db(): finally: db.close() + @event.listens_for(Engine, "connect") def _set_sqlite_pragma(conn, _): cursor = conn.cursor() @@ -31,21 +34,24 @@ def _set_sqlite_pragma(conn, _): @app.get("/api/items", response_model=dict[str, Item]) async def list_items(db: Session = Depends(get_db)): # natural sort by id - natsort = lambda item: [int(t) if t.isdigit() else t.lower() for t in re.split('(\d+)', item.id)] + natsort = lambda item: [ + int(t) if t.isdigit() else t.lower() for t in re.split("(\d+)", item.id) + ] items = crud.get_items(db) items = sorted(items, key=natsort) - return {i.id:i for i in items} + return {i.id: i for i in items} @app.put("/api/items/{id}") async def put_item(id: str, item: Item, db: Session = Depends(get_db)): if crud.put_item(db, id, item): - return Response(b'', status_code=204) - return Response(b'', status_code=201) + return Response(b"", status_code=204) + return Response(b"", status_code=201) @app.delete("/api/items/{id}", status_code=204) async def delete_item(id: str, db: Session = Depends(get_db)): crud.delete_item(db, id) + app.mount("/", StaticFiles(directory="static", html=True), name="static") diff --git a/models.py b/models.py index d7618a6..7aa9b21 100644 --- a/models.py +++ b/models.py @@ -1,7 +1,8 @@ -from sqlalchemy import Column, ForeignKey, String, Text, Boolean +from sqlalchemy import Boolean, Column, ForeignKey, String, Text from database import Base + class Item(Base): __tablename__ = "items"