From fd1f3fba182efb1ff154dc24f2553601b28791fb Mon Sep 17 00:00:00 2001 From: blendoit Date: Sat, 25 Jan 2020 11:47:18 -0800 Subject: Python module documentation and start work on SQL db. --- Pipfile | 1 + Pipfile.lock | 28 +++++++++++++++++++++------- app/routes.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 7 deletions(-) diff --git a/Pipfile b/Pipfile index e28b64e..a220484 100644 --- a/Pipfile +++ b/Pipfile @@ -10,6 +10,7 @@ flask = "*" flask-wtf = "*" uwsgi = "*" flask-socketio = "*" +flask-sqlalchemy = "*" [requires] python_version = "3.8" diff --git a/Pipfile.lock b/Pipfile.lock index 1e6bd41..0b33a96 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "efc56d845e2a3dfa2fa6132963f17abed9e32f9efdc80d03898efdcd1c3d07a3" + "sha256": "28f4a6fb6e08e2b08c8f9dd44c53ab851efd6b6883afe810ae0222d73176abf3" }, "pipfile-spec": 6, "requires": { @@ -39,6 +39,14 @@ "index": "pypi", "version": "==4.2.1" }, + "flask-sqlalchemy": { + "hashes": [ + "sha256:0078d8663330dc05a74bc72b3b6ddc441b9a744e2f56fe60af1a5bfc81334327", + "sha256:6974785d913666587949f7c2946f7001e4fa2cb2d19f4e69ead02e4b8f50b33d" + ], + "index": "pypi", + "version": "==2.4.1" + }, "flask-wtf": { "hashes": [ "sha256:5d14d55cfd35f613d99ee7cba0fc3fbbe63ba02f544d349158c14ca15561cc36", @@ -96,10 +104,10 @@ }, "python-engineio": { "hashes": [ - "sha256:50d108fc7feb7f970e6ebc86733752ebd714545bb5622383e6135bdad45fc9fe", - "sha256:f6bd466dea769bb19c59eaf8e68a296b195b6957650523f13a73d8c0767b7a38" + "sha256:47ae4a9b3b4f2e8a68929f37a518338838e119f24c9a9121af92c49f8bea55c3", + "sha256:c3a3822deb51fdf9c7fe4d78abf807c73b83ea538036a50862d3024450746253" ], - "version": "==3.11.1" + "version": "==3.11.2" }, "python-socketio": { "hashes": [ @@ -110,10 +118,16 @@ }, "six": { "hashes": [ - "sha256:1f1b7d42e254082a9db6279deae68afb421ceba6158efa6131de7b3003ee93fd", - "sha256:30f610279e8b2578cab6db20741130331735c781b56053c59c4076da27f06b66" + "sha256:236bdbdce46e6e6a3d61a337c0f8b763ca1e8717c03b369e87a7ec7ce1319c0a", + "sha256:8f3cd2e254d8f793e7f3d6d9df77b92252b52637291d0f0da013c76ea2724b6c" + ], + "version": "==1.14.0" + }, + "sqlalchemy": { + "hashes": [ + "sha256:64a7b71846db6423807e96820993fa12a03b89127d278290ca25c0b11ed7b4fb" ], - "version": "==1.13.0" + "version": "==1.3.13" }, "uwsgi": { "hashes": [ diff --git a/app/routes.py b/app/routes.py index 4604827..6c7059a 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,33 +1,60 @@ +""" +routes.py module +---------------- + +This Python module contains the logic supporting: +1. Navigating between website pages +2. Interpreting user requests to the server +3. Dispatching requested content back to the user + +Python dependencies: +- flask: provides web application features +- forms: provides secure form user submission +- sqlalchemy: provides communication with database on server. + +Personal imports: +These are used to avoid cluttering this file with +placeholder data for posts' content. +""" from flask import Flask, render_template, flash, redirect, url_for from forms import RegistrationForm, LoginForm +from flask_sqlalchemy import SQLAlchemy + from placeholder_posts import posts from placeholder_news import news from placeholder_messages import messages app = Flask(__name__) app.config['SECRET_KEY'] = 'foobarblendoitfoobar!' +app.config['SQLALCHEMY_DATABASE_URI'] = 'mariadb:///site.db' + +db = SQLAlchemy(app) @app.route("/") @app.route("/home") def home(): + """This is our homepage.""" home_posts = posts + news + messages return render_template('home.html', posts=home_posts) @app.route("/yes") def yes(): + """Another page, presenting the user with content marked as 'yes'.""" return render_template("yes.html", messages=messages, news=news) @app.route("/no") def no(): + """This page contains an archive of 'no's, for forgiving user mistakes.""" archives = posts + news + messages return render_template("no.html", archive=archives) @app.route("/register", methods=['GET', 'POST']) def register(): + """Account registration page leveraging the form Python package.""" form = RegistrationForm() if form.validate_on_submit(): flash(f"Alias created for {form.alias.data}.", 'success') @@ -41,5 +68,12 @@ def login(): return render_template('login.html', title="Login", form=form) +# If this file is executed as a script (i.e. double-clicked), +# the Python interpreter will run the Flask process and begin serving +# the web pages on the standard localhost address (127.0.0.1). +# But if this file is called as a module by another Python script, it will not +# serve content to the web pages, but the function definitions contained in +# this file will be available to the calling script. +# E.g. calling script will know what the yes() function is. if __name__ == '__main__': app.run(debug=True) -- cgit v1.2.3