mirror of
https://gitlab.aachen.ccc.de/inventory/in.git
synced 2024-11-29 10:13:59 +01:00
autoformat by black+isort
This commit is contained in:
parent
5643dbe030
commit
eba9b410c6
3 changed files with 23 additions and 11 deletions
13
crud.py
13
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]:
|
def get_items(db: Session) -> list[models.Item]:
|
||||||
return db.query(models.Item).all()
|
return db.query(models.Item).all()
|
||||||
|
|
||||||
|
|
||||||
def put_item(db: Session, id: str, item: schemas.Item):
|
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:
|
if not updated:
|
||||||
db.add(models.Item(**item.dict()))
|
db.add(models.Item(**item.dict()))
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
18
main.py
18
main.py
|
@ -1,9 +1,10 @@
|
||||||
|
import re
|
||||||
|
|
||||||
from fastapi import Depends, FastAPI, Response
|
from fastapi import Depends, FastAPI, Response
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
from sqlalchemy import event
|
from sqlalchemy import event
|
||||||
from sqlalchemy.orm import Session
|
|
||||||
from sqlalchemy.engine import Engine
|
from sqlalchemy.engine import Engine
|
||||||
import re
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
import crud
|
import crud
|
||||||
from database import SessionLocal, engine
|
from database import SessionLocal, engine
|
||||||
|
@ -14,6 +15,7 @@ Base.metadata.create_all(bind=engine)
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
def get_db():
|
def get_db():
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
try:
|
try:
|
||||||
|
@ -21,6 +23,7 @@ def get_db():
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
|
|
||||||
@event.listens_for(Engine, "connect")
|
@event.listens_for(Engine, "connect")
|
||||||
def _set_sqlite_pragma(conn, _):
|
def _set_sqlite_pragma(conn, _):
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
@ -31,21 +34,24 @@ def _set_sqlite_pragma(conn, _):
|
||||||
@app.get("/api/items", response_model=dict[str, Item])
|
@app.get("/api/items", response_model=dict[str, Item])
|
||||||
async def list_items(db: Session = Depends(get_db)):
|
async def list_items(db: Session = Depends(get_db)):
|
||||||
# natural sort by id
|
# 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 = crud.get_items(db)
|
||||||
items = sorted(items, key=natsort)
|
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}")
|
@app.put("/api/items/{id}")
|
||||||
async def put_item(id: str, item: Item, db: Session = Depends(get_db)):
|
async def put_item(id: str, item: Item, db: Session = Depends(get_db)):
|
||||||
if crud.put_item(db, id, item):
|
if crud.put_item(db, id, item):
|
||||||
return Response(b'', status_code=204)
|
return Response(b"", status_code=204)
|
||||||
return Response(b'', status_code=201)
|
return Response(b"", status_code=201)
|
||||||
|
|
||||||
|
|
||||||
@app.delete("/api/items/{id}", status_code=204)
|
@app.delete("/api/items/{id}", status_code=204)
|
||||||
async def delete_item(id: str, db: Session = Depends(get_db)):
|
async def delete_item(id: str, db: Session = Depends(get_db)):
|
||||||
crud.delete_item(db, id)
|
crud.delete_item(db, id)
|
||||||
|
|
||||||
|
|
||||||
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
from sqlalchemy import Column, ForeignKey, String, Text, Boolean
|
from sqlalchemy import Boolean, Column, ForeignKey, String, Text
|
||||||
|
|
||||||
from database import Base
|
from database import Base
|
||||||
|
|
||||||
|
|
||||||
class Item(Base):
|
class Item(Base):
|
||||||
__tablename__ = "items"
|
__tablename__ = "items"
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue