insert_universities.py 761 Bytes
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
linting    
Florent Chehab committed
8
import csv
Florent Chehab's avatar
Florent Chehab committed
9
10
import os

Florent Chehab's avatar
linting    
Florent Chehab committed
11
from rex.models.university import University
Florent Chehab's avatar
Florent Chehab committed
12

Florent Chehab's avatar
linting    
Florent Chehab committed
13
14
tmp = os.path.join(os.path.realpath(__file__),
                   '../../assets/destinations_extracted.csv')
Florent Chehab's avatar
Florent Chehab committed
15
16
17
18
19
20
21
22
23
24
25
destinations_path = os.path.abspath(tmp)

if not os.path.isfile(destinations_path):
    print(destinations_path)
    raise Exception("Missing file containing country data")

with open(destinations_path, 'rt') as f:
    reader = csv.reader(f)

    for row in reader:
        if row[0] != 'University':
Florent Chehab's avatar
linting    
Florent Chehab committed
26
27
            u = University(name=row[0], acronym='NULL', localization_lat=float(
                row[1]), localization_lon=float(row[2]))
Florent Chehab's avatar
Florent Chehab committed
28
            u.save()