diff --git a/z2/app/app.py b/z2/app/app.py new file mode 100644 index 0000000..8383ef9 --- /dev/null +++ b/z2/app/app.py @@ -0,0 +1,291 @@ +from flask import Flask, request, render_template_string, redirect, url_for +import os +import psycopg2 + +app = Flask(__name__) + + +# Function to make connection to database +def get_db_connection(): + conn = psycopg2.connect( + dbname="travel_planner", # Assurez-vous que ce nom correspond à celui dans statefulset.yaml + user="user", + password="password", + host="postgres-db", # Doit correspondre au nom du service + port="5432" + ) + return conn + + + +# Init database & create trips table if doesn't exist +def init_db(): + conn = get_db_connection() + cur = conn.cursor() + cur.execute(""" + CREATE TABLE IF NOT EXISTS travels ( + id SERIAL PRIMARY KEY, + traveler_name VARCHAR(255) NOT NULL, + name_travel VARCHAR(255) NOT NULL, + start_date DATE NOT NULL, + end_date DATE NOT NULL + ); + + CREATE TABLE IF NOT EXISTS stages ( + id SERIAL PRIMARY KEY, + travel_id INT REFERENCES travels(id) ON DELETE CASCADE, + city VARCHAR(255) NOT NULL, + order_index INT NOT NULL, + image_url TEXT + ); + + """) + conn.commit() + cur.close() + conn.close() + + +@app.route("/", methods=["GET", "POST"]) +def index(): + if request.method == "POST": + traveler_name = request.form["traveler_name"] # Name traveler + name_travel = request.form["name_travel"] # Travel's name + start_date = request.form["start_date"] # Start date + end_date = request.form["end_date"] # End date + + # Ajouter un nouveau voyage à la base de données + conn = get_db_connection() + cur = conn.cursor() + cur.execute(""" + INSERT INTO travels (traveler_name, name_travel, start_date, end_date) + VALUES (%s, %s, %s, %s) + """, (traveler_name, name_travel, start_date, end_date)) + conn.commit() + cur.close() + conn.close() + + # Show all travels + conn = get_db_connection() + cur = conn.cursor() + cur.execute("SELECT * FROM travels ORDER BY traveler_name") + travels = cur.fetchall() + cur.close() + conn.close() + + return render_template_string(""" + + Travel Planner + +

Travel Planner

+
+ + + + + +
+ +

All Travels

+ + + + """, travels=travels) + +#How can you show all stages for one travel ? +@app.route("/travel/") +def view_travel(travel_id): + # travel details + conn = get_db_connection() + cur = conn.cursor() + cur.execute("SELECT * FROM travels WHERE id = %s", (travel_id,)) + travel = cur.fetchone() + cur.close() + + if travel is None: + return "This travel doesn't exist", 404 + + # travel stage + conn = get_db_connection() + cur = conn.cursor() + cur.execute("SELECT * FROM stages WHERE travel_id = %s ORDER BY order_index", (travel_id,)) + stages = cur.fetchall() + cur.close() + + return render_template_string(""" + + Travel Details + +

{{ travel[1] }}

+

Traveler: {{ travel[2] }}

+

From: {{ travel[3] }} To: {{ travel[4] }}

+ +

Steps

+ +

Add Step

+
+ + + +
+ Back to All Trips + + + """, travel=travel, stages=stages) + +# How can you delete one stage in the travel ? +@app.route("/remove/") +def remove_stage(stage_id): + conn = get_db_connection() + cur = conn.cursor() + cur.execute("DELETE FROM stages WHERE id = %s", (stage_id,)) + conn.commit() + cur.close() + conn.close() + return redirect(url_for('index')) + +# How can you delete one trip ? +@app.route("/remove/") +def remove_travel(travel_id): + conn = get_db_connection() + cur = conn.cursor() + cur.execute("DELETE FROM travels WHERE id = %s", (travel_id,)) + conn.commit() + cur.close() + conn.close() + return redirect(url_for('index')) + +#Edit for travel +@app.route("/change/", methods=["GET", "POST"]) +def change_travel(travel_id): + if request.method == "POST": + traveler_name = request.form["traveler_name"] # Name traveler + name_travel = request.form["name_travel"] # Travel's name + start_date = request.form["start_date"] # Start date + end_date = request.form["end_date"] # End date + conn = get_db_connection() + cur = conn.cursor() + cur.execute("UPDATE travels SET traveler_name = %s, name_travel = %s, start_date = %s, end_date = %s WHERE id = %s;", (traveler_name,name_travel,start_date,end_date,travel_id)) + conn.commit() + cur.close() + conn.close() + return redirect(url_for('index')) + + # Show edit for travel + conn = get_db_connection() + cur = conn.cursor() + cur.execute("SELECT * FROM travels WHERE id = %s", (travel_id,)) + travel = cur.fetchone() + cur.close() + conn.close() + + return render_template_string(""" + + Edit Travel + +

Edit Travel

+
+
    +
  • + +
  • +
  • + +
  • +
  • + +
  • +
  • + +
  • + + + + Back to Travels + + + """, travel=travel) + +#Edit for stages +@app.route("/change/", methods=["GET", "POST"]) +def change_stage(stage_id): + if request.method == "POST": + city = request.form["city"] + order_index = request.form["order_index"] + image_url = request.form["image_url"] + conn = get_db_connection() + cur = conn.cursor() + cur.execute("UPDATE stages SET city = %s, order_index = %s, image_url = %s WHERE id = %s;", (city,order_index,image_url,stage_id)) + conn.commit() + cur.close() + conn.close() + return redirect(url_for('index')) + + # Show edit for travel + conn = get_db_connection() + cur = conn.cursor() + cur.execute("SELECT * FROM stages WHERE id = %s", (stage_id,)) + stage = cur.fetchone() + cur.close() + conn.close() + + return render_template_string(""" + + Edit Stage + +

    Edit Stage

    +
    +
      +
    • + +
    • +
    • + +
    • +
    • + +
    • + + + + Back to Stages + + + """, stage=stage) + +@app.route("/add_stage/", methods=["POST"]) +def add_stage(travel_id): + city = request.form["city"] + order_index = request.form["order_index"] + conn = get_db_connection() + cur = conn.cursor() + cur.execute(""" + INSERT INTO stages (travel_id, city, order_index) + VALUES (%s, %s, %s) + """, (travel_id, city, order_index)) + conn.commit() + cur.close() + conn.close() + return redirect(url_for('view_travel', travel_id=travel_id)) + + +if __name__ == "__main__": + init_db() # Init bdd + app.run(host='0.0.0.0', port=5000) + diff --git a/z2/app/requirements.txt b/z2/app/requirements.txt new file mode 100644 index 0000000..aa3ffb7 --- /dev/null +++ b/z2/app/requirements.txt @@ -0,0 +1,3 @@ +Flask==2.1.2 +psycopg2-binary==2.9.3 +Werkzeug==2.0.3 \ No newline at end of file