-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjson_import.py
44 lines (37 loc) · 1.55 KB
/
json_import.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import os, sys
import json
from sqlalchemy.exc import InvalidRequestError
from app import create_app, db
from app.models import Car, Dealer, CarModel
from app.telly_api import repository, vin_generator
def import_file():
config_name = os.getenv('FLASK_CONFIG')
app = create_app(config_name)
app.app_context().push()
with open('test3.json') as json_file:
data = json.load(json_file)
try:
for data_row in data:
vin = data_row['c'][0]['v']
print(f'importing {vin}')
if vin_generator.is_valid_vin(vin):
car_model = data_row['c'][4]['v']
opt_code = data_row['c'][5]['v']
ship_to = data_row['c'][6]['v']
sold_to = data_row['c'][7]['v']
address = data_row['c'][8]['v']
zip_code = data_row['c'][9]['v']
ext_color = data_row['c'][10]['v']
int_color = data_row['c'][11]['v']
created = data_row['c'][12]['v']
model_year = '2021'
car = Car(vin, ext_color, int_color, car_model, opt_code, sold_to, ship_to, model_year)
dealer = Dealer(ship_to, address, zip_code)
repository.create_dealer(dealer)
repository.create_car(car)
except InvalidRequestError as requestError:
print(requestError)
except:
print(f'failed with {sys.exc_info()[0]}')
if __name__ == '__main__':
import_file()