loadUniversities.py 1.77 KB
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
2
3
4
from backend_app.models.country import Country
from backend_app.models.city import City
from backend_app.models.university import University
from backend_app.models.campus import Campus
Florent Chehab's avatar
Florent Chehab committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

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,
Florent Chehab's avatar
Florent Chehab committed
23
                           dtype=object).fillna('')
Florent Chehab's avatar
Florent Chehab committed
24
25

        for index, row in data.iterrows():
26
            utc_id, univ_name, city_name, country_code, lat, lon, acronym, website, logo = row
Florent Chehab's avatar
Florent Chehab committed
27
28
29
            lat = round(float(lat), 6)
            lon = round(float(lon), 6)

30
            country = Country.objects.get(pk=country_code)
Florent Chehab's avatar
Florent Chehab committed
31
32
33
34
35
            city = City(
                name=city_name,
                country=country
            )
            city.save()
36
            self.add_info_and_save(city, self.admin)
Florent Chehab's avatar
Florent Chehab committed
37

38
39
40
41
42
43
44
45
46
            univ = University.objects.update_or_create(
                utc_id=utc_id,
                defaults={
                    "name": univ_name,
                    "acronym": acronym,
                    "website": website,
                    "logo": logo
                }
            )[0]
47
            self.add_info_and_save(univ, self.admin)
Florent Chehab's avatar
Florent Chehab committed
48
49
50
51
52
53
54
55
56
57

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