from fastapi import FastAPI # use database residing here DB_LOCATION = ( "../testbox/photovoter.dblite" # Q: any allowances for this being not OUR database? ) app = FastAPI() con = sqlite3.connect(DB_LOCATION) con.row_factory = sqlite3.Row cur = con.cursor() @app.get("/new_session") async def new_session(): """Start a new session""" # add session to the database # return new session cookie return {"session_id": 42} @app.get("/next_picture/{cookie}") async def next_picture(cookie: int): """Request new picture to rate.""" # check if the cookie is valid cur.execute( """SELECT sessionid FROM sessions WHERE cookie = :cookie LIMIT 1""", {"cookie": cookie}, ) sessionid = cur.fetchone() if sessionid is None: return # take not rated picture from the database # do not insert anything in the database yet # return this picture # SELECT all images EXCEPT images with marks from the current session -> # -> SELECT paths for these images # FIXME[0]: can this be done better? cur.execute( """SELECT imgid, resizedpath FROM images WHERE imgid IN (SELECT imgid FROM images EXCEPT SELECT imgid FROM marks WHERE sessionid = :sessionid) LIMIT 1 """, {"sessionid": sessionid["sessionid"]}, ) r = cur.fetchone() return {"picture_id": r["imgid"]} @app.get("/rate_picture/{session_id}/{picture_id}/{mark}") async def rate_picture(session_id: int, picture_id: int, mark: int): """Submit a rating for the picture""" # check if session is valid # add new mark to the session table pass