loadUniversities.py 1.85 KB
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
2
3
4
5
6
import os

from django.contrib.auth.models import User

import pandas as pd
from backend_app.models.campus import Campus
Florent Chehab's avatar
Florent Chehab committed
7
from backend_app.models.city import City
Florent Chehab's avatar
Florent Chehab committed
8
from backend_app.models.country import Country
Florent Chehab's avatar
Florent Chehab committed
9
from backend_app.models.university import University
Florent Chehab's avatar
Florent Chehab committed
10
11
12
13
14

from .loadGeneric import LoadGeneric


class LoadUniversities(LoadGeneric):
Florent Chehab's avatar
Florent Chehab committed
15
16
17
18
19
    """
    Load the universities in the app
    """

    def __init__(self, admin: User):
Florent Chehab's avatar
Florent Chehab committed
20
21
22
23
        self.admin = admin

    def load(self):

24
25
26
        tmp = os.path.join(
            os.path.realpath(__file__), "../../assets/destinations_extracted.csv"
        )
Florent Chehab's avatar
Florent Chehab committed
27
28
        destinations_path = os.path.abspath(tmp)

29
30
31
        data = pd.read_csv(destinations_path, sep=",", header=0, dtype=object).fillna(
            ""
        )
Florent Chehab's avatar
Florent Chehab committed
32
33

        for index, row in data.iterrows():
34
35
36
            utc_id, univ_name, city_name, country_code, lat, lon, acronym, website, logo = (
                row
            )
Florent Chehab's avatar
Florent Chehab committed
37
38
39
            lat = round(float(lat), 6)
            lon = round(float(lon), 6)

40
            country = Country.objects.get(pk=country_code)
41
            city = City(name=city_name, country=country)
Florent Chehab's avatar
Florent Chehab committed
42
            city.save()
43
            self.add_info_and_save(city, self.admin)
Florent Chehab's avatar
Florent Chehab committed
44

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

            main_campus = Campus(
                is_main_campus=True,
                name="Campus - " + univ.name,
                city=city,
                university=univ,
                lat=lat,
62
                lon=lon,
Florent Chehab's avatar
Florent Chehab committed
63
64
            )
            main_campus.save()
65
            self.add_info_and_save(main_campus, self.admin)