loadUniversities.py 1.75 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

import os
import pandas as pd
from .loadGeneric import LoadGeneric


class LoadUniversities(LoadGeneric):
    def __init__(self, admin):
        self.admin = admin

    def load(self):

17
18
19
        tmp = os.path.join(
            os.path.realpath(__file__), "../../assets/destinations_extracted.csv"
        )
Florent Chehab's avatar
Florent Chehab committed
20
21
        destinations_path = os.path.abspath(tmp)

22
23
24
        data = pd.read_csv(destinations_path, sep=",", header=0, dtype=object).fillna(
            ""
        )
Florent Chehab's avatar
Florent Chehab committed
25
26

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

33
            country = Country.objects.get(pk=country_code)
34
            city = City(name=city_name, country=country)
Florent Chehab's avatar
Florent Chehab committed
35
            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
            univ = University.objects.update_or_create(
                utc_id=utc_id,
                defaults={
                    "name": univ_name,
                    "acronym": acronym,
                    "website": website,
44
45
                    "logo": logo,
                },
46
            )[0]
47
            self.add_info_and_save(univ, self.admin)
Florent Chehab's avatar
Florent Chehab committed
48
49
50
51
52
53
54

            main_campus = Campus(
                is_main_campus=True,
                name="Campus - " + univ.name,
                city=city,
                university=univ,
                lat=lat,
55
                lon=lon,
Florent Chehab's avatar
Florent Chehab committed
56
57
            )
            main_campus.save()
58
            self.add_info_and_save(main_campus, self.admin)