insert_universities.py 2.36 KB
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
2
3
4
5
6
7
#!/usr/bin/env python3

"""
Script to insert the country data in the database

IT HAS TO BE RUN INSIDE ./manage.py shell
"""
Florent Chehab's avatar
Florent Chehab committed
8
import argparse
Florent Chehab's avatar
Florent Chehab committed
9
import os
Florent Chehab's avatar
Florent Chehab committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import pandas as pd
import requests
import json

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)
Florent Chehab's avatar
Florent Chehab committed
34
35


Florent Chehab's avatar
linting  
Florent Chehab committed
36
37
tmp = os.path.join(os.path.realpath(__file__),
                   '../../assets/destinations_extracted.csv')
Florent Chehab's avatar
Florent Chehab committed
38
39
destinations_path = os.path.abspath(tmp)

Florent Chehab's avatar
Florent Chehab committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
data = pd.read_csv(destinations_path, sep=',', header=0,
                   dtype=object)


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")
    print(r.content)
    return json.loads(r.content)["id"]


inserted_city_cc = {}


def insert_city(city_name, country):
    if not city_name + country in inserted_city_cc.keys():
        address = api_address + "/city/"
        data = {'country': country, 'name': city_name}
        inserted_city_cc[city_name + country] = make_post(address, data)
    return inserted_city_cc[city_name + country]


64
65
66
def insert_university(name):
    address = api_address + "/university/"
    data = {'name': name}
Florent Chehab's avatar
Florent Chehab committed
67
68
69
    return make_post(address, data)


70
71
72
73
74
def insert_main_campus(name, city_id, univ_id, lat, lon):
    address = api_address + "/campus/"
    data = {'name': "Campus " + name, 'city': city_id, 'lat': lat,
            'lon': lon, 'is_main_campus': True,
            'university': univ_id}
Florent Chehab's avatar
Florent Chehab committed
75
    return make_post(address, data)
Florent Chehab's avatar
Florent Chehab committed
76
77


Florent Chehab's avatar
Florent Chehab committed
78
79
80
81
82
for index, row in data.iterrows():
    univ, city, country, lat, lon = row
    lat = round(float(lat), 6)
    lon = round(float(lon), 6)
    city_id = insert_city(city, country)
83
84
    univ_id = insert_university(univ)
    m_c_id = insert_main_campus(univ, city_id, univ_id, lat, lon)