From 2daae1f37e5bf983b896f4231c0bfc521b9b68df Mon Sep 17 00:00:00 2001 From: rrr-marble Date: Thu, 23 Sep 2021 01:24:53 +0300 Subject: [PATCH] add: impl photo_points() --- main.py | 52 +++++++++++++++++++++++----------------------------- 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/main.py b/main.py index d687b51..80cccb0 100644 --- a/main.py +++ b/main.py @@ -17,7 +17,7 @@ con = sqlite3.connect(DB_LOCATION) con.row_factory = sqlite3.Row cur = con.cursor() # NB! single is enough for now, we might require multiple later -origins = [ # CORS +origins = [ # CORS "*", ] @@ -145,36 +145,30 @@ async def rate_picture(cookie: str, picture_id: int, mark: int): return {"status": "success"} + @app.get("/photo_points") -def photo_points(): +async def photo_points(): """Get points with the url of a photo and the rate""" + # assume we always have at least some photos + # fetch them all + cur.execute( + """SELECT images.imgid, resizedpath, GPSLatitude, GPSLongitude, + 100*SUM(marks.mark)/COUNT(marks.mark)/MAX(marks.mark) + FROM images + LEFT JOIN marks ON images.imgid = marks.imgid + GROUP BY images.imgid; + """, # 100 * SUM(marks.mark)/COUNT(marks.mark)/MAX(marks.mark) + # is an ad-hoc percentage of likes without know how front end defined like/dislike + # returns None with no marks (sqlite handles division by 0 gracefully) + ) + points = cur.fetchall() return [ { - "id": 0, - "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c5/JPEG_example_down.jpg/350px-JPEG_example_down.jpg", - "lon": 37.34542, - "lat": 55.12323, - "rate": 36 # percentage of likes - }, - { - "id": 1, - "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c5/JPEG_example_down.jpg/350px-JPEG_example_down.jpg", - "lon": 37.34342, - "lat": 55.12423, - "rate": 62 # percentage of likes - }, - { - "id": 2, - "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c5/JPEG_example_down.jpg/350px-JPEG_example_down.jpg", - "lon": 37.34642, - "lat": 55.12223, - "rate": 43 # percentage of likes - }, - { - "id": 3, - "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c5/JPEG_example_down.jpg/350px-JPEG_example_down.jpg", - "lon": 37.34342, - "lat": 55.12923, - "rate": 90 # percentage of likes + "id": point["imgid"], + "url": point["resizedpath"], + "lon": point["GPSLongitude"], + "lat": point["GPSLatitude"], + "rate": point["100*SUM(marks.mark)/COUNT(marks.mark)/MAX(marks.mark)"], } - ] \ No newline at end of file + for point in points + ]