diff --git a/.gitignore b/.gitignore index 2d7e6aa..f2ac9c9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .venv postgresql -__pycache__ \ No newline at end of file +__pycache__ +db.env \ No newline at end of file diff --git a/app/hello.py b/app/hello.py new file mode 100644 index 0000000..f0d6792 --- /dev/null +++ b/app/hello.py @@ -0,0 +1,92 @@ +from datetime import datetime + +from urllib.parse import quote_plus +from sqlalchemy import create_engine, text + +from flask import Flask +from flask import request +from flask import render_template + +DB_USER = 'avsa_form' +DB_PASSWORD = '#4gvAwnUr5@MuZk9cYb!' +DB_HOST = 'avsa_form_db' +DB_PORT = '5432' +DB_NAME = 'avsa_form_db' +DB_TABLE = 'accounts' + +class user_data: + def __init__(self, first_name, last_name, phone_number, request_at, start_availability, end_availability, user_type): + self.first_name = first_name + self.last_name = last_name + self.phone_number = phone_number + self.request_at = request_at + self.start_availability = start_availability + self.end_availability = end_availability + self.user_type = user_type + +# new_data = current_data('lucas','royer','0612345678','2011-05-16 15:36:38','2011-05-16 15:36:38','2011-05-16 15:36:38','benevole') + +def insert_db(current_data): + # Connect to DB + engine = create_engine(f"postgresql+psycopg2://{DB_USER}:%s@{DB_HOST}:{DB_PORT}/{DB_NAME}" % quote_plus(DB_PASSWORD)) + conn = engine.connect() + + + + data = { + 'first_name': current_data.first_name, + 'last_name': current_data.last_name, + 'phone_number': current_data.phone_number, + 'request_at': current_data.request_at, + 'start_availability': current_data.start_availability, + 'end_availability': current_data.end_availability, + 'user_type': current_data.user_type, + } + + # SQL query + query=text(f"INSERT INTO {DB_TABLE} (first_name, last_name, phone_number, request_at, start_availability, end_availability, user_type) VALUES (:first_name, :last_name, :phone_number, :request_at, :start_availability, :end_availability, :user_type);") + + conn.execute(query, data) + conn.commit() + + +def create_app(): + app = Flask(__name__) + app.config.from_mapping( + SECRET_KEY='dev', + ) + + @app.route("/") + def accueil(): + return render_template('accueil.html') + + @app.route('/formulaire', methods=['GET']) + def formulaire(): + return render_template('formulaire.html', retry=False) + + @app.route('/resultat', methods=['POST']) + def resultat(): + if request.method == 'POST': + first_name = request.form['first_name'] + last_name = request.form['last_name'] + phone_number = request.form['phone_number'] + request_at = datetime.now().strftime("%m-%d-%Y %H:%M:%S") + + availability_date = request.form['availability_date'] + start_availability_h = request.form['start_availability_h'] + start_availability_m = request.form['start_availability_m'] + end_availability_h = request.form['end_availability_h'] + end_availability_m = request.form['end_availability_m'] + + start_availability = f'{availability_date} {start_availability_h}:{start_availability_m}:00' + end_availability = f'{availability_date} {end_availability_h}:{end_availability_m}:00' + user_type = request.form['user_type'] + current_data = user_data(first_name, last_name, phone_number, request_at, start_availability, end_availability, user_type) + + if any(value is None for value in current_data.__dict__.values()): + return render_template('formulaire.html', retry=True) + else: + insert_db(current_data) + return render_template('resultat.html', first_name=first_name, last_name=last_name, phone_number=phone_number, request_at=request_at, start_availability_h=start_availability_h, start_availability_m=start_availability_m, end_availability_h=end_availability_h, end_availability_m=end_availability_m, user_type=current_data.user_type) + + return app \ No newline at end of file diff --git a/templates/accueil.html b/app/templates/accueil.html similarity index 100% rename from templates/accueil.html rename to app/templates/accueil.html diff --git a/app/templates/calendar.html b/app/templates/calendar.html new file mode 100644 index 0000000..392c04f --- /dev/null +++ b/app/templates/calendar.html @@ -0,0 +1,405 @@ + + + + + + + + + tailwind Calendar + + + +
+
+
+ + 2020 July + +
+ + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + Sun + + + Mon + + + Tue + + + Wed + + + Thu + + + Fri + + + Sat +
+
+
+ 1 +
+
+
+ + Meating + + + 12:00~14:00 + +
+
+ + Meating + + + 18:00~20:00 + +
+
+
+
+
+
+ 2 +
+
+
+
+
+
+ 3 +
+
+
+
+
+
+ 4 +
+
+
+
+
+
+ 6 +
+
+
+
+
+
+ 7 +
+
+
+ + Shopping + + + 12:00~14:00 + +
+
+
+
+
+
+ 8 +
+
+
+
+
+
+ 9 +
+
+
+
+
+
+ 10 +
+
+
+
+
+
+ 12 +
+
+
+
+
+
+ 13 +
+
+
+
+
+
+ 14 +
+
+
+
+
+
+ 15 +
+
+
+
+
+
+ 16 +
+
+
+
+
+
+ 16 +
+
+
+
+
+
+ 17 +
+
+
+
+
+
+ 18 +
+
+
+
+
+
+ 19 +
+
+
+
+
+
+ 20 +
+
+
+
+
+
+ 21 +
+
+
+
+
+
+ 22 +
+
+
+
+
+
+ 23 +
+
+
+
+
+
+ 24 +
+
+
+
+
+
+ 25 +
+
+
+
+
+
+ 26 +
+
+
+
+
+
+ 27 +
+
+
+
+
+
+ 28 +
+
+
+
+
+
+ 29 +
+
+
+
+
+
+ 30 +
+
+
+
+
+
+ 31 +
+
+
+
+
+
+ 1 +
+
+
+
+
+
+ 2 +
+
+
+
+
+
+ 3 +
+
+
+
+
+
+ 4 +
+
+
+
+
+
+ 5 +
+
+
+
+
+
+ + + + diff --git a/templates/formulaire.html b/app/templates/formulaire.html similarity index 100% rename from templates/formulaire.html rename to app/templates/formulaire.html diff --git a/templates/resultat.html b/app/templates/resultat.html similarity index 100% rename from templates/resultat.html rename to app/templates/resultat.html diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 27fee6a..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,21 +0,0 @@ -#version: '3' - -services: - db: - image: postgres:13.1 - container_name: avsa_form_db - restart: always - volumes: - - ./postgresql:/var/lib/postgresql/data - env_file: - - db.env - networks: - - avsa_form - - flask: - image: python:3.11.8-alpine - -networks: - avsa_form: - external: true - #reverse-proxy: diff --git a/hello.py b/hello.py deleted file mode 100644 index ee751db..0000000 --- a/hello.py +++ /dev/null @@ -1,85 +0,0 @@ -from datetime import datetime - -from urllib.parse import quote_plus -from sqlalchemy import create_engine, text - -from flask import Flask -from flask import request -from flask import render_template - -DB_USER = 'avsa_form' -DB_PASSWORD = '#4gvAwnUr5@MuZk9cYb!' -DB_HOST = 'avsa_form_db' -DB_PORT = '5432' -DB_NAME = 'avsa_form_db' -DB_TABLE = 'accounts' - -class user_data: - def __init__(self, first_name, last_name, phone_number, request_at, start_availability, end_availability, user_type): - self.first_name = first_name - self.last_name = last_name - self.phone_number = phone_number - self.request_at = request_at - self.start_availability = start_availability - self.end_availability = end_availability - self.user_type = user_type - -# new_data = current_data('lucas','royer','0612345678','2011-05-16 15:36:38','2011-05-16 15:36:38','2011-05-16 15:36:38','benevole') - -def insert_db(current_data): - # Connect to DB - engine = create_engine(f"postgresql+psycopg2://{DB_USER}:%s@{DB_HOST}:{DB_PORT}/{DB_NAME}" % quote_plus(DB_PASSWORD)) - conn = engine.connect() - - - - data = { - 'first_name': current_data.first_name, - 'last_name': current_data.last_name, - 'phone_number': current_data.phone_number, - 'request_at': current_data.request_at, - 'start_availability': current_data.start_availability, - 'end_availability': current_data.end_availability, - 'user_type': current_data.user_type, - } - - # SQL query - query=text(f"INSERT INTO {DB_TABLE} (first_name, last_name, phone_number, request_at, start_availability, end_availability, user_type) VALUES (:first_name, :last_name, :phone_number, :request_at, :start_availability, :end_availability, :user_type);") - - conn.execute(query, data) - conn.commit() - -app = Flask(__name__) - -@app.route("/") -def accueil(): - return render_template('accueil.html') - -@app.route('/formulaire', methods=['GET']) -def formulaire(): - return render_template('formulaire.html', retry=False) - -@app.route('/resultat', methods=['POST']) -def resultat(): - if request.method == 'POST': - first_name = request.form['first_name'] - last_name = request.form['last_name'] - phone_number = request.form['phone_number'] - request_at = datetime.now().strftime("%m-%d-%Y %H:%M:%S") - - availability_date = request.form['availability_date'] - start_availability_h = request.form['start_availability_h'] - start_availability_m = request.form['start_availability_m'] - end_availability_h = request.form['end_availability_h'] - end_availability_m = request.form['end_availability_m'] - - start_availability = f'{availability_date} {start_availability_h}:{start_availability_m}:00' - end_availability = f'{availability_date} {end_availability_h}:{end_availability_m}:00' - user_type = request.form['user_type'] - current_data = user_data(first_name, last_name, phone_number, request_at, start_availability, end_availability, user_type) - - if any(value is None for value in current_data.__dict__.values()): - return render_template('formulaire.html', retry=True) - else: - insert_db(current_data) - return render_template('resultat.html', first_name=first_name, last_name=last_name, phone_number=phone_number, request_at=request_at, start_availability_h=start_availability_h, start_availability_m=start_availability_m, end_availability_h=end_availability_h, end_availability_m=end_availability_m, user_type=current_data.user_type) diff --git a/production/Dockerfile b/production/Dockerfile new file mode 100644 index 0000000..7e40144 --- /dev/null +++ b/production/Dockerfile @@ -0,0 +1,6 @@ +FROM python:3.11.8-alpine + +# Prod depedencies +RUN pip install flask psycopg2-binary sqlalchemy waitress + +WORKDIR /app \ No newline at end of file diff --git a/db.env b/production/app-public.env similarity index 55% rename from db.env rename to production/app-public.env index af3180c..41bd3cf 100644 --- a/db.env +++ b/production/app-public.env @@ -1,3 +1,3 @@ POSTGRES_USER=avsa_form -POSTGRES_PASSWORD=#4gvAwnUr5@MuZk9cYb! +POSTGRES_PASSWORD= POSTGRES_DB=avsa_form_db \ No newline at end of file diff --git a/create_table.sql b/production/create_table.sql similarity index 100% rename from create_table.sql rename to production/create_table.sql diff --git a/production/db-public.env b/production/db-public.env new file mode 100644 index 0000000..41bd3cf --- /dev/null +++ b/production/db-public.env @@ -0,0 +1,3 @@ +POSTGRES_USER=avsa_form +POSTGRES_PASSWORD= +POSTGRES_DB=avsa_form_db \ No newline at end of file diff --git a/production/docker-compose.yml b/production/docker-compose.yml new file mode 100644 index 0000000..e62bfd0 --- /dev/null +++ b/production/docker-compose.yml @@ -0,0 +1,36 @@ +services: + app: + context: . + dockerfile: Dockerfile + + container_name: avsa_form_app + restart: always + + volumes: + - ../app:/app + + command: waitress-serve --call 'hello:create_app' + + env_file: + - app.env + - db.env + + networks: + - reverse-proxy + - avsa_form + + db: + image: postgres:13.1 + container_name: avsa_form_db + restart: always + volumes: + - ./postgresql:/var/lib/postgresql/data + env_file: + - db.env + networks: + - avsa_form + +networks: + avsa_form: + reverse-proxy: + external: true \ No newline at end of file