-
Notifications
You must be signed in to change notification settings - Fork 0
/
startup.py
50 lines (30 loc) · 982 Bytes
/
startup.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
45
46
47
48
from locator import app, db, logger
import os, json
from locator.models import *
DEFAULT_DIR = app.config.get('DEFAULT_DIR')
def upload_countries():
filepath = os.path.join(DEFAULT_DIR, 'countries.json')
logger.info(filepath)
_file = open(filepath)
data = json.loads(_file.read().encode("UTF-8"))
for item in data:
country = Country(name=item.get('name'), code=item.get('code'))
db.session.add(country)
try:
db.session.commit()
logger.info(country)
except:
db.session.rollback()
def upload_states(name):
country = Country.query.filter(func.lower(Country.name)==name.lower()).first()
filepath = os.path.join(DEFAULT_DIR, 'states/%s.json'%country.name.lower())
logger.info(filepath)
_file = open(filepath)
data = json.loads(_file.read().encode("UTF-8"))
for key, value in data.items():
state = State(name=value, code=key, country_id=country.id)
db.session.add(state)
try:
db.session.commit()
except:
db.session.rollback()