From a21efb991e118d7cc1ba7d3ed7496713af856e2d Mon Sep 17 00:00:00 2001 From: Andrew Ridgway Date: Wed, 27 Mar 2024 15:53:02 +1000 Subject: [PATCH] first pass and Flask-WTF --- requirements.txt | 2 ++ src/flask/pool_data.py | 57 ++++++++++++++++++++++-------- src/flask/templates/index.html | 38 ++++++++++++++++---- src/flask/templates/updater.html | 59 ++++++++++---------------------- 4 files changed, 94 insertions(+), 62 deletions(-) diff --git a/requirements.txt b/requirements.txt index 030807e..b987434 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,5 @@ flask requests_html beautifulsoup4 click +Flask-WTF +bootstrap-flask diff --git a/src/flask/pool_data.py b/src/flask/pool_data.py index 2f9ebe6..139b34f 100644 --- a/src/flask/pool_data.py +++ b/src/flask/pool_data.py @@ -2,37 +2,64 @@ import mongo.build_db as pool_database import mongo.query_db as pool_database_query from flask import Flask, render_template, request, jsonify, redirect +from flask_bootstrap import Bootstrap5 +from wtforms import StringField, SubmitField +from wtforms.validators import DataRequired, Length + app = Flask(__name__) +app.secret_key = 'testsecret' #this value will change +bootstrap = Bootstrap5(app) + +csrf = CSRFProtect(app) + +class userForm(FlaskForm): + username = StringField("User Name?", validators=[DataRequired()]) + password = PasswordField("Password?", validators[DataRequired()]) + submit = SubmitField("Letsa GO!") + +class dataForm(FlaskForm): + test_user = RadioField("Tester:", + choices=[("Isabella"), ("Heather"), ("Ariah")]) + Date = DateField("Date:") + PH = DecimalField("PH:") + total_chlorine = IntegerField("Total Chlorine:") + free_chlorine = IntegerField("Free Chlorine:") + alkalinity = DecimalField("Alkalinity:") + salt = IntegerField("Salt:") + submit = SubmitField("Write it, Write it REAAAAAAL GOOOD") + @app.route("/", methods=["GET","POST"]) def index(): - if request.method == "POST": - username = request.form["username"] - password = request.form["password"] + form = userForm() + if form.validate_on_submit() + username = form.username.data + password = form.password.data db = pool_database_query.pool_query() if db.user_check(username, password): return redirect("/updater") else: - return render_template("index.html", try_again=True) + return render_template("index.html", try_again=True, form=form) else: - return render_template("index.html", try_again=False) + return render_template("index.html", try_again=False, form=form) @app.route("/updater", methods=["GET", "POST"]) def updater(): query_db = pool_database_query.pool_query() query = query_db.get_top(10, "ph") + form = dataForm() - if request.method == "POST": + if form.validate_on_submit() database = pool_database.pool_data() new_record = { - "ph": f'{request.form["ph"]}', - "total_chlorine" : f'{request.form["total_chlorine"]}', - "free_chlorine": f'{request.form["free_chlorine"]}', - "alkalinity": f'{request.form["alkalinity"]}', - "salt": f'{request.form["salt"]}', - "date": f'{request.form["date"]}', - "test_user": f'{request.form["test_user"]}', + "ph": f'{form.PH.data}', + "total_chlorine" : f'{form.total_chlorine.data}', + "free_chlorine": f'{form.free_chlorine.data}', + "alkalinity": f'{form.alkalinity.data}', + "salt": f'{form.salt.data}', + "date": f'{form.Date.data}', + "test_user": f'{form.test_user.data}', } if database.record_exists(new_record["date"], new_record["test_user"]): for field in database.existing_record: @@ -45,9 +72,9 @@ def updater(): new_record["alkalinity"], new_record["date"], new_record["test_user"], new_record["salt"]) - return render_template("updater.html", list=query) + return render_template("updater.html", list=query, form=form) else: - return render_template("updater.html", list=query) + return render_template("updater.html", list=query, form=form) @app.route("/update_db", methods=["POST"]) def pool_data_update(): diff --git a/src/flask/templates/index.html b/src/flask/templates/index.html index 95490ec..244597f 100644 --- a/src/flask/templates/index.html +++ b/src/flask/templates/index.html @@ -1,11 +1,37 @@ +{% extends 'base.html' %} +{% from 'bootstrap5/form.html' import render_form %} + +{% block title %} +Login to Pool Data Tracker +{% endblock %} +{% block content %} + + +
-

Login to Pool Data Tracker

{% if try_again %}

Login Failed Please try Again

{% endif %} -
- - - -
+ +
+
+
+ +

WHOOOOOOO ARE YOU!

+ +

If you know you know

+ + {{ render_form(form) }} + +

{{ message }}

+ +
+
+
+ +{% endblock %} diff --git a/src/flask/templates/updater.html b/src/flask/templates/updater.html index 6012147..e871649 100644 --- a/src/flask/templates/updater.html +++ b/src/flask/templates/updater.html @@ -1,49 +1,25 @@ -

Data Input

+{% extends 'base.html' %} +{% from 'bootstrap5/form.html' import render_form %} + +{% block title %} + Data Input +{% endblock %} +{% block content %} + + +
Input Data you want to store
- +
( + {{ render_form(form) }} +
-
-
- -
- -
- -
- -

-
-
-
-
-

-
-
-
-

-
-
-
-

-
-
-
-

-
-
-
-

-
-
-
-

-
- -
-
{% for row in list %} @@ -61,3 +37,4 @@ +{% endblock %}