WIP on registration modifications.
This commit is contained in:
parent
cc4bdafa0b
commit
cb74511677
5 changed files with 115 additions and 48 deletions
|
|
@ -11,6 +11,7 @@ from crudkit.integrations.flask import init_app
|
|||
from .config import DevConfig
|
||||
|
||||
from .routes.index import init_index_routes
|
||||
from .routes.listing import init_listing_routes
|
||||
|
||||
def create_app(config_cls=DevConfig) -> Flask:
|
||||
app = Flask(__name__)
|
||||
|
|
@ -53,6 +54,7 @@ def create_app(config_cls=DevConfig) -> Flask:
|
|||
app.register_blueprint(bp_reports)
|
||||
|
||||
init_index_routes(app)
|
||||
init_listing_routes(app)
|
||||
|
||||
@app.teardown_appcontext
|
||||
def _remove_session(_exc):
|
||||
|
|
|
|||
|
|
@ -16,54 +16,6 @@ from ..models.work_log import WorkLog
|
|||
bp_index = Blueprint("index", __name__)
|
||||
|
||||
def init_index_routes(app):
|
||||
|
||||
@bp_index.get("/api/pivot/inventory")
|
||||
def pivot_inventory():
|
||||
session = get_session()
|
||||
# qs params: rows, cols, where_status, top_n
|
||||
rows = request.args.get("rows", "device_type.description")
|
||||
cols = request.args.get("cols", "condition")
|
||||
top_n = int(request.args.get("top_n", "40"))
|
||||
|
||||
# Map friendly names to columns
|
||||
COLS = {
|
||||
"device_type.description": DeviceType.description.label("row"),
|
||||
"condition": Inventory.condition.label("row"),
|
||||
"status": Inventory.condition.label("row"), # alias if you use 'status' in UI
|
||||
}
|
||||
ROW = COLS.get(rows)
|
||||
COL = Inventory.condition.label("col") if cols == "condition" else DeviceType.description.label("col")
|
||||
|
||||
stmt = (
|
||||
select(ROW, COL, func.count(Inventory.id).label("n"))
|
||||
.select_from(Inventory).join(DeviceType, Inventory.type_id == DeviceType.id)
|
||||
.group_by(ROW, COL)
|
||||
)
|
||||
data = session.execute(stmt).all() # [(row, col, n), ...]
|
||||
|
||||
# reshape into Chart.js: labels = sorted rows by total, datasets per column value
|
||||
import pandas as pd
|
||||
df = pd.DataFrame(data, columns=["row", "col", "n"])
|
||||
if df.empty:
|
||||
return jsonify({"labels": [], "datasets": []})
|
||||
|
||||
totals = df.groupby("row")["n"].sum().sort_values(ascending=False)
|
||||
keep_rows = totals.head(top_n).index.tolist()
|
||||
df = df[df["row"].isin(keep_rows)]
|
||||
|
||||
labels = keep_rows
|
||||
by_col = df.pivot_table(index="row", columns="col", values="n", aggfunc="sum", fill_value=0)
|
||||
by_col = by_col.reindex(labels) # row order
|
||||
|
||||
payload = {
|
||||
"labels": labels,
|
||||
"datasets": [
|
||||
{"label": str(col), "data": by_col[col].astype(int).tolist()}
|
||||
for col in by_col.columns
|
||||
]
|
||||
}
|
||||
return jsonify(payload)
|
||||
|
||||
@bp_index.get("/")
|
||||
def index():
|
||||
session = get_session()
|
||||
|
|
|
|||
13
inventory/routes/listing.py
Normal file
13
inventory/routes/listing.py
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
from flask import Blueprint, render_template
|
||||
|
||||
from ..db import get_session
|
||||
|
||||
bp_listing = Blueprint("listing", __name__)
|
||||
|
||||
def init_listing_routes(app):
|
||||
@bp_listing.get("/listing/<model>")
|
||||
def show_list(model):
|
||||
session = get_session()
|
||||
return render_template("listing.html", model=model)
|
||||
|
||||
app.register_blueprint(bp_listing)
|
||||
5
inventory/templates/listing.html
Normal file
5
inventory/templates/listing.html
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
{% extends 'base.html' %}
|
||||
|
||||
{% block main %}
|
||||
Feelin' fine.
|
||||
{% endblock %}
|
||||
Loading…
Add table
Add a link
Reference in a new issue