-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
133 lines (100 loc) · 3.04 KB
/
app.py
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
from __future__ import annotations
from functools import partial
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
from htmx_components_flask import htmx_components_flask
from htmx_components_python import GridConfig, TreeConfig
from jinja2 import StrictUndefined
from sqlalchemy import Table, asc, desc, func, select, text
from form import KitchenSink
app = Flask(__name__)
app.register_blueprint(htmx_components_flask)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///northwind.db"
app.config["SECRET_KEY"] = "s3cr3t"
db = SQLAlchemy()
db.init_app(app)
with app.app_context():
db.reflect()
customers_table = db.metadata.tables["Customers_FTS"]
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True
app.jinja_env.undefined = StrictUndefined
@app.get("/")
def index():
return render_template("index.html")
@app.route("/gridview", methods=["GET", "POST"])
def viewgrid():
grid1 = GridConfig(
columns=[
"CustomerID",
"CompanyName",
"ContactName",
"ContactTitle",
"Region",
]
)
grid1.get_records = partial(get_records, customers_table)
return render_template("gridview.html", grid_config=grid1)
@app.route("/form", methods=["GET", "POST"])
def form():
form = KitchenSink(request.form)
form.validate_on_submit()
return render_template("form.html", form=form)
@app.route("/treeview", methods=["GET", "POST"])
def tree():
tree = TreeConfig(
data=get_georegions_eager,
)
return render_template("treeview.html", tree=tree)
def get_georegions_eager(q: str | None = None):
query = """
with recursive exp1 as (
select
id,
name,
parent_id
from georegions
where
name like :q
union
select
parent.id,
parent.name,
parent.parent_id
from georegions as parent
inner join exp1 as child on child.parent_id = parent.id
order by
name asc
)
select * from exp1
"""
cursor_result = db.session.execute(text(query), {"q": f"%{q}%"})
result = []
for row in cursor_result:
result.append(row._mapping)
return result
def get_records(
table: Table,
page: int = 1,
page_size: int = 20,
sorts: list[tuple[str, str]] | None = None,
q: str = "",
):
if not sorts:
sorts = []
query = select(table)
count_query = select(func.count("*")).select_from(table)
where_clause = text(f"{table.name} match :q")
if q:
query = query.filter(where_clause)
count_query = count_query.filter(where_clause)
for sort in sorts:
attr = table.c[sort[0]]
query = query.order_by(desc(attr) if sort[1] == "desc" else asc(attr))
query = query.limit(page_size).offset(page_size * (page - 1))
cursor_result = db.session.execute(query, {"q": f"{q}"})
result = []
for row in cursor_result:
result.append(row._mapping)
total = db.session.scalar(count_query, {"q": f"{q}"})
return result, (total // page_size) + 1