autoformat by black+isort

This commit is contained in:
clonejo 2024-01-14 18:56:43 +01:00
parent 5643dbe030
commit eba9b410c6
3 changed files with 23 additions and 11 deletions

13
crud.py
View file

@ -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()

18
main.py
View file

@ -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")

View file

@ -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"