Skip to content

Commit

Permalink
Merge pull request #58 from pingpingy1/main
Browse files Browse the repository at this point in the history
[feat] DB 저장 함수 변경
  • Loading branch information
Re-st authored Nov 19, 2023
2 parents 8a0afc5 + ea54b7d commit ddac8f0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 43 deletions.
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

0 comments on commit ddac8f0

Please sign in to comment.