View raw

1 # -*- mode: python; -*- 2 3 4 from flask import ( 5 Blueprint, 6 render_template, 7 send_file, 8 request, 9 redirect, 10 flash, 11 url_for, 12 jsonify, 13 abort, 14 ) 15 from flask_login import login_required, current_user 16 17 18 # from datetime import datetime 19 import inspect 20 21 from . import db 22 23 modules = Blueprint("modules", __name__) 24 25 26 from .models import * 27 from .forms import * 28 29 30 @modules.route("/modules") 31 def modules_home(): 32 return redirect("/index") 33 34 35 @modules.route("/add-<item>", methods=["GET", "POST"]) 36 @login_required 37 def add_item(item): 38 """Add a new item to a corresponding database table. 39 40 The item must match a database model class name (table). Then, we 41 match the model class attributes with request.form values. 42 43 """ 44 print("db table keys are", db.metadata.tables.keys()) 45 if item.capitalize() not in db.metadata.tables.keys(): 46 return render_template("errors/item-not-found.html", item=item) 47 if request.method == "GET": 48 form = globals()[f"Add{item.capitalize()}"]() 49 return render_template("add-item.html", item=item, form=form) 50 if request.method == "POST": 51 table = globals()[item.capitalize()] 52 table_fields = inspect.signature(table).parameters 53 form_values = {key: request.form[key] for key in table_fields} 54 print(f"Ready to insert {form_values}") 55 record = table(**form_values) 56 db.session.add(record) 57 db.session.commit() 58 item_pk = table.query.order_by(table.primary_key.desc()).first().primary_key 59 flash( 60 f"Successfully added item #{item_pk} to {table.__table__.name.capitalize()} table.", 61 "info", 62 ) 63 return redirect("/modules") 64 65 66 @modules.route("/delete-<pk>-from-<table>", methods=["POST"]) 67 @login_required 68 def delete_item(pk, table): 69 """Delete item with Primary Key = pk from corresponding database 70 table. 71 """ 72 model = globals()[table] 73 record = model.query.filter_by(primary_key=pk).first() 74 db.session.delete(record) 75 db.session.commit() 76 flash(f"Successfully removed item #{pk} from {table} table.", "info") 77 return redirect("/modules") 78 79 80 @modules.route("/modules/customers") 81 @login_required 82 def customers(): 83 modules = Module.query.all() 84 customers = Customer.query.order_by(Customer.primary_key.desc()) 85 module = "customers" 86 flash(f"Successfully accessed module {module}.", "info") 87 flash(f"Still fighting against styling in {module}.", "error") 88 return render_template( 89 f"modules/{module}.html", 90 modules=modules, 91 customers=customers, 92 ) 93 94 95 @modules.route("/modules/products") 96 @login_required 97 def products(): 98 modules = Module.query.all() 99 products = Product.query.order_by(Product.primary_key.desc()) 100 module = "products" 101 flash(f"Successfully accessed module {module}.", "info") 102 flash(f"Still fighting against styling in {module}.", "error") 103 return render_template( 104 f"modules/{module}.html", 105 modules=modules, 106 products=products, 107 ) 108 109 110 @modules.route("/modules/ferti") 111 @login_required 112 def ferti(): 113 modules = Module.query.all() 114 logs = Fertilog.query.order_by(Fertilog.primary_key.desc()).all() 115 targets = Fertitarget.query.all() 116 module = "ferti" 117 flash(f"Latest target is {targets}.", "info") 118 flash(f"Still fighting against styling in {module}.", "error") 119 return render_template( 120 f"modules/{module}.html", 121 modules=modules, 122 logs=logs, 123 ) 124 125 126 # @main.route("/add-invoice", methods=["GET", "POST"]) 127 # def add_invoice(): 128 # form = AddInvoice() 129 # if request.method == "GET": 130 # return render_template("add-invoice.html") 131 132 133 # @main.route("/preview-invoice", methods=["GET", "POST"]) 134 # def preview_invoice(): 135 # if request.method == "GET": 136 # return render_template("preview-invoice.html") 137