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

import pandas as pd
4
from backend_app.load_data.shared import ASSETS_PATH
Florent Chehab's avatar
Florent Chehab committed
5
from backend_app.models.campus import Campus
Florent Chehab's avatar
Florent Chehab committed
6
from backend_app.models.city import City
Florent Chehab's avatar
Florent Chehab committed
7
from backend_app.models.country import Country
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 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
        self.admin = admin

    def load(self):
Florent Chehab's avatar
Florent Chehab committed
23
        destinations_path = abspath(join(ASSETS_PATH, "destinations_extracted.csv"))
Florent Chehab's avatar
Florent Chehab committed
24

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

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

36
            country = Country.objects.get(pk=country_code)
37
            city = City(name=city_name, country=country)
Florent Chehab's avatar
Florent Chehab committed
38
            city.save()
39
            self.add_info_and_save(city, self.admin)
Florent Chehab's avatar
Florent Chehab committed
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,
47 48
                    "logo": logo,
                },
49
            )[0]
50
            self.add_info_and_save(univ, self.admin)
Florent Chehab's avatar
Florent Chehab committed
51 52 53 54 55 56 57

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