From e084f90c19e779592f6e3f14eb8f70a7287f0dad Mon Sep 17 00:00:00 2001 From: Marius Peter Date: Sun, 15 May 2022 13:17:35 +0200 Subject: Modularization. --- app/modules/modules.py.bkp | 136 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 app/modules/modules.py.bkp (limited to 'app/modules/modules.py.bkp') diff --git a/app/modules/modules.py.bkp b/app/modules/modules.py.bkp new file mode 100644 index 0000000..b962a0f --- /dev/null +++ b/app/modules/modules.py.bkp @@ -0,0 +1,136 @@ +# -*- mode: python; -*- + + +from flask import ( + Blueprint, + render_template, + send_file, + request, + redirect, + flash, + url_for, + jsonify, + abort, +) +from flask_login import login_required, current_user + + +# from datetime import datetime +import inspect + +from . import db + +modules = Blueprint("modules", __name__) + + +from .models import * +from .forms import * + + +@modules.route("/modules") +def modules_home(): + return redirect("/index") + + +@modules.route("/add-", methods=["GET", "POST"]) +@login_required +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. + + """ + print("db table keys are", db.metadata.tables.keys()) + if item.capitalize() 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 = {key: request.form[key] for key in table_fields} + print(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("/modules") + + +@modules.route("/delete--from-", methods=["POST"]) +@login_required +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") + + +@modules.route("/modules/customers") +@login_required +def customers(): + modules = Module.query.all() + customers = Customer.query.order_by(Customer.primary_key.desc()) + module = "customers" + flash(f"Successfully accessed module {module}.", "info") + flash(f"Still fighting against styling in {module}.", "error") + return render_template( + f"modules/{module}.html", + modules=modules, + customers=customers, + ) + + +@modules.route("/modules/products") +@login_required +def products(): + modules = Module.query.all() + products = Product.query.order_by(Product.primary_key.desc()) + module = "products" + flash(f"Successfully accessed module {module}.", "info") + flash(f"Still fighting against styling in {module}.", "error") + return render_template( + f"modules/{module}.html", + modules=modules, + products=products, + ) + + +@modules.route("/modules/ferti") +@login_required +def ferti(): + modules = Module.query.all() + logs = Fertilog.query.order_by(Fertilog.primary_key.desc()).all() + targets = Fertitarget.query.all() + module = "ferti" + flash(f"Latest target is {targets}.", "info") + flash(f"Still fighting against styling in {module}.", "error") + return render_template( + f"modules/{module}.html", + modules=modules, + logs=logs, + ) + + +# @main.route("/add-invoice", methods=["GET", "POST"]) +# def add_invoice(): +# form = AddInvoice() +# if request.method == "GET": +# return render_template("add-invoice.html") + + +# @main.route("/preview-invoice", methods=["GET", "POST"]) +# def preview_invoice(): +# if request.method == "GET": +# return render_template("preview-invoice.html") -- cgit v1.2.3