#!/usr/bin/env python3

#importation des exceptions django, des packages python et des classes de l'application

from django.core.exceptions import ObjectDoesNotExist
from application1.models import Incidents, Airports_dep, Airports_arr
import pandas as pd
import numpy as np

#Création des dataframes
incidents = pd.read_csv("/users/mmath/renaudin/projet_bddr/incidents_data.csv", encoding='UTF-8')
airports = pd.read_csv("/users/mmath/renaudin/projet_bddr/airports_data.csv", encoding='UTF-8')


#Remplissage de la table "Incident"

(p,q) = incidents.shape

for k in range(p):
    
    Date = incidents['date'][k]
    code_IACO_dep = Airports_dep.objects.get(IACO=incidents['airp_dep_ICAO'][k])
    code_IACO_arr = Airports_arr.objects.get(IACO=incidents['airp_arr_ICAO'][k])
    localisation = incidents['location'][k]
    inc_country = incidents['incident_country'][k]
    plane = incidents['type'][k]
    
    
#    if not(fatal==None):
#        fatal=int(float(fatal))
    
    try :
        
        incident = Incidents.objects.get(id=k+1)
        
    except ObjectDoesNotExist as err :
            incident = Incidents(id=k+1,date=Date,location=localisation,incident_country=inc_country,plane_type=plane,airport_dep=code_IACO_dep,airport_arr=code_IACO_arr)
            incident.save()
            
            
            
            
            
            