loadUniversities.py 2.15 KB
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
from os.path import abspath, join
Florent Chehab's avatar
Florent Chehab committed
2

3
from backend_app.load_data.utils import ASSETS_PATH, csv_2_dict_list
Florent Chehab's avatar
Florent Chehab committed
4
from backend_app.models.campus import Campus
Florent Chehab's avatar
Florent Chehab committed
5
from backend_app.models.city import City
Florent Chehab's avatar
Florent Chehab committed
6
from backend_app.models.country import Country
7
from backend_app.models.partner import Partner
Florent Chehab's avatar
Florent Chehab committed
8
from backend_app.models.university import University
Florent Chehab's avatar
Florent Chehab committed
9
from base_app.models import User
Florent Chehab's avatar
Florent Chehab committed
10 11 12 13
from .loadGeneric import LoadGeneric


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

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

21 22
    @staticmethod
    def get_destination_data():
Florent Chehab's avatar
Florent Chehab committed
23
        destinations_path = abspath(join(ASSETS_PATH, "destinations_extracted.csv"))
24
        return csv_2_dict_list(destinations_path)
Florent Chehab's avatar
Florent Chehab committed
25

26 27 28 29
    def load(self):
        for row in self.get_destination_data():
            lat = round(float(row["lat"]), 6)
            lon = round(float(row["lon"]), 6)
Florent Chehab's avatar
Florent Chehab committed
30

31 32
            country = Country.objects.get(pk=row["country"])
            city = City(name=row["city"], country=country)
Florent Chehab's avatar
Florent Chehab committed
33
            city.save()
34
            self.add_info_and_save(city, self.admin)
Florent Chehab's avatar
Florent Chehab committed
35

36
            univ = University.objects.update_or_create(
37
                pk=row["utc_id"],  # Not perfect but should do the trick
38
                defaults={
39 40 41 42
                    "name": row["university"],
                    "acronym": row["acronym"],
                    "website": row["website"],
                    "logo": row["logo"],
43
                },
44
            )[0]
45
            self.add_info_and_save(univ, self.admin)
Florent Chehab's avatar
Florent Chehab committed
46 47 48 49 50 51 52

            main_campus = Campus(
                is_main_campus=True,
                name="Campus - " + univ.name,
                city=city,
                university=univ,
                lat=lat,
53
                lon=lon,
Florent Chehab's avatar
Florent Chehab committed
54
            )
55
            self.add_info_and_save(main_campus, self.admin)
56 57 58 59 60 61 62 63 64 65 66

            Partner.objects.update_or_create(
                utc_id=row["utc_id"],
                defaults=dict(
                    univ_name=row["university"],
                    city=row["city"],
                    country=row["country"],
                    iso_code=row["country"],
                    university=univ,
                ),
            )