1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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-<item>", 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-<pk>-from-<table>", 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")
|