# -*- 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")