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
|
"""
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/<module>")
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-<pk>-from-<table>", 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-<item>", 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")
|