#! /usr/bin/python
# -*- coding:utf-8 -*-
import json
from flask import Flask, render_template, request, redirect, url_for, session
app = Flask(__name__)
username = 'nina'
password = 'ninaetleo'
#taches : dictionnaire (tableau associatif) de tâches
taches=[]
#id : identifiant de la tache
id = 1
@app.route('/')
def accueil():
if not session.get('authentifie'):
return render_template('login.html')
else :
#lire le fichier
lire()
return render_template('accueil.html', message = session['username'])
#sauver()
#print(taches)
return render_template('accueil.html')
@app.route('/login', methods=['POST'])
def login():
# vérification du compte et du mot de passe
if request.form['username'] == username and request.form['password'] == password:
session['authentifie'] = True
session['username'] = username
else:
return render_template('login.html', message = "Erreur de compte ou de mot de passe !")
return redirect(url_for('accueil'))
@app.route("/logout")
def logout():
# deconnexion
session['authentifie'] = False
return redirect(url_for('accueil'))
@app.route('/page2')
def page2():
return render_template('page2.html')
@app.route('/page3')
def page3():
global taches
# fournir au template index.html le tableau taches accomplie et non accomplie
accomplies = []
nonaccomplies = []
for tache in taches:
if tache["accomplie"] == False:
nonaccomplies.append(tache)
else:
accomplies.append(tache)
return render_template('page3.html', accomplies=accomplies, nonaccomplies=nonaccomplies)
@app.route('/toto')
def index():
global taches
# fournir au template index.html le tableau taches accomplie et non accomplie
accomplies = []
nonaccomplies = []
for tache in taches:
if tache["accomplie"] == False:
nonaccomplies.append(tache)
else:
accomplies.append(tache)
return render_template('index.html', accomplies=accomplies, nonaccomplies=nonaccomplies)
@app.route('/ajout', methods=['POST'])
def ajout():
# utiliser les variables globales taches et id
global taches, id
vartache = request.form['tache']
taches.append({"id":id, "libelle":vartache, "accomplie":False})
print("taches apres ajout {}".format(taches))
# sauver le tableau
sauver()
#incrémenter id pour la prochaine tâche
id = id + 1
return redirect(url_for('page3'))
@app.route('/miseajour', methods=['POST'])
def miseajour():
# utiliser les variables globales taches et id
global taches, id
varid = request.form['choix']
print("varid : {}".format(varid))
# mise à jour de la tâche
for tache in taches:
if tache["id"] == int(varid):
tache["accomplie"] = True
return redirect(url_for('page3'))
def sauver():
global taches
with open('leonie.txt', 'w') as fichier:
fichier.write(json.dumps(taches, indent=4))
print(taches)
def lire():
global taches, id
#with open(url_for('static', filename='leonie.txt'), "r") as fichier:
with open('leonie.txt', 'r') as fichier:
taches=json.load(fichier)
print("taches contient {}".format(taches))
id=len(taches)+1
if __name__ == '__main__':
# il faut définir une clé secrete
app.secret_key = 'laclesecrete'
app.run(debug=True, host='0.0.0.0', port=5005)
==== Fichiers HTML ====
* page login.html
Projet Bullet'journal
NINA et LEO
Bienvenue les poulets/poulettes.
Authentification
{{message}}
* page accueil.html
Projet Bullet'journal
* page page2.html
Projet Bullet'journal
* page page3.html
Projet Bullet'journal
Todo liste
Ajouter une nouvelle tâche :
Tâches non accomplies
Tâches accomplies
{% for tache in accomplies %}
- {{tache.libelle}}
{% endfor %}
* fichier style.css
body {
background-color: white;
color: white;
}
#agenda{
background-image:url("images/ouvert.jpg");
background-repeat:no-repeat;
background-size: cover;
}
#todo {
#margin-left:20px;
padding:70px;
}
#toto {
padding:70px;
background-image:url("images/ouvertG.jpg");
background-repeat:no-repeat;
}
#agendaG {
width:800px;
padding:40px;
background-image:url("images/ouvertG.jpg");
background-repeat:no-repeat;
background-size:cover;
}
#agendaD {
padding:70px;
background-image:url("images/ouvertD.jpg");
background-repeat:no-repeat;
background-size:cover;
}
* fichier de données leonie.txt
[
{
"id": 1,
"libelle": "Lire",
"accomplie": true
},
{
"id": 2,
"libelle": "Aller en cours",
"accomplie": false
},
{
"id": 3,
"libelle": "RDV dentiste",
"accomplie": false
}
]