From 366a6de93ca5f3cd2abbe60af7d6fd2e9349dfb8 Mon Sep 17 00:00:00 2001 From: pingpingy1 Date: Fri, 1 Dec 2023 12:42:59 +0900 Subject: [PATCH] =?UTF-8?q?[fix]=20=EA=B8=B0=EC=B4=88=EC=9D=98=ED=9A=8C=20?= =?UTF-8?q?divArea=20=EB=A1=9C=EC=A7=81=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routers/scrapResultLocal.py | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/routers/scrapResultLocal.py b/routers/scrapResultLocal.py index 1252365..2b0d1c6 100644 --- a/routers/scrapResultLocal.py +++ b/routers/scrapResultLocal.py @@ -270,20 +270,26 @@ async def getLocalTemplateData( } ) - divArea_id = ( - await client.stats_db["diversity_index"].find_one( - {"localId": {"$exists": True}, "ageDiversityRank": 1} - ) - )["localId"] - divArea = await client.stats_db["age_stat"].find_one( - { - "level": 2, - "councilorType": "local_councilor", - "is_elected": True, - "localId": divArea_id, - "year": most_recent_year, - } + areas_sorted = ( + client.stats_db["diversity_index"] + .find({"localId": {"$exists": True}}) + .sort("ageDiversityRank") ) + async for area in areas_sorted: + divArea = await client.stats_db["age_stat"].find_one( + { + "level": 2, + "councilorType": "local_councilor", + "is_elected": True, + "localId": area["localId"], + "year": most_recent_year, + } + ) + if divArea is not None: + break + + if divArea is None: + return NO_DATA_ERROR_RESPONSE uniArea_id = ( await client.stats_db["diversity_index"].find_one( @@ -348,7 +354,7 @@ async def getLocalTemplateData( "firstQuintile": age_stat_elected["data"][0]["firstquintile"], "lastQuintile": age_stat_elected["data"][0]["lastquintile"], "divArea": { - "localId": divArea_id, + "localId": divArea["localId"], "firstQuintile": divArea["data"][0]["firstquintile"], "lastQuintile": divArea["data"][0]["lastquintile"], },