from backend_app.models.country import Country import os import pandas as pd from .loadGeneric import LoadGeneric class LoadCountries(LoadGeneric): def __init__(self, admin): self.admin = admin def load(self): tmp = os.path.join(os.path.realpath(__file__), "../../assets/country.csv") country_file_loc = os.path.abspath(tmp) tmp = os.path.join( os.path.realpath(__file__), "../../assets/alpha-conv-table.csv" ) conv_alpha_file_loc = os.path.abspath(tmp) country_pd = pd.read_csv( country_file_loc, sep=",", header=0, dtype=object ).fillna("") # Need to load the information for converting # Countries alpha-3 code to alpha-2 code data_conv = pd.read_csv(conv_alpha_file_loc, sep=",", header=0, na_filter=False) conv_alpha = {} for index, row in data_conv.iterrows(): conv_alpha[row["alpha-3"]] = row["alpha-2"] for index, r in country_pd.iterrows(): Iso_3 = str(r["ISO-alpha3 Code"]) code_alpha_2 = None if Iso_3 in conv_alpha.keys(): code_alpha_2 = conv_alpha[Iso_3] else: print("ignoring country :", Iso_3) continue country = Country( name=r["Country or Area"], iso_alpha2_code=code_alpha_2, iso_alpha3_code=Iso_3, region_name=r["Region Name"], region_un_code=r["Region Code"], sub_region_name=r["Sub-region Name"], sub_region_un_code=r["Sub-region Code"], intermediate_region_name=r["Intermediate Region Name"], intermediate_region_un_code=r["Intermediate Region Code"], ) country.save() self.add_info_and_save(country, self.admin)