Make the app distributable to other GE Aerospace sites (one self-hosted
instance per site, ADR-004). GE values remain the shipped defaults; every
site-specific behavior is now a Setting an admin can change in the UI.
Settings-driven site config:
- Branding: site/QR/badge logos, favicon, primary color (upload endpoints
mirror the map-blueprint pattern; new Settings > Branding section).
- ServiceNow: search/incident/change URL templates ({ticket}), ticket
prefixes, enable toggle. Defaults point at the current
geaerospaceqa.service-now.com global search. Disabled = plain-text tickets.
- Employee-id regex (employeeid_pattern), printer hostname template,
QR label targets (qr_target_printer / qr_target_usb, blank = asset page,
else URL template with placeholders), usb_label_style (barcode|qr).
- West Jefferson floor-plan PNGs removed from the tree; generic placeholder
ships as the map default and sites upload their own blueprint.
Security closeout:
- dashboarddefaults writes now require admin.
- Collector: generic error messages (no str(exc) leak); API key accepted
via X-API-Key header only (BREAKING: querystring api_key removed).
- IP-based login rate limiting (AUTH_RATELIMIT_* knobs) atop account lockout.
- Setting.set() creation race fixed (IntegrityError retry).
Release engineering and docs:
- __version__ 0.5.0 (distinct from __contract_version__, ADR-007),
CHANGELOG.md, Gitea Actions CI config, frontend version aligned.
- One wizard-first install story across README/DEPLOY; new CONFIG.md,
UPGRADE.md, BACKUP-RESTORE.md; CLAUDE.md and ROADMAP de-staled.
- Dockerfile multi-stage build now bundles the frontend; compose binds
MySQL to 127.0.0.1; stale database/schema.sql and one-off SQL removed.
Debt and fixes:
- .query.get() -> db.session.get() sweep; datetime.utcnow() removed
(naive-UTC via timezone-aware now); users.py on authz decorators.
- Fixed 4 stale tests (slides feed shape, shopfloor splitperemployee,
plugin contract purity) and the USB label page field mapping (both usb
modes emit the cmmc shape: device_id/device_desc).
- Health endpoint reports the real version.
248 tests pass; naming/style check green; frontend builds; fresh-DB
flask db upgrade + seeds verified; QR targets verified by decoding
rendered codes.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
235 lines
7.7 KiB
Python
235 lines
7.7 KiB
Python
"""Locations API endpoints - Full CRUD."""
|
|
|
|
from flask import Blueprint, request
|
|
from flask_jwt_extended import jwt_required
|
|
|
|
from shopdb.extensions import db
|
|
from shopdb.core.models import Location, LocationType
|
|
from shopdb.utils.responses import (
|
|
success_response,
|
|
error_response,
|
|
paginated_response,
|
|
ErrorCodes
|
|
)
|
|
from shopdb.utils.pagination import get_pagination_params, paginate_query
|
|
|
|
from shopdb.utils.authz import require_permission, require_role
|
|
|
|
locations_bp = Blueprint('locations', __name__)
|
|
|
|
|
|
def _loc_type_dict(t):
|
|
return {
|
|
'locationtypeid': t.locationtypeid,
|
|
'locationtype': t.locationtype,
|
|
'description': t.description,
|
|
'color': t.color,
|
|
'isactive': t.isactive,
|
|
}
|
|
|
|
|
|
@locations_bp.route('/types', methods=['GET'])
|
|
@jwt_required(optional=True)
|
|
def list_location_types():
|
|
"""List location types. ?active=false includes inactive ones."""
|
|
query = LocationType.query
|
|
if request.args.get('active', 'true').lower() != 'false':
|
|
query = query.filter_by(isactive=True)
|
|
types = query.order_by(LocationType.locationtype).all()
|
|
return success_response([_loc_type_dict(t) for t in types])
|
|
|
|
|
|
@locations_bp.route('/types', methods=['POST'])
|
|
@jwt_required()
|
|
@require_role('admin')
|
|
def create_location_type():
|
|
data = request.get_json() or {}
|
|
if not data.get('locationtype'):
|
|
return error_response(ErrorCodes.VALIDATION_ERROR, 'locationtype is required')
|
|
existing = LocationType.query.filter_by(locationtype=data['locationtype']).first()
|
|
if existing:
|
|
if not existing.isactive:
|
|
existing.isactive = True
|
|
for key in ('description', 'color'):
|
|
if data.get(key) is not None:
|
|
setattr(existing, key, data[key])
|
|
db.session.commit()
|
|
return success_response(_loc_type_dict(existing), message='Reactivated existing type')
|
|
return error_response(ErrorCodes.CONFLICT,
|
|
f"Location type '{data['locationtype']}' already exists", http_code=409)
|
|
t = LocationType(locationtype=data['locationtype'],
|
|
description=data.get('description'), color=data.get('color'))
|
|
db.session.add(t)
|
|
db.session.commit()
|
|
return success_response(_loc_type_dict(t), message='Location type created', http_code=201)
|
|
|
|
|
|
@locations_bp.route('/types/<int:type_id>', methods=['PUT'])
|
|
@jwt_required()
|
|
@require_role('admin')
|
|
def update_location_type(type_id):
|
|
t = db.session.get(LocationType, type_id)
|
|
if not t:
|
|
return error_response(ErrorCodes.NOT_FOUND, 'Location type not found', http_code=404)
|
|
data = request.get_json() or {}
|
|
if 'locationtype' in data and data['locationtype'] != t.locationtype:
|
|
if LocationType.query.filter_by(locationtype=data['locationtype']).first():
|
|
return error_response(ErrorCodes.CONFLICT,
|
|
f"Location type '{data['locationtype']}' already exists", http_code=409)
|
|
for key in ('locationtype', 'description', 'color', 'isactive'):
|
|
if key in data:
|
|
setattr(t, key, data[key])
|
|
db.session.commit()
|
|
return success_response(_loc_type_dict(t), message='Location type updated')
|
|
|
|
|
|
@locations_bp.route('/types/<int:type_id>', methods=['DELETE'])
|
|
@jwt_required()
|
|
@require_role('admin')
|
|
def delete_location_type(type_id):
|
|
t = db.session.get(LocationType, type_id)
|
|
if not t:
|
|
return error_response(ErrorCodes.NOT_FOUND, 'Location type not found', http_code=404)
|
|
inuse = Location.query.filter_by(locationtypeid=type_id).count()
|
|
if inuse:
|
|
return error_response(ErrorCodes.CONFLICT,
|
|
f"Cannot delete: {inuse} location(s) still use this type", http_code=409)
|
|
db.session.delete(t)
|
|
db.session.commit()
|
|
return success_response(message='Location type deleted')
|
|
|
|
|
|
@locations_bp.route('', methods=['GET'])
|
|
@jwt_required(optional=True)
|
|
def list_locations():
|
|
"""List all locations."""
|
|
page, per_page = get_pagination_params(request)
|
|
|
|
query = Location.query
|
|
|
|
if request.args.get('active', 'true').lower() != 'false':
|
|
query = query.filter(Location.isactive == True)
|
|
|
|
if search := request.args.get('search'):
|
|
query = query.filter(
|
|
db.or_(
|
|
Location.locationname.ilike(f'%{search}%'),
|
|
Location.building.ilike(f'%{search}%')
|
|
)
|
|
)
|
|
|
|
query = query.order_by(Location.locationname)
|
|
|
|
items, total = paginate_query(query, page, per_page)
|
|
data = [loc.to_dict() for loc in items]
|
|
|
|
return paginated_response(data, page, per_page, total)
|
|
|
|
|
|
@locations_bp.route('/<int:location_id>', methods=['GET'])
|
|
@jwt_required(optional=True)
|
|
def get_location(location_id: int):
|
|
"""Get a single location."""
|
|
loc = db.session.get(Location, location_id)
|
|
|
|
if not loc:
|
|
return error_response(
|
|
ErrorCodes.NOT_FOUND,
|
|
f'Location with ID {location_id} not found',
|
|
http_code=404
|
|
)
|
|
|
|
return success_response(loc.to_dict())
|
|
|
|
|
|
@locations_bp.route('', methods=['POST'])
|
|
@jwt_required()
|
|
@require_role('admin')
|
|
def create_location():
|
|
"""Create a new location."""
|
|
data = request.get_json()
|
|
|
|
if not data or not data.get('locationname'):
|
|
return error_response(ErrorCodes.VALIDATION_ERROR, 'locationname is required')
|
|
|
|
if Location.query.filter_by(locationname=data['locationname']).first():
|
|
return error_response(
|
|
ErrorCodes.CONFLICT,
|
|
f"Location '{data['locationname']}' already exists",
|
|
http_code=409
|
|
)
|
|
|
|
loc = Location(
|
|
locationname=data['locationname'],
|
|
building=data.get('building'),
|
|
floor=data.get('floor'),
|
|
room=data.get('room'),
|
|
description=data.get('description'),
|
|
locationtypeid=data.get('locationtypeid'),
|
|
parentlocationid=data.get('parentlocationid'),
|
|
mapimage=data.get('mapimage'),
|
|
mapwidth=data.get('mapwidth'),
|
|
mapheight=data.get('mapheight')
|
|
)
|
|
|
|
db.session.add(loc)
|
|
db.session.commit()
|
|
|
|
return success_response(loc.to_dict(), message='Location created', http_code=201)
|
|
|
|
|
|
@locations_bp.route('/<int:location_id>', methods=['PUT'])
|
|
@jwt_required()
|
|
@require_role('admin')
|
|
def update_location(location_id: int):
|
|
"""Update a location."""
|
|
loc = db.session.get(Location, location_id)
|
|
|
|
if not loc:
|
|
return error_response(
|
|
ErrorCodes.NOT_FOUND,
|
|
f'Location with ID {location_id} not found',
|
|
http_code=404
|
|
)
|
|
|
|
data = request.get_json()
|
|
if not data:
|
|
return error_response(ErrorCodes.VALIDATION_ERROR, 'No data provided')
|
|
|
|
if 'locationname' in data and data['locationname'] != loc.locationname:
|
|
if Location.query.filter_by(locationname=data['locationname']).first():
|
|
return error_response(
|
|
ErrorCodes.CONFLICT,
|
|
f"Location '{data['locationname']}' already exists",
|
|
http_code=409
|
|
)
|
|
|
|
for key in ['locationname', 'building', 'floor', 'room', 'description',
|
|
'locationtypeid', 'parentlocationid', 'mapimage', 'mapwidth',
|
|
'mapheight', 'isactive']:
|
|
if key in data:
|
|
setattr(loc, key, data[key])
|
|
|
|
db.session.commit()
|
|
return success_response(loc.to_dict(), message='Location updated')
|
|
|
|
|
|
@locations_bp.route('/<int:location_id>', methods=['DELETE'])
|
|
@jwt_required()
|
|
@require_role('admin')
|
|
def delete_location(location_id: int):
|
|
"""Delete (deactivate) a location."""
|
|
loc = db.session.get(Location, location_id)
|
|
|
|
if not loc:
|
|
return error_response(
|
|
ErrorCodes.NOT_FOUND,
|
|
f'Location with ID {location_id} not found',
|
|
http_code=404
|
|
)
|
|
|
|
loc.isactive = False
|
|
db.session.commit()
|
|
|
|
return success_response(message='Location deleted')
|