Add USB, Notifications, Network plugins and reusable EmployeeSearch component
New Plugins: - USB plugin: Device checkout/checkin with employee lookup, checkout history - Notifications plugin: Announcements with types, scheduling, shopfloor display - Network plugin: Network device management with subnets and VLANs - Equipment and Computers plugins: Asset type separation Frontend: - EmployeeSearch component: Reusable employee lookup with autocomplete - USB views: List, detail, checkout/checkin modals - Notifications views: List, form with recognition mode - Network views: Device list, detail, form - Calendar view with FullCalendar integration - Shopfloor and TV dashboard views - Reports index page - Map editor for asset positioning - Light/dark mode fixes for map tooltips Backend: - Employee search API with external lookup service - Collector API for PowerShell data collection - Reports API endpoints - Slides API for TV dashboard - Fixed AppVersion model (removed BaseModel inheritance) - Added checkout_name column to usbcheckouts table Styling: - Unified detail page styles - Improved pagination (page numbers instead of prev/next) - Dark/light mode theme improvements Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
5
plugins/equipment/__init__.py
Normal file
5
plugins/equipment/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
"""Equipment plugin for ShopDB."""
|
||||
|
||||
from .plugin import EquipmentPlugin
|
||||
|
||||
__all__ = ['EquipmentPlugin']
|
||||
5
plugins/equipment/api/__init__.py
Normal file
5
plugins/equipment/api/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
"""Equipment plugin API."""
|
||||
|
||||
from .routes import equipment_bp
|
||||
|
||||
__all__ = ['equipment_bp']
|
||||
429
plugins/equipment/api/routes.py
Normal file
429
plugins/equipment/api/routes.py
Normal file
@@ -0,0 +1,429 @@
|
||||
"""Equipment plugin API endpoints."""
|
||||
|
||||
from flask import Blueprint, request
|
||||
from flask_jwt_extended import jwt_required
|
||||
|
||||
from shopdb.extensions import db
|
||||
from shopdb.core.models import Asset, AssetType, Vendor, Model
|
||||
from shopdb.utils.responses import (
|
||||
success_response,
|
||||
error_response,
|
||||
paginated_response,
|
||||
ErrorCodes
|
||||
)
|
||||
from shopdb.utils.pagination import get_pagination_params, paginate_query
|
||||
|
||||
from ..models import Equipment, EquipmentType
|
||||
|
||||
equipment_bp = Blueprint('equipment', __name__)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Equipment Types
|
||||
# =============================================================================
|
||||
|
||||
@equipment_bp.route('/types', methods=['GET'])
|
||||
@jwt_required()
|
||||
def list_equipment_types():
|
||||
"""List all equipment types."""
|
||||
page, per_page = get_pagination_params(request)
|
||||
|
||||
query = EquipmentType.query
|
||||
|
||||
if request.args.get('active', 'true').lower() != 'false':
|
||||
query = query.filter(EquipmentType.isactive == True)
|
||||
|
||||
if search := request.args.get('search'):
|
||||
query = query.filter(EquipmentType.equipmenttype.ilike(f'%{search}%'))
|
||||
|
||||
query = query.order_by(EquipmentType.equipmenttype)
|
||||
|
||||
items, total = paginate_query(query, page, per_page)
|
||||
data = [t.to_dict() for t in items]
|
||||
|
||||
return paginated_response(data, page, per_page, total)
|
||||
|
||||
|
||||
@equipment_bp.route('/types/<int:type_id>', methods=['GET'])
|
||||
@jwt_required()
|
||||
def get_equipment_type(type_id: int):
|
||||
"""Get a single equipment type."""
|
||||
t = EquipmentType.query.get(type_id)
|
||||
|
||||
if not t:
|
||||
return error_response(
|
||||
ErrorCodes.NOT_FOUND,
|
||||
f'Equipment type with ID {type_id} not found',
|
||||
http_code=404
|
||||
)
|
||||
|
||||
return success_response(t.to_dict())
|
||||
|
||||
|
||||
@equipment_bp.route('/types', methods=['POST'])
|
||||
@jwt_required()
|
||||
def create_equipment_type():
|
||||
"""Create a new equipment type."""
|
||||
data = request.get_json()
|
||||
|
||||
if not data or not data.get('equipmenttype'):
|
||||
return error_response(ErrorCodes.VALIDATION_ERROR, 'equipmenttype is required')
|
||||
|
||||
if EquipmentType.query.filter_by(equipmenttype=data['equipmenttype']).first():
|
||||
return error_response(
|
||||
ErrorCodes.CONFLICT,
|
||||
f"Equipment type '{data['equipmenttype']}' already exists",
|
||||
http_code=409
|
||||
)
|
||||
|
||||
t = EquipmentType(
|
||||
equipmenttype=data['equipmenttype'],
|
||||
description=data.get('description'),
|
||||
icon=data.get('icon')
|
||||
)
|
||||
|
||||
db.session.add(t)
|
||||
db.session.commit()
|
||||
|
||||
return success_response(t.to_dict(), message='Equipment type created', http_code=201)
|
||||
|
||||
|
||||
@equipment_bp.route('/types/<int:type_id>', methods=['PUT'])
|
||||
@jwt_required()
|
||||
def update_equipment_type(type_id: int):
|
||||
"""Update an equipment type."""
|
||||
t = EquipmentType.query.get(type_id)
|
||||
|
||||
if not t:
|
||||
return error_response(
|
||||
ErrorCodes.NOT_FOUND,
|
||||
f'Equipment type with ID {type_id} not found',
|
||||
http_code=404
|
||||
)
|
||||
|
||||
data = request.get_json()
|
||||
if not data:
|
||||
return error_response(ErrorCodes.VALIDATION_ERROR, 'No data provided')
|
||||
|
||||
if 'equipmenttype' in data and data['equipmenttype'] != t.equipmenttype:
|
||||
if EquipmentType.query.filter_by(equipmenttype=data['equipmenttype']).first():
|
||||
return error_response(
|
||||
ErrorCodes.CONFLICT,
|
||||
f"Equipment type '{data['equipmenttype']}' already exists",
|
||||
http_code=409
|
||||
)
|
||||
|
||||
for key in ['equipmenttype', 'description', 'icon', 'isactive']:
|
||||
if key in data:
|
||||
setattr(t, key, data[key])
|
||||
|
||||
db.session.commit()
|
||||
return success_response(t.to_dict(), message='Equipment type updated')
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Equipment CRUD
|
||||
# =============================================================================
|
||||
|
||||
@equipment_bp.route('', methods=['GET'])
|
||||
@jwt_required()
|
||||
def list_equipment():
|
||||
"""
|
||||
List all equipment with filtering and pagination.
|
||||
|
||||
Query parameters:
|
||||
- page, per_page: Pagination
|
||||
- active: Filter by active status
|
||||
- search: Search by asset number or name
|
||||
- type_id: Filter by equipment type ID
|
||||
- vendor_id: Filter by vendor ID
|
||||
- location_id: Filter by location ID
|
||||
- businessunit_id: Filter by business unit ID
|
||||
"""
|
||||
page, per_page = get_pagination_params(request)
|
||||
|
||||
# Join Equipment with Asset
|
||||
query = db.session.query(Equipment).join(Asset)
|
||||
|
||||
# Active filter
|
||||
if request.args.get('active', 'true').lower() != 'false':
|
||||
query = query.filter(Asset.isactive == True)
|
||||
|
||||
# Search filter
|
||||
if search := request.args.get('search'):
|
||||
query = query.filter(
|
||||
db.or_(
|
||||
Asset.assetnumber.ilike(f'%{search}%'),
|
||||
Asset.name.ilike(f'%{search}%'),
|
||||
Asset.serialnumber.ilike(f'%{search}%')
|
||||
)
|
||||
)
|
||||
|
||||
# Equipment type filter
|
||||
if type_id := request.args.get('type_id'):
|
||||
query = query.filter(Equipment.equipmenttypeid == int(type_id))
|
||||
|
||||
# Vendor filter
|
||||
if vendor_id := request.args.get('vendor_id'):
|
||||
query = query.filter(Equipment.vendorid == int(vendor_id))
|
||||
|
||||
# Location filter
|
||||
if location_id := request.args.get('location_id'):
|
||||
query = query.filter(Asset.locationid == int(location_id))
|
||||
|
||||
# Business unit filter
|
||||
if bu_id := request.args.get('businessunit_id'):
|
||||
query = query.filter(Asset.businessunitid == int(bu_id))
|
||||
|
||||
# Sorting
|
||||
sort_by = request.args.get('sort', 'assetnumber')
|
||||
sort_dir = request.args.get('dir', 'asc')
|
||||
|
||||
if sort_by == 'assetnumber':
|
||||
col = Asset.assetnumber
|
||||
elif sort_by == 'name':
|
||||
col = Asset.name
|
||||
else:
|
||||
col = Asset.assetnumber
|
||||
|
||||
query = query.order_by(col.desc() if sort_dir == 'desc' else col)
|
||||
|
||||
items, total = paginate_query(query, page, per_page)
|
||||
|
||||
# Build response with both asset and equipment data
|
||||
data = []
|
||||
for equip in items:
|
||||
item = equip.asset.to_dict() if equip.asset else {}
|
||||
item['equipment'] = equip.to_dict()
|
||||
data.append(item)
|
||||
|
||||
return paginated_response(data, page, per_page, total)
|
||||
|
||||
|
||||
@equipment_bp.route('/<int:equipment_id>', methods=['GET'])
|
||||
@jwt_required()
|
||||
def get_equipment(equipment_id: int):
|
||||
"""Get a single equipment item with full details."""
|
||||
equip = Equipment.query.get(equipment_id)
|
||||
|
||||
if not equip:
|
||||
return error_response(
|
||||
ErrorCodes.NOT_FOUND,
|
||||
f'Equipment with ID {equipment_id} not found',
|
||||
http_code=404
|
||||
)
|
||||
|
||||
result = equip.asset.to_dict() if equip.asset else {}
|
||||
result['equipment'] = equip.to_dict()
|
||||
|
||||
return success_response(result)
|
||||
|
||||
|
||||
@equipment_bp.route('/by-asset/<int:asset_id>', methods=['GET'])
|
||||
@jwt_required()
|
||||
def get_equipment_by_asset(asset_id: int):
|
||||
"""Get equipment data by asset ID."""
|
||||
equip = Equipment.query.filter_by(assetid=asset_id).first()
|
||||
|
||||
if not equip:
|
||||
return error_response(
|
||||
ErrorCodes.NOT_FOUND,
|
||||
f'Equipment for asset {asset_id} not found',
|
||||
http_code=404
|
||||
)
|
||||
|
||||
result = equip.asset.to_dict() if equip.asset else {}
|
||||
result['equipment'] = equip.to_dict()
|
||||
|
||||
return success_response(result)
|
||||
|
||||
|
||||
@equipment_bp.route('', methods=['POST'])
|
||||
@jwt_required()
|
||||
def create_equipment():
|
||||
"""
|
||||
Create new equipment (creates both Asset and Equipment records).
|
||||
|
||||
Required fields:
|
||||
- assetnumber: Business identifier
|
||||
|
||||
Optional fields:
|
||||
- name, serialnumber, statusid, locationid, businessunitid
|
||||
- equipmenttypeid, vendorid, modelnumberid
|
||||
- requiresmanualconfig, islocationonly
|
||||
- mapleft, maptop, notes
|
||||
"""
|
||||
data = request.get_json()
|
||||
|
||||
if not data:
|
||||
return error_response(ErrorCodes.VALIDATION_ERROR, 'No data provided')
|
||||
|
||||
if not data.get('assetnumber'):
|
||||
return error_response(ErrorCodes.VALIDATION_ERROR, 'assetnumber is required')
|
||||
|
||||
# Check for duplicate assetnumber
|
||||
if Asset.query.filter_by(assetnumber=data['assetnumber']).first():
|
||||
return error_response(
|
||||
ErrorCodes.CONFLICT,
|
||||
f"Asset with number '{data['assetnumber']}' already exists",
|
||||
http_code=409
|
||||
)
|
||||
|
||||
# Get equipment asset type
|
||||
equipment_type = AssetType.query.filter_by(assettype='equipment').first()
|
||||
if not equipment_type:
|
||||
return error_response(
|
||||
ErrorCodes.INTERNAL_ERROR,
|
||||
'Equipment asset type not found. Plugin may not be properly installed.',
|
||||
http_code=500
|
||||
)
|
||||
|
||||
# Create the core asset
|
||||
asset = Asset(
|
||||
assetnumber=data['assetnumber'],
|
||||
name=data.get('name'),
|
||||
serialnumber=data.get('serialnumber'),
|
||||
assettypeid=equipment_type.assettypeid,
|
||||
statusid=data.get('statusid', 1),
|
||||
locationid=data.get('locationid'),
|
||||
businessunitid=data.get('businessunitid'),
|
||||
mapleft=data.get('mapleft'),
|
||||
maptop=data.get('maptop'),
|
||||
notes=data.get('notes')
|
||||
)
|
||||
|
||||
db.session.add(asset)
|
||||
db.session.flush() # Get the assetid
|
||||
|
||||
# Create the equipment extension
|
||||
equip = Equipment(
|
||||
assetid=asset.assetid,
|
||||
equipmenttypeid=data.get('equipmenttypeid'),
|
||||
vendorid=data.get('vendorid'),
|
||||
modelnumberid=data.get('modelnumberid'),
|
||||
requiresmanualconfig=data.get('requiresmanualconfig', False),
|
||||
islocationonly=data.get('islocationonly', False),
|
||||
lastmaintenancedate=data.get('lastmaintenancedate'),
|
||||
nextmaintenancedate=data.get('nextmaintenancedate'),
|
||||
maintenanceintervaldays=data.get('maintenanceintervaldays')
|
||||
)
|
||||
|
||||
db.session.add(equip)
|
||||
db.session.commit()
|
||||
|
||||
result = asset.to_dict()
|
||||
result['equipment'] = equip.to_dict()
|
||||
|
||||
return success_response(result, message='Equipment created', http_code=201)
|
||||
|
||||
|
||||
@equipment_bp.route('/<int:equipment_id>', methods=['PUT'])
|
||||
@jwt_required()
|
||||
def update_equipment(equipment_id: int):
|
||||
"""Update equipment (both Asset and Equipment records)."""
|
||||
equip = Equipment.query.get(equipment_id)
|
||||
|
||||
if not equip:
|
||||
return error_response(
|
||||
ErrorCodes.NOT_FOUND,
|
||||
f'Equipment with ID {equipment_id} not found',
|
||||
http_code=404
|
||||
)
|
||||
|
||||
data = request.get_json()
|
||||
if not data:
|
||||
return error_response(ErrorCodes.VALIDATION_ERROR, 'No data provided')
|
||||
|
||||
asset = equip.asset
|
||||
|
||||
# Check for conflicting assetnumber
|
||||
if 'assetnumber' in data and data['assetnumber'] != asset.assetnumber:
|
||||
if Asset.query.filter_by(assetnumber=data['assetnumber']).first():
|
||||
return error_response(
|
||||
ErrorCodes.CONFLICT,
|
||||
f"Asset with number '{data['assetnumber']}' already exists",
|
||||
http_code=409
|
||||
)
|
||||
|
||||
# Update asset fields
|
||||
asset_fields = ['assetnumber', 'name', 'serialnumber', 'statusid',
|
||||
'locationid', 'businessunitid', 'mapleft', 'maptop', 'notes', 'isactive']
|
||||
for key in asset_fields:
|
||||
if key in data:
|
||||
setattr(asset, key, data[key])
|
||||
|
||||
# Update equipment fields
|
||||
equipment_fields = ['equipmenttypeid', 'vendorid', 'modelnumberid',
|
||||
'requiresmanualconfig', 'islocationonly',
|
||||
'lastmaintenancedate', 'nextmaintenancedate', 'maintenanceintervaldays']
|
||||
for key in equipment_fields:
|
||||
if key in data:
|
||||
setattr(equip, key, data[key])
|
||||
|
||||
db.session.commit()
|
||||
|
||||
result = asset.to_dict()
|
||||
result['equipment'] = equip.to_dict()
|
||||
|
||||
return success_response(result, message='Equipment updated')
|
||||
|
||||
|
||||
@equipment_bp.route('/<int:equipment_id>', methods=['DELETE'])
|
||||
@jwt_required()
|
||||
def delete_equipment(equipment_id: int):
|
||||
"""Delete (soft delete) equipment."""
|
||||
equip = Equipment.query.get(equipment_id)
|
||||
|
||||
if not equip:
|
||||
return error_response(
|
||||
ErrorCodes.NOT_FOUND,
|
||||
f'Equipment with ID {equipment_id} not found',
|
||||
http_code=404
|
||||
)
|
||||
|
||||
# Soft delete the asset (equipment extension will stay linked)
|
||||
equip.asset.isactive = False
|
||||
db.session.commit()
|
||||
|
||||
return success_response(message='Equipment deleted')
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Dashboard
|
||||
# =============================================================================
|
||||
|
||||
@equipment_bp.route('/dashboard/summary', methods=['GET'])
|
||||
@jwt_required()
|
||||
def dashboard_summary():
|
||||
"""Get equipment dashboard summary data."""
|
||||
# Total active equipment count
|
||||
total = db.session.query(Equipment).join(Asset).filter(
|
||||
Asset.isactive == True
|
||||
).count()
|
||||
|
||||
# Count by equipment type
|
||||
by_type = db.session.query(
|
||||
EquipmentType.equipmenttype,
|
||||
db.func.count(Equipment.equipmentid)
|
||||
).join(Equipment, Equipment.equipmenttypeid == EquipmentType.equipmenttypeid
|
||||
).join(Asset, Asset.assetid == Equipment.assetid
|
||||
).filter(Asset.isactive == True
|
||||
).group_by(EquipmentType.equipmenttype
|
||||
).all()
|
||||
|
||||
# Count by status
|
||||
from shopdb.core.models import AssetStatus
|
||||
by_status = db.session.query(
|
||||
AssetStatus.status,
|
||||
db.func.count(Equipment.equipmentid)
|
||||
).join(Asset, Asset.assetid == Equipment.assetid
|
||||
).join(AssetStatus, AssetStatus.statusid == Asset.statusid
|
||||
).filter(Asset.isactive == True
|
||||
).group_by(AssetStatus.status
|
||||
).all()
|
||||
|
||||
return success_response({
|
||||
'total': total,
|
||||
'by_type': [{'type': t, 'count': c} for t, c in by_type],
|
||||
'by_status': [{'status': s, 'count': c} for s, c in by_status]
|
||||
})
|
||||
22
plugins/equipment/manifest.json
Normal file
22
plugins/equipment/manifest.json
Normal file
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "equipment",
|
||||
"version": "1.0.0",
|
||||
"description": "Equipment management plugin for CNCs, CMMs, lathes, grinders, and other manufacturing equipment",
|
||||
"author": "ShopDB Team",
|
||||
"dependencies": [],
|
||||
"core_version": ">=1.0.0",
|
||||
"api_prefix": "/api/equipment",
|
||||
"provides": {
|
||||
"asset_type": "equipment",
|
||||
"features": [
|
||||
"equipment_tracking",
|
||||
"maintenance_scheduling",
|
||||
"vendor_management",
|
||||
"model_catalog"
|
||||
]
|
||||
},
|
||||
"settings": {
|
||||
"enable_maintenance_alerts": true,
|
||||
"maintenance_alert_days": 30
|
||||
}
|
||||
}
|
||||
8
plugins/equipment/models/__init__.py
Normal file
8
plugins/equipment/models/__init__.py
Normal file
@@ -0,0 +1,8 @@
|
||||
"""Equipment plugin models."""
|
||||
|
||||
from .equipment import Equipment, EquipmentType
|
||||
|
||||
__all__ = [
|
||||
'Equipment',
|
||||
'EquipmentType',
|
||||
]
|
||||
109
plugins/equipment/models/equipment.py
Normal file
109
plugins/equipment/models/equipment.py
Normal file
@@ -0,0 +1,109 @@
|
||||
"""Equipment plugin models."""
|
||||
|
||||
from shopdb.extensions import db
|
||||
from shopdb.core.models.base import BaseModel
|
||||
|
||||
|
||||
class EquipmentType(BaseModel):
|
||||
"""
|
||||
Equipment type classification.
|
||||
|
||||
Examples: CNC, CMM, Lathe, Grinder, EDM, Part Marker, etc.
|
||||
"""
|
||||
__tablename__ = 'equipmenttypes'
|
||||
|
||||
equipmenttypeid = db.Column(db.Integer, primary_key=True)
|
||||
equipmenttype = db.Column(db.String(100), unique=True, nullable=False)
|
||||
description = db.Column(db.Text)
|
||||
icon = db.Column(db.String(50), comment='Icon name for UI')
|
||||
|
||||
def __repr__(self):
|
||||
return f"<EquipmentType {self.equipmenttype}>"
|
||||
|
||||
|
||||
class Equipment(BaseModel):
|
||||
"""
|
||||
Equipment-specific extension data.
|
||||
|
||||
Links to core Asset table via assetid.
|
||||
Stores equipment-specific fields like type, model, vendor, etc.
|
||||
"""
|
||||
__tablename__ = 'equipment'
|
||||
|
||||
equipmentid = db.Column(db.Integer, primary_key=True)
|
||||
|
||||
# Link to core asset
|
||||
assetid = db.Column(
|
||||
db.Integer,
|
||||
db.ForeignKey('assets.assetid', ondelete='CASCADE'),
|
||||
unique=True,
|
||||
nullable=False,
|
||||
index=True
|
||||
)
|
||||
|
||||
# Equipment classification
|
||||
equipmenttypeid = db.Column(
|
||||
db.Integer,
|
||||
db.ForeignKey('equipmenttypes.equipmenttypeid'),
|
||||
nullable=True
|
||||
)
|
||||
|
||||
# Vendor and model
|
||||
vendorid = db.Column(
|
||||
db.Integer,
|
||||
db.ForeignKey('vendors.vendorid'),
|
||||
nullable=True
|
||||
)
|
||||
modelnumberid = db.Column(
|
||||
db.Integer,
|
||||
db.ForeignKey('models.modelnumberid'),
|
||||
nullable=True
|
||||
)
|
||||
|
||||
# Equipment-specific fields
|
||||
requiresmanualconfig = db.Column(
|
||||
db.Boolean,
|
||||
default=False,
|
||||
comment='Multi-PC machine needs manual configuration'
|
||||
)
|
||||
islocationonly = db.Column(
|
||||
db.Boolean,
|
||||
default=False,
|
||||
comment='Virtual location marker (not actual equipment)'
|
||||
)
|
||||
|
||||
# Maintenance tracking
|
||||
lastmaintenancedate = db.Column(db.DateTime, nullable=True)
|
||||
nextmaintenancedate = db.Column(db.DateTime, nullable=True)
|
||||
maintenanceintervaldays = db.Column(db.Integer, nullable=True)
|
||||
|
||||
# Relationships
|
||||
asset = db.relationship(
|
||||
'Asset',
|
||||
backref=db.backref('equipment', uselist=False, lazy='joined')
|
||||
)
|
||||
equipmenttype = db.relationship('EquipmentType', backref='equipment')
|
||||
vendor = db.relationship('Vendor', backref='equipment_items')
|
||||
model = db.relationship('Model', backref='equipment_items')
|
||||
|
||||
__table_args__ = (
|
||||
db.Index('idx_equipment_type', 'equipmenttypeid'),
|
||||
db.Index('idx_equipment_vendor', 'vendorid'),
|
||||
)
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Equipment {self.assetid}>"
|
||||
|
||||
def to_dict(self):
|
||||
"""Convert to dictionary with related names."""
|
||||
result = super().to_dict()
|
||||
|
||||
# Add related object names
|
||||
if self.equipmenttype:
|
||||
result['equipmenttype_name'] = self.equipmenttype.equipmenttype
|
||||
if self.vendor:
|
||||
result['vendor_name'] = self.vendor.vendor
|
||||
if self.model:
|
||||
result['model_name'] = self.model.modelnumber
|
||||
|
||||
return result
|
||||
220
plugins/equipment/plugin.py
Normal file
220
plugins/equipment/plugin.py
Normal file
@@ -0,0 +1,220 @@
|
||||
"""Equipment plugin main class."""
|
||||
|
||||
import json
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import List, Dict, Optional, Type
|
||||
|
||||
from flask import Flask, Blueprint
|
||||
import click
|
||||
|
||||
from shopdb.plugins.base import BasePlugin, PluginMeta
|
||||
from shopdb.extensions import db
|
||||
from shopdb.core.models import AssetType, AssetStatus
|
||||
|
||||
from .models import Equipment, EquipmentType
|
||||
from .api import equipment_bp
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class EquipmentPlugin(BasePlugin):
|
||||
"""
|
||||
Equipment plugin - manages manufacturing equipment assets.
|
||||
|
||||
Equipment includes CNCs, CMMs, lathes, grinders, EDMs, part markers, etc.
|
||||
Uses the new Asset architecture with Equipment extension table.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self._manifest = self._load_manifest()
|
||||
|
||||
def _load_manifest(self) -> Dict:
|
||||
"""Load plugin manifest from JSON file."""
|
||||
manifestpath = Path(__file__).parent / 'manifest.json'
|
||||
if manifestpath.exists():
|
||||
with open(manifestpath, 'r') as f:
|
||||
return json.load(f)
|
||||
return {}
|
||||
|
||||
@property
|
||||
def meta(self) -> PluginMeta:
|
||||
"""Return plugin metadata."""
|
||||
return PluginMeta(
|
||||
name=self._manifest.get('name', 'equipment'),
|
||||
version=self._manifest.get('version', '1.0.0'),
|
||||
description=self._manifest.get(
|
||||
'description',
|
||||
'Equipment management for manufacturing assets'
|
||||
),
|
||||
author=self._manifest.get('author', 'ShopDB Team'),
|
||||
dependencies=self._manifest.get('dependencies', []),
|
||||
core_version=self._manifest.get('core_version', '>=1.0.0'),
|
||||
api_prefix=self._manifest.get('api_prefix', '/api/equipment'),
|
||||
)
|
||||
|
||||
def get_blueprint(self) -> Optional[Blueprint]:
|
||||
"""Return Flask Blueprint with API routes."""
|
||||
return equipment_bp
|
||||
|
||||
def get_models(self) -> List[Type]:
|
||||
"""Return list of SQLAlchemy model classes."""
|
||||
return [Equipment, EquipmentType]
|
||||
|
||||
def init_app(self, app: Flask, db_instance) -> None:
|
||||
"""Initialize plugin with Flask app."""
|
||||
logger.info(f"Equipment plugin initialized (v{self.meta.version})")
|
||||
|
||||
def on_install(self, app: Flask) -> None:
|
||||
"""Called when plugin is installed."""
|
||||
with app.app_context():
|
||||
self._ensure_asset_type()
|
||||
self._ensure_asset_statuses()
|
||||
self._ensure_equipment_types()
|
||||
logger.info("Equipment plugin installed")
|
||||
|
||||
def _ensure_asset_type(self) -> None:
|
||||
"""Ensure equipment asset type exists."""
|
||||
existing = AssetType.query.filter_by(assettype='equipment').first()
|
||||
if not existing:
|
||||
at = AssetType(
|
||||
assettype='equipment',
|
||||
plugin_name='equipment',
|
||||
table_name='equipment',
|
||||
description='Manufacturing equipment (CNCs, CMMs, lathes, etc.)',
|
||||
icon='cog'
|
||||
)
|
||||
db.session.add(at)
|
||||
logger.debug("Created asset type: equipment")
|
||||
db.session.commit()
|
||||
|
||||
def _ensure_asset_statuses(self) -> None:
|
||||
"""Ensure standard asset statuses exist."""
|
||||
statuses = [
|
||||
('In Use', 'Asset is currently in use', '#28a745'),
|
||||
('Spare', 'Spare/backup asset', '#17a2b8'),
|
||||
('Retired', 'Asset has been retired', '#6c757d'),
|
||||
('Maintenance', 'Asset is under maintenance', '#ffc107'),
|
||||
('Decommissioned', 'Asset has been decommissioned', '#dc3545'),
|
||||
]
|
||||
|
||||
for name, description, color in statuses:
|
||||
existing = AssetStatus.query.filter_by(status=name).first()
|
||||
if not existing:
|
||||
s = AssetStatus(
|
||||
status=name,
|
||||
description=description,
|
||||
color=color
|
||||
)
|
||||
db.session.add(s)
|
||||
logger.debug(f"Created asset status: {name}")
|
||||
|
||||
db.session.commit()
|
||||
|
||||
def _ensure_equipment_types(self) -> None:
|
||||
"""Ensure basic equipment types exist."""
|
||||
equipment_types = [
|
||||
('CNC', 'Computer Numerical Control machine', 'cnc'),
|
||||
('CMM', 'Coordinate Measuring Machine', 'cmm'),
|
||||
('Lathe', 'Lathe machine', 'lathe'),
|
||||
('Grinder', 'Grinding machine', 'grinder'),
|
||||
('EDM', 'Electrical Discharge Machine', 'edm'),
|
||||
('Part Marker', 'Part marking/engraving equipment', 'marker'),
|
||||
('Mill', 'Milling machine', 'mill'),
|
||||
('Press', 'Press machine', 'press'),
|
||||
('Robot', 'Industrial robot', 'robot'),
|
||||
('Other', 'Other equipment type', 'cog'),
|
||||
]
|
||||
|
||||
for name, description, icon in equipment_types:
|
||||
existing = EquipmentType.query.filter_by(equipmenttype=name).first()
|
||||
if not existing:
|
||||
et = EquipmentType(
|
||||
equipmenttype=name,
|
||||
description=description,
|
||||
icon=icon
|
||||
)
|
||||
db.session.add(et)
|
||||
logger.debug(f"Created equipment type: {name}")
|
||||
|
||||
db.session.commit()
|
||||
|
||||
def on_uninstall(self, app: Flask) -> None:
|
||||
"""Called when plugin is uninstalled."""
|
||||
logger.info("Equipment plugin uninstalled")
|
||||
|
||||
def get_cli_commands(self) -> List:
|
||||
"""Return CLI commands for this plugin."""
|
||||
|
||||
@click.group('equipment')
|
||||
def equipmentcli():
|
||||
"""Equipment plugin commands."""
|
||||
pass
|
||||
|
||||
@equipmentcli.command('list-types')
|
||||
def list_types():
|
||||
"""List all equipment types."""
|
||||
from flask import current_app
|
||||
|
||||
with current_app.app_context():
|
||||
types = EquipmentType.query.filter_by(isactive=True).all()
|
||||
if not types:
|
||||
click.echo('No equipment types found.')
|
||||
return
|
||||
|
||||
click.echo('Equipment Types:')
|
||||
for t in types:
|
||||
click.echo(f" [{t.equipmenttypeid}] {t.equipmenttype}")
|
||||
|
||||
@equipmentcli.command('stats')
|
||||
def stats():
|
||||
"""Show equipment statistics."""
|
||||
from flask import current_app
|
||||
from shopdb.core.models import Asset
|
||||
|
||||
with current_app.app_context():
|
||||
total = db.session.query(Equipment).join(Asset).filter(
|
||||
Asset.isactive == True
|
||||
).count()
|
||||
|
||||
click.echo(f"Total active equipment: {total}")
|
||||
|
||||
# By type
|
||||
by_type = db.session.query(
|
||||
EquipmentType.equipmenttype,
|
||||
db.func.count(Equipment.equipmentid)
|
||||
).join(Equipment, Equipment.equipmenttypeid == EquipmentType.equipmenttypeid
|
||||
).join(Asset, Asset.assetid == Equipment.assetid
|
||||
).filter(Asset.isactive == True
|
||||
).group_by(EquipmentType.equipmenttype
|
||||
).all()
|
||||
|
||||
if by_type:
|
||||
click.echo("\nBy Type:")
|
||||
for t, c in by_type:
|
||||
click.echo(f" {t}: {c}")
|
||||
|
||||
return [equipmentcli]
|
||||
|
||||
def get_dashboard_widgets(self) -> List[Dict]:
|
||||
"""Return dashboard widget definitions."""
|
||||
return [
|
||||
{
|
||||
'name': 'Equipment Status',
|
||||
'component': 'EquipmentStatusWidget',
|
||||
'endpoint': '/api/equipment/dashboard/summary',
|
||||
'size': 'medium',
|
||||
'position': 5,
|
||||
},
|
||||
]
|
||||
|
||||
def get_navigation_items(self) -> List[Dict]:
|
||||
"""Return navigation menu items."""
|
||||
return [
|
||||
{
|
||||
'name': 'Equipment',
|
||||
'icon': 'cog',
|
||||
'route': '/equipment',
|
||||
'position': 10,
|
||||
},
|
||||
]
|
||||
Reference in New Issue
Block a user