printedparts stage 3: read API + list page (first visible win)
GET /items (paginated, search across code/name/description/bin, lowstock filter) and GET /items/<id> with recent transactions, both open reads. printedpartsApi client, router file repointed at the renamed views, PrintedItemsList with image thumbs and a red/green quantity badge against the per-item threshold. Nav entry '3D Parts' with a new 'box' Lucide icon mapping (the sidebar renders nothing for unknown icon names - lab gotcha).
This commit is contained in:
@@ -1,18 +1,66 @@
|
||||
"""Printedparts plugin API routes.
|
||||
|
||||
Stage 2 placeholder: the blueprint must import cleanly for plugin discovery
|
||||
and migrations (the alembic env imports the models package, which pulls in
|
||||
plugin.py and this module). Real endpoints land in the next stage.
|
||||
Reads are open (jwt optional) like every list surface; mutations arrive in
|
||||
later stages with permission gates. The kiosk endpoints (unauthenticated by
|
||||
explicit decision - see the proposal) also land later.
|
||||
"""
|
||||
|
||||
from flask import Blueprint
|
||||
from flask import Blueprint, request
|
||||
from flask_jwt_extended import jwt_required
|
||||
from sqlalchemy import or_
|
||||
|
||||
from shopdb.api import success_response
|
||||
from shopdb.api import (
|
||||
db,
|
||||
success_response,
|
||||
error_response,
|
||||
paginated_response,
|
||||
ErrorCodes,
|
||||
get_pagination_params,
|
||||
paginate_query,
|
||||
)
|
||||
|
||||
from ..models import PrintedItem
|
||||
|
||||
printedparts_bp = Blueprint('printedparts', __name__)
|
||||
|
||||
|
||||
@printedparts_bp.route('/ping', methods=['GET'])
|
||||
def ping():
|
||||
"""Liveness probe for the lab: proves the blueprint is registered."""
|
||||
return success_response({'plugin': 'printedparts', 'status': 'ok'})
|
||||
@printedparts_bp.route('/items', methods=['GET'])
|
||||
@jwt_required(optional=True)
|
||||
def list_items():
|
||||
"""List printed items, paginated; search + low-stock filter."""
|
||||
page, per_page = get_pagination_params(request)
|
||||
query = PrintedItem.query
|
||||
if request.args.get('active', 'true').lower() != 'false':
|
||||
query = query.filter(PrintedItem.isactive == True)
|
||||
if search := request.args.get('search'):
|
||||
like = f'%{search}%'
|
||||
query = query.filter(or_(
|
||||
PrintedItem.itemcode.ilike(like),
|
||||
PrintedItem.itemname.ilike(like),
|
||||
PrintedItem.itemdescription.ilike(like),
|
||||
PrintedItem.binlocation.ilike(like),
|
||||
))
|
||||
if request.args.get('lowstock', '').lower() == 'true':
|
||||
query = query.filter(
|
||||
PrintedItem.quantityonhand <= PrintedItem.lowstockthreshold)
|
||||
query = query.order_by(PrintedItem.itemname)
|
||||
items, total = paginate_query(query, page, per_page)
|
||||
return paginated_response(
|
||||
[item.to_dict() for item in items], page, per_page, total)
|
||||
|
||||
|
||||
@printedparts_bp.route('/items/<int:item_id>', methods=['GET'])
|
||||
@jwt_required(optional=True)
|
||||
def get_item(item_id: int):
|
||||
"""Get one printed item with its recent transactions."""
|
||||
item = db.session.get(PrintedItem, item_id)
|
||||
if not item:
|
||||
return error_response(ErrorCodes.NOT_FOUND,
|
||||
f'Printed item {item_id} not found',
|
||||
http_code=404)
|
||||
data = item.to_dict()
|
||||
recent = (item.transactions
|
||||
.order_by(db.desc('transactiondate'))
|
||||
.limit(25).all())
|
||||
data['recenttransactions'] = [t.to_dict() for t in recent]
|
||||
return success_response(data)
|
||||
|
||||
Reference in New Issue
Block a user