Skip to content
Snippets Groups Projects
Commit 6c44bb1d authored by Florent Chehab's avatar Florent Chehab
Browse files

Insertion through API

parent a2172e2e
No related branches found
No related tags found
No related merge requests found
......@@ -6,9 +6,30 @@ Script to insert the country data in the database
IT HAS TO BE RUN INSIDE ./manage.py shell
"""
import os
import argparse
import pandas as pd
import requests
from rex.models.country import Country, Region
if __name__ != "__main__":
print(__name__)
raise Exception("Script has to be run directly")
# getting args from command line
parser = argparse.ArgumentParser(
description='Country and Region Insertion')
parser.add_argument('--address', '-a', dest='address',
action='store', default='http://127.0.0.1:8000/api',
help='Adresse of the web REST API')
parser.add_argument('--token', '-t', dest='token',
action='store', default='NO_TOKEN_PROVIDED',
help='Permanent token to use')
args = parser.parse_args()
api_address = str(args.address)
api_token = str(args.token)
tmp = os.path.join(os.path.realpath(__file__), '../../assets/country.csv')
country_file_loc = os.path.abspath(tmp)
......@@ -21,11 +42,41 @@ data = data.drop(["Least Developed Countries (LDC)",
"Developed / Developing Countries"],
axis=1)
def make_post(address, data):
h = {'Authorization': 'Token ' + api_token}
r = requests.post(address, headers=h, data=data)
if r.status_code is 403:
raise Exception("Authentification failed")
if not r.ok:
print(r.content)
print(data)
return r.ok
def save_country(name, code, region):
a = api_address + "/country/"
if region is not None:
region = api_address + "/region/" + region + '/'
data = {'name': name, 'iso_alpha3_code': code, 'region': region}
return make_post(a, data)
def save_region(name, code, parent):
a = api_address + "/region/"
if parent is not None:
parent = a + parent + '/'
data = {'name': name, 'un_code': code, 'parent': parent}
return make_post(a, data)
r = [("Region Code", "Region Name"),
("Sub-region Code", "Sub-region Name"),
("Intermediate Region Code", "Intermediate Region Name"),
("ISO-alpha3 Code", "Country or Area")]
inserted_regions = []
for index, row in data.iterrows():
for i in range(len(r) - 1):
......@@ -33,20 +84,23 @@ for index, row in data.iterrows():
region_code = row[r_type[0]]
region_name = row[r_type[1]]
if region_code is not '':
if region_code in inserted_regions:
continue
# look for parent region
parent = None
if i > 0:
parent_code = row[r[i - 1][0]]
if parent_code is not '':
parent = Region.objects.get(pk=parent_code)
region = Region(un_code=region_code,
name=region_name,
parent=parent)
region.save()
parent = parent_code
save_region(region_name, region_code, parent)
inserted_regions.append(region_code)
else:
break
if region_code is '':
region_code = None
c_code = row[r[3][0]]
if c_code is '':
c_code = row["M49 Code"] # Don't forget countries
c_name = row[r[3][1]]
c = Country(name=c_name, iso_alpha3_code=c_code, region=region)
c.save()
save_country(c_name, c_code, region_code)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment