Skip to content

Commit

Permalink
feat(json): remove json import code/comments
Browse files Browse the repository at this point in the history
  • Loading branch information
machikoyasuda committed Dec 6, 2023
1 parent bf708c2 commit 2f13ce2
Showing 1 changed file with 2 additions and 22 deletions.
24 changes: 2 additions & 22 deletions eligibility_server/db/setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import csv
import json
from tempfile import NamedTemporaryFile

import click
Expand Down Expand Up @@ -36,7 +35,7 @@ def init_db_command():

def import_users():
"""
Imports user data to database, from either a local or remote JSON or CSV file,
Imports user data to database, from either a local or remote CSV file,
given the `IMPORT_FILE_PATH` setting.
CSV files take extra settings: `CSV_DELIMITER`, `CSV_QUOTING`, `CSV_QUOTECHAR`
"""
Expand All @@ -47,36 +46,17 @@ def import_users():
format = path.split(".")[-1].lower()
remote = path.lower().startswith("http")

if format not in ["json", "csv"]:
if format not in ["csv"]:
click.warning(f"File format is not supported: {format}")
return

if format == "json":
import_json_users(path, remote)
elif format == "csv":
import_csv_users(path, remote)

click.echo(f"Users added: {User.query.count()}")
click.echo(f"Eligibility types added: {Eligibility.query.count()}")


def import_json_users(json_path, remote):
data = {}
if remote:
# download the file to a dict
data = requests.get(json_path, timeout=config.request_timeout).json()
else:
# open the file and load to a dict
with open(json_path) as file:
data = json.load(file)
if "users" in data:
data = data["users"]
# unpack from the key/value pairs in data
# sub = [name, types]
for sub, (name, types) in data.items():
save_user(sub, name, types)


def import_csv_users(csv_path, remote):
# placeholder for a temp file that remote is downloaded to
temp_csv = None
Expand Down

0 comments on commit 2f13ce2

Please sign in to comment.