loadUniversities.py 1.43 KB
Newer Older
Florent Chehab's avatar
Florent Chehab committed
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 49 50 51
from backend.models.location import City, Country
from backend.models.university import University, Campus

import os
import pandas as pd
from .loadGeneric import LoadGeneric


class LoadUniversities(LoadGeneric):

    def __init__(self, admin):
        self.admin = admin

    def load(self):

        tmp = os.path.join(os.path.realpath(__file__),
                           '../../assets/destinations_extracted.csv')
        destinations_path = os.path.abspath(tmp)

        data = pd.read_csv(destinations_path, sep=',', header=0,
                           dtype=object)

        for index, row in data.iterrows():
            univ_name, city_name, country_code, lat, lon = row
            lat = round(float(lat), 6)
            lon = round(float(lon), 6)

            country = Country.objects.filter(pk=country_code)[0]
            city = City(
                name=city_name,
                country=country
            )
            city.save()
            self.add_info(city, self.admin)

            univ = University(
                name=univ_name
            )
            univ.save()
            self.add_info(univ, self.admin)

            main_campus = Campus(
                is_main_campus=True,
                name="Campus - " + univ.name,
                city=city,
                university=univ,
                lat=lat,
                lon=lon
            )
            main_campus.save()
            self.add_info(main_campus, self.admin)