Soem crudkit fixes aimed at eliminating session sharing.

This commit is contained in:
Yaro Kasear 2025-09-15 11:51:56 -05:00
parent d045a1a05f
commit cf795086f1
10 changed files with 107 additions and 47 deletions

View file

@ -1,15 +1,12 @@
from flask import Blueprint, current_app, jsonify, render_template, request, send_file
from flask import Blueprint, current_app, render_template, send_file
from pathlib import Path
from sqlalchemy import func, select
import pandas as pd
from crudkit.core.service import CRUDService
from crudkit.core.spec import CRUDSpec
import crudkit
from crudkit.ui.fragments import render_table
from ..db import get_session
from ..models.device_type import DeviceType
from ..models.inventory import Inventory
from ..models.work_log import WorkLog
@ -18,9 +15,8 @@ bp_index = Blueprint("index", __name__)
def init_index_routes(app):
@bp_index.get("/")
def index():
session = get_session()
inventory_service = CRUDService(Inventory, session)
work_log_service = CRUDService(WorkLog, session)
inventory_service = crudkit.crud.get_service(Inventory)
work_log_service = crudkit.crud.get_service(WorkLog)
work_logs = work_log_service.list({
"complete__ne": 1,
"fields": [

View file

@ -1,4 +1,4 @@
from flask import Blueprint, render_template, abort
from flask import Blueprint, render_template, abort, request
import crudkit
@ -9,25 +9,65 @@ bp_listing = Blueprint("listing", __name__)
def init_listing_routes(app):
@bp_listing.get("/listing/<model>")
def show_list(model):
page_num = int(request.args.get("page", 1))
if model.lower() not in {"inventory", "user", "worklog"}:
abort(404)
cls = crudkit.crud.get_model(model)
if cls is None:
abort(404)
spec = {}
columns = []
if model.lower() == 'inventory':
spec = {"fields": [
"label",
"name",
"barcode",
"serial",
"device_type.description"
"brand.name",
"model",
"device_type.description",
"condition",
"owner.label",
"location.label",
]}
spec["limit"] = 0
columns = [
{"field": "label"},
{"field": "name"},
{"field": "barcode", "label": "Barcode #"},
{"field": "serial", "label": "Serial #"},
{"field": "brand.name", "label": "Brand"},
{"field": "model"},
{"field": "device_type.description", "label": "Device Type"},
{"field": "condition"},
{"field": "owner.label", "label": "Contact", "link": {"endpoint": "user.get_item", "params": {"id": "{owner.id}"}}},
{"field": "location.label", "label": "Room"},
]
if model.lower() == 'user':
spec = {"fields": [
"label",
"last_name",
"first_name",
"supervisor.label",
"staff",
"active",
]}
columns = [
{"field": "label", "label": "Full Name"},
{"field": "last_name"},
{"field": "first_name"},
{"field": "supervisor.label", "label": "Supervisor", "link": {"endpoint": "user.get_item", "params": {"id": "{supervisor.id}"}}},
{"field": "staff"},
{"field": "active"},
]
spec["limit"] = 15
spec["offset"] = (page_num - 1) * 15
service = crudkit.crud.get_service(cls)
rows = service.list(spec)
table = render_table(rows, opts={"object_class": model})
table = render_table(rows, columns=columns, opts={"object_class": model})
return render_template("listing.html", model=model, table=table)