Supprimer z2/app.py
This commit is contained in:
parent
cc1f7b921d
commit
e554dcd948
173
z2/app.py
173
z2/app.py
@ -1,173 +0,0 @@
|
||||
from flask import Flask, request, redirect, url_for, render_template_string
|
||||
import os
|
||||
import psycopg2
|
||||
from psycopg2 import pool
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
DB_HOST = os.environ.get('DB_HOST', 'localhost')
|
||||
DB_PORT = os.environ.get('DB_PORT', '5432')
|
||||
DB_NAME = os.environ.get('DB_NAME', 'postgres')
|
||||
DB_USER = os.environ.get('DB_USER', 'postgres')
|
||||
DB_PASS = os.environ.get('DB_PASS', 'postgres')
|
||||
|
||||
connection_pool = psycopg2.pool.SimpleConnectionPool(
|
||||
1, 10,
|
||||
host=DB_HOST,
|
||||
port=DB_PORT,
|
||||
database=DB_NAME,
|
||||
user=DB_USER,
|
||||
password=DB_PASS
|
||||
)
|
||||
|
||||
def init_db():
|
||||
conn = connection_pool.getconn()
|
||||
try:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute('''
|
||||
CREATE TABLE IF NOT EXISTS shopping_items (
|
||||
id SERIAL PRIMARY KEY,
|
||||
item TEXT NOT NULL,
|
||||
quantity INTEGER DEFAULT 1,
|
||||
purchased BOOLEAN DEFAULT FALSE
|
||||
);
|
||||
''')
|
||||
conn.commit()
|
||||
except Exception as e:
|
||||
print(f"Erreur lors de l'initialisation de la base de données: {e}")
|
||||
conn.rollback()
|
||||
finally:
|
||||
connection_pool.putconn(conn)
|
||||
|
||||
HTML_TEMPLATE = """
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Liste de Courses</title>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
</head>
|
||||
<body>
|
||||
<h1>Ma Liste de Courses</h1>
|
||||
|
||||
<form action="/add" method="post">
|
||||
<input type="text" name="item" placeholder="Article" required>
|
||||
<input type="number" name="quantity" value="1" min="1">
|
||||
<button type="submit">Ajouter</button>
|
||||
</form>
|
||||
|
||||
<h2>Articles à acheter</h2>
|
||||
<ul>
|
||||
{% for item in items %}
|
||||
{% if not item[3] %}
|
||||
<li>
|
||||
{{ item[1] }} ({{ item[2] }})
|
||||
<form action="/toggle/{{ item[0] }}" method="post" style="display:inline">
|
||||
<button type="submit">Acheté</button>
|
||||
</form>
|
||||
<form action="/delete/{{ item[0] }}" method="post" style="display:inline">
|
||||
<button type="submit">Supprimer</button>
|
||||
</form>
|
||||
</li>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</ul>
|
||||
|
||||
<h2>Articles achetés</h2>
|
||||
<ul>
|
||||
{% for item in items %}
|
||||
{% if item[3] %}
|
||||
<li>
|
||||
{{ item[1] }} ({{ item[2] }})
|
||||
<form action="/toggle/{{ item[0] }}" method="post" style="display:inline">
|
||||
<button type="submit">Non acheté</button>
|
||||
</form>
|
||||
<form action="/delete/{{ item[0] }}" method="post" style="display:inline">
|
||||
<button type="submit">Supprimer</button>
|
||||
</form>
|
||||
</li>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
conn = connection_pool.getconn()
|
||||
try:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("SELECT id, item, quantity, purchased FROM shopping_items ORDER BY purchased, id")
|
||||
items = cur.fetchall()
|
||||
except Exception as e:
|
||||
print(f"Erreur lors de la récupération des articles: {e}")
|
||||
items = []
|
||||
finally:
|
||||
connection_pool.putconn(conn)
|
||||
|
||||
return render_template_string(HTML_TEMPLATE, items=items)
|
||||
|
||||
@app.route('/add', methods=['POST'])
|
||||
def add_item():
|
||||
item = request.form.get('item')
|
||||
quantity = request.form.get('quantity', 1, type=int)
|
||||
|
||||
conn = connection_pool.getconn()
|
||||
try:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"INSERT INTO shopping_items (item, quantity) VALUES (%s, %s)",
|
||||
(item, quantity)
|
||||
)
|
||||
conn.commit()
|
||||
except Exception as e:
|
||||
print(f"Erreur lors de l'ajout d'un article: {e}")
|
||||
conn.rollback()
|
||||
finally:
|
||||
connection_pool.putconn(conn)
|
||||
|
||||
return redirect(url_for('index'))
|
||||
|
||||
@app.route('/toggle/<int:item_id>', methods=['POST'])
|
||||
def toggle_item(item_id):
|
||||
conn = connection_pool.getconn()
|
||||
try:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"UPDATE shopping_items SET purchased = NOT purchased WHERE id = %s",
|
||||
(item_id,)
|
||||
)
|
||||
conn.commit()
|
||||
except Exception as e:
|
||||
print(f"Erreur lors du basculement du statut: {e}")
|
||||
conn.rollback()
|
||||
finally:
|
||||
connection_pool.putconn(conn)
|
||||
|
||||
return redirect(url_for('index'))
|
||||
|
||||
@app.route('/delete/<int:item_id>', methods=['POST'])
|
||||
def delete_item(item_id):
|
||||
conn = connection_pool.getconn()
|
||||
try:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("DELETE FROM shopping_items WHERE id = %s", (item_id,))
|
||||
conn.commit()
|
||||
except Exception as e:
|
||||
print(f"Erreur lors de la suppression d'un article: {e}")
|
||||
conn.rollback()
|
||||
finally:
|
||||
connection_pool.putconn(conn)
|
||||
|
||||
return redirect(url_for('index'))
|
||||
|
||||
with app.app_context():
|
||||
try:
|
||||
init_db()
|
||||
except Exception as e:
|
||||
print(f"Impossible d'initialiser la base de données: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
port = int(os.environ.get("PORT", 5000))
|
||||
app.run(host='0.0.0.0', port=port, debug=True)
|
Loading…
Reference in New Issue
Block a user