loadCountries.py 1.97 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.country import Country
Florent Chehab's avatar
Florent Chehab committed
5
from base_app.models import User
Florent Chehab's avatar
Florent Chehab committed
6 7 8 9
from .loadGeneric import LoadGeneric


class LoadCountries(LoadGeneric):
Florent Chehab's avatar
Florent Chehab committed
10 11 12 13 14
    """
        Class to handle the loading of countries in the app
    """

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

17 18
    @staticmethod
    def get_countries_data():
Florent Chehab's avatar
Florent Chehab committed
19
        country_file_loc = abspath(join(ASSETS_PATH, "country.csv"))
20
        return csv_2_dict_list(country_file_loc)
Florent Chehab's avatar
Florent Chehab committed
21

22 23 24 25
    @staticmethod
    def get_iso_conv():
        conv_alpha_file_loc = abspath(join(ASSETS_PATH, "alpha-conv-table.csv"))
        return csv_2_dict_list(conv_alpha_file_loc)
Florent Chehab's avatar
Florent Chehab committed
26

27
    def load(self):
Florent Chehab's avatar
Florent Chehab committed
28
        # Need to load the information for converting
29
        # countries alpha-3 code to alpha-2 code
Florent Chehab's avatar
Florent Chehab committed
30
        conv_alpha = {}
31
        for row in self.get_iso_conv():
Florent Chehab's avatar
Florent Chehab committed
32 33
            conv_alpha[row["alpha-3"]] = row["alpha-2"]

34 35 36 37
        for row in self.get_countries_data():
            code_iso_3 = str(row["ISO-alpha3 Code"])
            if code_iso_3 in conv_alpha.keys():
                code_alpha_2 = conv_alpha[code_iso_3]
Florent Chehab's avatar
Florent Chehab committed
38
            else:
39 40 41 42
                print(
                    "This country is not correctly identified and won't be inserted in db:",
                    row,
                )
Florent Chehab's avatar
Florent Chehab committed
43 44 45
                continue

            country = Country(
46
                name=row["Country or Area"],
Florent Chehab's avatar
Florent Chehab committed
47
                iso_alpha2_code=code_alpha_2,
48 49 50 51 52 53 54
                iso_alpha3_code=code_iso_3,
                region_name=row["Region Name"],
                region_un_code=row["Region Code"],
                sub_region_name=row["Sub-region Name"],
                sub_region_un_code=row["Sub-region Code"],
                intermediate_region_name=row["Intermediate Region Name"],
                intermediate_region_un_code=row["Intermediate Region Code"],
Florent Chehab's avatar
Florent Chehab committed
55 56
            )
            country.save()
57
            self.add_info_and_save(country, self.admin)