zkt25/z1/app/app.py

150 lines
4.9 KiB
Python

from flask import Flask, request, render_template_string, redirect, url_for
import os
import psycopg2
app = Flask(__name__)
# Retrieve database connection settings
db_host = os.environ.get("DB_HOST", "postgres_db")
db_name = os.environ.get("POSTGRES_DB", "contacts_db")
db_user = os.environ.get("POSTGRES_USER", "user")
db_password = os.environ.get("POSTGRES_PASSWORD", "password")
# Function to make connection to database
def get_db_connection():
conn = psycopg2.connect(
host=db_host,
database=db_name,
user=db_user,
password=db_password
)
return conn
# Init database & create contacts table if doesn't exist
def init_db():
conn = get_db_connection()
cur = conn.cursor()
cur.execute("""
CREATE TABLE IF NOT EXISTS contacts (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
phone VARCHAR(20) NOT NULL
);
""")
conn.commit()
cur.close()
conn.close()
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
name = request.form["name"]
email = request.form["email"]
phone = request.form["phone"]
# Add new contact at the database
conn = get_db_connection()
cur = conn.cursor()
cur.execute("INSERT INTO contacts (name, email, phone) VALUES (%s, %s, %s)",
(name, email, phone))
conn.commit()
cur.close()
conn.close()
# Show all contacts
conn = get_db_connection()
cur = conn.cursor()
cur.execute("SELECT * FROM contacts")
contacts = cur.fetchall()
cur.close()
conn.close()
return render_template_string("""
<html>
<head><title>Contact Directory</title></head>
<body>
<h1>Contact Directory</h1>
<form method="post">
<input type="text" name="name" placeholder="Name" required>
<input type="email" name="email" placeholder="Email" required>
<input type="text" name="phone" placeholder="Phone">
<button type="submit">Add Contact</button>
</form>
<h2>Contacts</h2>
<ul>
{% for contact in contacts %}
<li>
{{ contact[1] }} - {{ contact[2] }} - {{ contact[3] }}
<a href="{{ url_for('remove_contact', contact_id=contact[0]) }}">Delete</a>
<a href="{{ url_for('change_contact', contact_id=contact[0]) }}">Edit</a>
</li>
{% endfor %}
</ul>
</body>
</html>
""", contacts=contacts)
# How can you delete one contact ?
@app.route("/remove/<int:contact_id>")
def remove_contact(contact_id):
conn = get_db_connection()
cur = conn.cursor()
cur.execute("DELETE FROM contacts WHERE id = %s", (contact_id,))
conn.commit()
cur.close()
conn.close()
return redirect(url_for('index'))
@app.route("/change/<int:contact_id>", methods=["GET", "POST"])
def change_contact(contact_id):
if request.method == "POST":
name = request.form["name"]
email = request.form["email"]
phone = request.form["phone"]
conn = get_db_connection()
cur = conn.cursor()
cur.execute("UPDATE contacts SET name = %s, email = %s, phone = %s WHERE id = %s;", (name,email,phone, contact_id))
conn.commit()
cur.close()
conn.close()
return redirect(url_for('index'))
# Show edit for contact
conn = get_db_connection()
cur = conn.cursor()
cur.execute("SELECT * FROM contacts WHERE id = %s", (contact_id,))
contact = cur.fetchone()
cur.close()
conn.close()
return render_template_string("""
<html>
<head><title>Edit Contact</title></head>
<body>
<h1>Edit Contact</h1>
<form method="POST">
<ul>
<li>
<label for="name">New Name</label>
<input type="name" name="name" value="{{ contact[1] }}" required></li>
<li>
<label for="email">New Email</label>
<input type="email" name="email" value="{{ contact[2] }}" required></li>
<li>
<label for="phone">New Phone</label>
<input type="phone" name="phone" value="{{ contact[3] }}" required></li>
<button type="submit">Update Contact</button>
</form>
<a href="{{ url_for('index') }}">Back to Contacts</a>
</body>
</html>
""", contact=contact)
if __name__ == "__main__":
init_db() # Initialiser la base de données et la table
app.run(host='0.0.0.0', port=5000)