mirror of
https://gitlab.aachen.ccc.de/inventory/in.git
synced 2024-11-25 16:53:59 +01:00
fix natural sorting
This commit is contained in:
parent
c2133771eb
commit
93d6063c52
2 changed files with 5 additions and 1 deletions
2
crud.py
2
crud.py
|
@ -4,7 +4,7 @@ from sqlalchemy import DECIMAL, cast
|
|||
import models, schemas
|
||||
|
||||
def get_items(db: Session) -> list[models.Item]:
|
||||
return db.query(models.Item).order_by(models.Item.type, cast(models.Item.id, DECIMAL))
|
||||
return db.query(models.Item).all()
|
||||
|
||||
|
||||
def put_item(db: Session, id: str, item: schemas.Item):
|
||||
|
|
4
main.py
4
main.py
|
@ -3,6 +3,7 @@ from fastapi.staticfiles import StaticFiles
|
|||
from sqlalchemy import event
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy.engine import Engine
|
||||
import re
|
||||
|
||||
import crud
|
||||
from database import SessionLocal, engine
|
||||
|
@ -29,7 +30,10 @@ def _set_sqlite_pragma(conn, _):
|
|||
|
||||
@app.get("/api/items", response_model=dict[str, Item])
|
||||
async def list_items(db: Session = Depends(get_db)):
|
||||
# sort by type, id (natural)
|
||||
natsort = lambda s: (s.type, [int(t) if t.isdigit() else t.lower() for t in re.split('(\d+)', s.id)])
|
||||
items = crud.get_items(db)
|
||||
items = sorted(items, key=natsort)
|
||||
return {i.id:i for i in items}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue