def sauvegarder(request):
    username = request.session
    if 'idsession' not in request.session:
        return redirect("/")
    now = datetime.datetime.now()
    gest = dbEtablissement()

    progr = dbProgramme()
    programme = progr.returnAll()

    nometab = request.GET['etablissement']
    etabli = gest.searchEtab(nom=nometab)

    all = gest.returnAll()
    annee = request.GET['annee']
    semestre = request.GET['semestre']
    nomcours = request.GET['nomcours']
    etab = dbIdcours()
    code = "{}-{}{}-{}".format(etabli.nom,annee,semestre,nomcours)
    ecole = Idcours(etablissement=etabli,annee=annee,semestre=semestre,nomcours=nomcours,codecours=code,date=now)

    if(not etab.isExist(id_etablissement=etabli,annee=annee,semestre=semestre,nomcours=nomcours,codecours=code)):
        if(not etab.save(ecole)):
            message = "Code cours ajouter !"
        else:
            message = "Code cours non ajouter."
    else:
        message = "le Code cours {} existe deja.".format(code)
    return render(request, 'idcours/ajouter.html',{'message': message,'etab':all,'programme':programme,'username':username['idsession']})
def supprimer(request,id):
    username = request.session
    if 'idsession' not in request.session:
        return redirect("/")
    gest = dbIdcours()
    etab = gest.returnOne(id)
    return render(request, 'idcours/supprimer.html',{'etab':etab,'username':username['idsession']})
def index(request):
    username = request.session
    if 'idsession' not in request.session:
        return redirect("/")
    gest = dbIdcours()
    schools = gest.returnAll()
    return render(request, 'idcours/lister.html', {'school': schools,'username':username['idsession']})
def savemodification(request,id):
    username = request.session
    if 'idsession' not in request.session:
        return redirect("/")
    now = datetime.datetime.now()

    cod = request.GET['idcours']
    code = dbIdcours()
    idcours = code.returnID(codecours=cod)
    p = request.GET['professeur']
    pro = dbProfesseur()
    prof = pro.returnOne(id=p)
    nomab = request.GET['etablissement']
    tab = dbEtablissement()
    etablissement = tab.returnOne(id=nomab)
    titre = request.GET['titre']
    creditECTS = request.GET['creditECTS']
    public = request.GET['public']
    objectif = request.GET['objectif']
    description = request.GET['description']
    formatcours = request.GET['formatcours']
    prerequis = request.GET['prerequis']
    ressources = request.GET['ressources']
    evaluation = request.GET['evaluation']
    plan = request.GET['plan']

    etab = dbCours()
    if(etab.modify(id=id,idcours=idcours,professeur=prof,etablissement=etablissement,titre=titre,creditECTS=creditECTS,public=public,objectif=objectif,description=description,plan=plan,formatcours=formatcours,prerequis=prerequis,ressources=ressources,evaluation=evaluation,date=now)):
        message = "Code cours non modifier !"
    else:
        message = "Code cours modifier."
    return render(request, 'cours/savemodification.html',{'message':message,'username':username['idsession']})
def modifier(request,id):
    username = request.session
    if 'idsession' not in request.session:
        return redirect("/")
    idcours = dbIdcours()
    cours = idcours.returnOne(id)
    gest = dbEtablissement()
    etab = gest.returnAll()
    return render(request, 'idcours/modifier.html',{'etab':cours,'school':etab,'username':username['idsession']})
def savesuppression(request,id):
    username = request.session
    if 'idsession' not in request.session:
        return redirect("/")
    gest = dbIdcours()
    etab = gest.returnOne(id=id)

    if(not etab==None):
        if(not gest.delete(id=id)):
            message = "Code cours effacee."
        else:
            message = "Code cours non effacee."
    else:
        message = "le code cours n'existe plus !"
    return render(request, 'idcours/savesuppression.html',{'message':message,'etab':etab,'username':username['idsession']})
def ajouter(request):
    username = request.session
    if 'idsession' not in request.session:
        return redirect("/")
    now = datetime.datetime.now()
    id = dbIdcours()
    idcours = id.returnAll()
    dico = {}
    for ele in idcours:
        dico = {ele.id:ele.nomcours}
    gest = dbEtablissement()
    etab = gest.returnAll()
    dbprof = dbProfesseur()
    prof = dbprof.returnAll()
    t = get_template('cours/ajouter.html')
    html = t.render(Context({'current_date': now,'idcours':idcours,'prof':prof,'etab':etab,'dico':dico,'username':username['idsession']}))
    return HttpResponse(html)
def savemodification(request,id):
    username = request.session
    if 'idsession' not in request.session:
        return redirect("/")
    now = datetime.datetime.now()
    gest = dbEtablissement()
    nometab = request.GET['etablissement']
    etabli = gest.searchEtab(nom=nometab)
    all = gest.returnAll()
    annee = request.GET['annee']
    semestre = request.GET['semestre']
    nomcours = request.GET['nomcours']
    code = "{}-{}{}-{}".format(etabli.nom,annee,semestre,nomcours)
    etab = dbIdcours()
    ecole = Idcours(etablissement=etabli,annee=annee,semestre=semestre,nomcours=nomcours,codecours=code,date=now)
    if(etab.modify(id=id,id_etablissement=etabli,annee=annee,semestre=semestre,nomcours=nomcours,codecours=code,date=now)):
        message = "Code cours non modifier !"
    else:
        message = "Code cours modifier."
    return render(request, 'idcours/savemodification.html',{'message':message,'etab':ecole,'school':all,'username':username['idsession']})
def sauvegarder(request):
    username = request.session
    if 'idsession' not in request.session:
        return redirect("/")
    now = datetime.datetime.now()

    idcours = request.GET['idcours']
    prof = request.GET['professeur']
    nometab = request.GET['etablissement']
    titre = request.GET['titre']
    creditECTS = request.GET['creditECTS']
    public = request.GET['public']
    objectif = request.GET['objectif']
    description = request.GET['description']
    formatcours = request.GET['formatcours']
    prerequis = request.GET['prerequis']
    ressources = request.GET['ressources']
    evaluation = request.GET['evaluation']
    plan = request.GET['plan']

    id = dbIdcours()
    cours = id.returnAll()
    gest = dbEtablissement()
    etabli = gest.returnAll()
    dbprof = dbProfesseur()
    professeur = dbprof.returnAll()

    etab = dbCours()
    ecole = Cours(idcours=id.returnOne(id=idcours),professeur=dbprof.returnOne(id=prof),etablissement=gest.returnOne(id=nometab),titre=titre,creditECTS=creditECTS,public=public,objectif=objectif,description=description,plan=plan,formatcours=formatcours,prerequis=prerequis,ressources=ressources,evaluation=evaluation,date=now)

    if(not etab.isExist(idcours=id.returnOne(id=idcours),professeur=dbprof.returnOne(id=prof),etablissement=gest.returnOne(id=nometab),titre=titre)):
        if(not etab.save(ecole)):
            message = "Code cours ajouter !"
        else:
            message = "Code cours non ajouter."
    else:
        message = "le cours {} existe deja.".format(ecole.titre)
    return render(request, 'cours/ajouter.html',{'message': message,'idcours':cours,'prof':professeur,'etab':etabli,'username':username['idsession']})