From 1294d1a0377baeac7a785c786851fadcdc7fed9b Mon Sep 17 00:00:00 2001 From: profitroll Date: Mon, 23 Jan 2023 16:39:11 +0100 Subject: [PATCH] Changed only_ids behavior + sorting added --- extensions/saves.py | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/extensions/saves.py b/extensions/saves.py index 0e629d7..abbf73b 100644 --- a/extensions/saves.py +++ b/extensions/saves.py @@ -3,6 +3,7 @@ from urllib.parse import quote_plus from os import remove from typing import Dict, List, Literal, Union from xmltodict import parse +from pymongo import DESCENDING from models.saves import StardewSave, StardewSaveBrief from modules.app import app, get_api_key, user_by_key from modules.database import col_devices, col_saves @@ -27,29 +28,25 @@ async def saves_get(device: Union[str, None] = None, version: Union[str, None] = if version is not None: query["data.game_version"] = version - saves_entries = list(col_saves.find(query)) + saves_entries = list(col_saves.find(query).sort("date", DESCENDING)) if len(saves_entries) == 0: raise HTTPException(HTTP_404_NOT_FOUND, detail="Could not find any saves.") output = [] + added = [] - if only_ids is True: - for entry in saves_entries: - if entry["id"] not in output: - output.append( - { - "id": entry["id"], - "farmer": entry["data"]["farmer"] - } - ) - else: - for entry in saves_entries: - out_entry = entry - del out_entry["_id"] - del out_entry["user"] - del out_entry["file"] - output.append(out_entry) + for entry in saves_entries: + out_entry = entry + del out_entry["_id"] + del out_entry["user"] + del out_entry["file"] + if only_ids is True: + if entry["id"] in added: + continue + else: + added.append(entry["id"]) + output.append(out_entry) return UJSONResponse(output) @@ -65,7 +62,7 @@ async def saves_get_by_id(id: int, device: Union[str, None] = None, version: Uni if version is not None: query["data.game_version"] = version - saves_entries = list(col_saves.find(query)) + saves_entries = list(col_saves.find(query).sort("date", DESCENDING)) if len(saves_entries) == 0: raise HTTPException(HTTP_404_NOT_FOUND, detail="Could not find save with such id.")