Add filtering functionality to inventory listing and enhance templates for better data presentation
This commit is contained in:
parent
a7708ce9c5
commit
5df5f86fd2
8 changed files with 64 additions and 9 deletions
48
routes.py
48
routes.py
|
@ -4,7 +4,7 @@ import html
|
|||
from sqlalchemy.orm import joinedload
|
||||
from typing import Callable, Any, List
|
||||
from . import db
|
||||
from .utils import eager_load_user_relationships, eager_load_inventory_relationships, eager_load_room_relationships, eager_load_worklog_relationships
|
||||
from .utils import eager_load_user_relationships, eager_load_inventory_relationships, eager_load_room_relationships, eager_load_worklog_relationships, chunk_list
|
||||
|
||||
main = Blueprint('main', __name__)
|
||||
|
||||
|
@ -106,7 +106,8 @@ def render_paginated_table(
|
|||
entry_route: str,
|
||||
row_fn: Callable[[Any], List[dict]],
|
||||
endpoint: str,
|
||||
per_page=15
|
||||
per_page=15,
|
||||
extra_args={}
|
||||
):
|
||||
data = make_paginated_data(query, page, per_page)
|
||||
return render_template(
|
||||
|
@ -120,7 +121,8 @@ def render_paginated_table(
|
|||
endpoint=endpoint,
|
||||
total_pages=data['total_pages'],
|
||||
headers=headers,
|
||||
entry_route=entry_route
|
||||
entry_route=entry_route,
|
||||
extra_args=extra_args
|
||||
)
|
||||
|
||||
@main.route("/")
|
||||
|
@ -130,24 +132,56 @@ def index():
|
|||
def link(text, endpoint, **values):
|
||||
return {"text": text, "url": url_for(endpoint, **values)}
|
||||
|
||||
FILTER_MAP = {
|
||||
'user': Inventory.owner_id,
|
||||
'room': Inventory.location_id,
|
||||
'brand': Inventory.brand_id,
|
||||
}
|
||||
|
||||
@main.route("/inventory")
|
||||
def list_inventory():
|
||||
page = request.args.get('page', default=1, type=int)
|
||||
query = eager_load_inventory_relationships(db.session.query(Inventory)).order_by(Inventory.inventory_name, Inventory.barcode, Inventory.serial)
|
||||
filter_by = request.args.get('filter_by', type=str)
|
||||
id = request.args.get('id', type=int)
|
||||
|
||||
query = db.session.query(Inventory)
|
||||
query = eager_load_inventory_relationships(query)
|
||||
query = query.order_by(Inventory.inventory_name, Inventory.barcode, Inventory.serial)
|
||||
|
||||
if filter_by and id:
|
||||
column = FILTER_MAP.get(filter_by)
|
||||
if column is not None:
|
||||
query = query.filter(column == id)
|
||||
else:
|
||||
return "Invalid filter_by parameter", 400
|
||||
|
||||
return render_paginated_table(
|
||||
query=query,
|
||||
page=page,
|
||||
title="Inventory",
|
||||
title="Inventory (Filtered)" if filter_by else "Inventory",
|
||||
headers=inventory_headers,
|
||||
row_fn=lambda i: [fn(i) for fn in inventory_headers.values()],
|
||||
endpoint="main.list_inventory",
|
||||
entry_route="inventory_item"
|
||||
entry_route="inventory_item",
|
||||
extra_args={'filter_by': filter_by, 'id': id}
|
||||
)
|
||||
|
||||
@main.route("/inventory/index")
|
||||
def inventory_index():
|
||||
category = request.args.get('category')
|
||||
return render_template('inventory_index.html', title="Inventory Index", category=category)
|
||||
listing = None
|
||||
|
||||
if category == 'user':
|
||||
users = db.session.query(User.id, User.first_name, User.last_name).order_by(User.first_name, User.last_name).all()
|
||||
listing = chunk_list([(user.id, f"{user.first_name or ''} {user.last_name or ''}".strip()) for user in users], 12)
|
||||
elif category == 'location':
|
||||
pass
|
||||
elif category == 'type':
|
||||
pass
|
||||
elif category:
|
||||
return f"Dude, why {category}?"
|
||||
|
||||
return render_template('inventory_index.html', title="Inventory Index", category=category, listing=listing)
|
||||
|
||||
@main.route("/inventory_item/<int:id>")
|
||||
def inventory_item(id):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue