1
1
import csv
2
- import json
3
2
from tempfile import NamedTemporaryFile
4
3
5
4
import click
@@ -36,7 +35,7 @@ def init_db_command():
36
35
37
36
def import_users ():
38
37
"""
39
- Imports user data to database, from either a local or remote JSON or CSV file,
38
+ Imports user data to database, from either a local or remote CSV file,
40
39
given the `IMPORT_FILE_PATH` setting.
41
40
CSV files take extra settings: `CSV_DELIMITER`, `CSV_QUOTING`, `CSV_QUOTECHAR`
42
41
"""
@@ -47,36 +46,17 @@ def import_users():
47
46
format = path .split ("." )[- 1 ].lower ()
48
47
remote = path .lower ().startswith ("http" )
49
48
50
- if format not in ["json" , " csv" ]:
49
+ if format not in ["csv" ]:
51
50
click .warning (f"File format is not supported: { format } " )
52
51
return
53
52
54
- if format == "json" :
55
- import_json_users (path , remote )
56
53
elif format == "csv" :
57
54
import_csv_users (path , remote )
58
55
59
56
click .echo (f"Users added: { User .query .count ()} " )
60
57
click .echo (f"Eligibility types added: { Eligibility .query .count ()} " )
61
58
62
59
63
- def import_json_users (json_path , remote ):
64
- data = {}
65
- if remote :
66
- # download the file to a dict
67
- data = requests .get (json_path , timeout = config .request_timeout ).json ()
68
- else :
69
- # open the file and load to a dict
70
- with open (json_path ) as file :
71
- data = json .load (file )
72
- if "users" in data :
73
- data = data ["users" ]
74
- # unpack from the key/value pairs in data
75
- # sub = [name, types]
76
- for sub , (name , types ) in data .items ():
77
- save_user (sub , name , types )
78
-
79
-
80
60
def import_csv_users (csv_path , remote ):
81
61
# placeholder for a temp file that remote is downloaded to
82
62
temp_csv = None
0 commit comments