Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] DB 저장 함수 변경 #58

Merged
merged 3 commits into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 0 additions & 32 deletions API/MongoDB.py

This file was deleted.

5 changes: 2 additions & 3 deletions API/candidate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import os, requests, sys
import xml.etree.ElementTree as ET
from typing import List
from .MongoDB import Councilor
import argparse

from configurations.secrets import OpenDataPortalSecrets
from .utils import save_to_excel, getLocalMetroMap
Expand Down Expand Up @@ -41,10 +39,11 @@ def fetch_data(
data_list = []
for item in root.findall(".//item"):
data_entry = {child.tag: child.text for child in item}

for column in drop_columns:
data_entry.pop(column)

data_list.append(Councilor.from_dict(data_entry))
data_list.append(data_entry)

return data_list

Expand Down
19 changes: 11 additions & 8 deletions API/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from . import BASE_DIR, SG_TYPECODE, SG_TYPECODE_TYPE
from configurations.secrets import MongoDBSecrets
from db.client import client
from API.MongoDB import Councilor


def save_to_excel(data: List[dict], sgTypecode: str, is_elected: bool) -> None:
Expand All @@ -31,23 +32,25 @@ def get_local_district_id(sd_name: str, wiw_name: str) -> Optional[int]:


def save_to_mongo(data: List[dict], sgTypecode: str) -> None:
db = client[str(MongoDBSecrets.database_name)]
main_collection = db[str(SG_TYPECODE_TYPE[sgTypecode])]
db = client["council"]
main_collection = db["local_councilor"]

local_metro_map = getLocalMetroMap()

# TODO: Support other types of councils
if sgTypecode == "6":
for entry in data:
district_id = get_local_district_id(entry["sdName"], entry["wiwName"])
entry.pop("sdName")
entry.pop("wiwName")
district_id = local_metro_map[(entry["sdName"], entry["wiwName"])][
"local_id"
]

if district_id:
main_collection.update_one(
{
"council_id": district_id,
"council_type": SG_TYPECODE_TYPE[sgTypecode],
"name": entry["name"],
"localId": district_id,
},
{"$push": {"councilors": entry}},
{"$push": Councilor.from_dict(entry)},
upsert=True,
)
else:
Expand Down