""" 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 user form 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, request, redirect, flash, url_for, jsonify from flask_bootstrap import Bootstrap from datetime import datetime import inspect from model import * from forms import * app = Flask(__name__) app.config.from_pyfile("../config.py") db.init_app(app) @app.route("/") @app.route("/fapg/home") def project(): """This is our project welcome page.""" michel = User( name_first="Michel", name_last="Peter", email="le-boss@fapg.com", phone_mobile="00000000", phone_alternative="0000000000", updated="2022-04-21", ) modules = Module.query.all() print(module.name for module in modules) return render_template("home.html", user=michel, project="fapg", modules=modules) @app.route("/modules") def all_modules(): return redirect("/") @app.route("/modules/") def render_module(module): modules = Module.query.all() if module not in [mod.name for mod in modules]: return render_template("errors/module-not-found.html", module=module) customers = Customer.query.order_by(Customer.primary_key.desc()) products = Product.query.order_by(Product.primary_key.desc()) logs = Log.query.order_by(Log.primary_key.desc()) latest_target = ( Log.query.filter_by(target="True").order_by(Log.primary_key.desc()).first() ) flash(f"Successfully accessed module {module}.", "info") flash(f"Still fighting against styling in {module}.", "error") return render_template( f"modules/{module}.html", module=module, modules=modules, customers=customers, products=products, logs=logs, target=latest_target, ) @app.route("/delete--from-", methods=["POST"]) def delete_item(pk, table): """Delete item with Primary Key = pk from corresponding database table. """ model = globals()[table] record = model.query.filter_by(primary_key=pk).first() db.session.delete(record) db.session.commit() flash(f"Successfully removed item #{pk} from {table} table.", "info") return redirect("/modules") @app.route("/add-", methods=["GET", "POST"]) def add_item(item): """Add a new item to a corresponding database table. The item must match a database model class name (table). Then, we match the model class attributes with request.form values. """ if item not in db.metadata.tables.keys(): return render_template("errors/item-not-found.html", item=item) if request.method == "GET": form = globals()[f"Add{item.capitalize()}"]() return render_template("add-item.html", item=item, form=form) if request.method == "POST": table = globals()[item.capitalize()] table_fields = inspect.signature(table).parameters form_values = [request.form[key] for key in table_fields] debug = f"Ready to insert {form_values}" record = table(*form_values) db.session.add(record) db.session.commit() item_pk = table.query.order_by(table.primary_key.desc()).first().primary_key flash( f"Successfully added item #{item_pk} to {table.__table__.name.capitalize()} table.", "info", ) return redirect(f"/modules") @app.route("/add-invoice", methods=["GET", "POST"]) def add_invoice(): form = AddInvoice() if request.method == "GET": return render_template("add-invoice.html") @app.route("/preview-invoice", methods=["GET", "POST"]) def preview_invoice(): if request.method == "GET": return render_template("preview-invoice.html")