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/computers/__init__.py
Normal file
5
plugins/computers/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
"""Computers plugin for ShopDB."""
|
||||
|
||||
from .plugin import ComputersPlugin
|
||||
|
||||
__all__ = ['ComputersPlugin']
|
||||
5
plugins/computers/api/__init__.py
Normal file
5
plugins/computers/api/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
"""Computers plugin API."""
|
||||
|
||||
from .routes import computers_bp
|
||||
|
||||
__all__ = ['computers_bp']
|
||||
628
plugins/computers/api/routes.py
Normal file
628
plugins/computers/api/routes.py
Normal file
@@ -0,0 +1,628 @@
|
||||
"""Computers 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, OperatingSystem, Application, AppVersion
|
||||
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 Computer, ComputerType, ComputerInstalledApp
|
||||
|
||||
computers_bp = Blueprint('computers', __name__)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Computer Types
|
||||
# =============================================================================
|
||||
|
||||
@computers_bp.route('/types', methods=['GET'])
|
||||
@jwt_required()
|
||||
def list_computer_types():
|
||||
"""List all computer types."""
|
||||
page, per_page = get_pagination_params(request)
|
||||
|
||||
query = ComputerType.query
|
||||
|
||||
if request.args.get('active', 'true').lower() != 'false':
|
||||
query = query.filter(ComputerType.isactive == True)
|
||||
|
||||
if search := request.args.get('search'):
|
||||
query = query.filter(ComputerType.computertype.ilike(f'%{search}%'))
|
||||
|
||||
query = query.order_by(ComputerType.computertype)
|
||||
|
||||
items, total = paginate_query(query, page, per_page)
|
||||
data = [t.to_dict() for t in items]
|
||||
|
||||
return paginated_response(data, page, per_page, total)
|
||||
|
||||
|
||||
@computers_bp.route('/types/<int:type_id>', methods=['GET'])
|
||||
@jwt_required()
|
||||
def get_computer_type(type_id: int):
|
||||
"""Get a single computer type."""
|
||||
t = ComputerType.query.get(type_id)
|
||||
|
||||
if not t:
|
||||
return error_response(
|
||||
ErrorCodes.NOT_FOUND,
|
||||
f'Computer type with ID {type_id} not found',
|
||||
http_code=404
|
||||
)
|
||||
|
||||
return success_response(t.to_dict())
|
||||
|
||||
|
||||
@computers_bp.route('/types', methods=['POST'])
|
||||
@jwt_required()
|
||||
def create_computer_type():
|
||||
"""Create a new computer type."""
|
||||
data = request.get_json()
|
||||
|
||||
if not data or not data.get('computertype'):
|
||||
return error_response(ErrorCodes.VALIDATION_ERROR, 'computertype is required')
|
||||
|
||||
if ComputerType.query.filter_by(computertype=data['computertype']).first():
|
||||
return error_response(
|
||||
ErrorCodes.CONFLICT,
|
||||
f"Computer type '{data['computertype']}' already exists",
|
||||
http_code=409
|
||||
)
|
||||
|
||||
t = ComputerType(
|
||||
computertype=data['computertype'],
|
||||
description=data.get('description'),
|
||||
icon=data.get('icon')
|
||||
)
|
||||
|
||||
db.session.add(t)
|
||||
db.session.commit()
|
||||
|
||||
return success_response(t.to_dict(), message='Computer type created', http_code=201)
|
||||
|
||||
|
||||
@computers_bp.route('/types/<int:type_id>', methods=['PUT'])
|
||||
@jwt_required()
|
||||
def update_computer_type(type_id: int):
|
||||
"""Update a computer type."""
|
||||
t = ComputerType.query.get(type_id)
|
||||
|
||||
if not t:
|
||||
return error_response(
|
||||
ErrorCodes.NOT_FOUND,
|
||||
f'Computer 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 'computertype' in data and data['computertype'] != t.computertype:
|
||||
if ComputerType.query.filter_by(computertype=data['computertype']).first():
|
||||
return error_response(
|
||||
ErrorCodes.CONFLICT,
|
||||
f"Computer type '{data['computertype']}' already exists",
|
||||
http_code=409
|
||||
)
|
||||
|
||||
for key in ['computertype', 'description', 'icon', 'isactive']:
|
||||
if key in data:
|
||||
setattr(t, key, data[key])
|
||||
|
||||
db.session.commit()
|
||||
return success_response(t.to_dict(), message='Computer type updated')
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Computers CRUD
|
||||
# =============================================================================
|
||||
|
||||
@computers_bp.route('', methods=['GET'])
|
||||
@jwt_required()
|
||||
def list_computers():
|
||||
"""
|
||||
List all computers with filtering and pagination.
|
||||
|
||||
Query parameters:
|
||||
- page, per_page: Pagination
|
||||
- active: Filter by active status
|
||||
- search: Search by asset number, name, or hostname
|
||||
- type_id: Filter by computer type ID
|
||||
- os_id: Filter by operating system ID
|
||||
- location_id: Filter by location ID
|
||||
- businessunit_id: Filter by business unit ID
|
||||
- shopfloor: Filter by shopfloor flag (true/false)
|
||||
"""
|
||||
page, per_page = get_pagination_params(request)
|
||||
|
||||
# Join Computer with Asset
|
||||
query = db.session.query(Computer).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}%'),
|
||||
Computer.hostname.ilike(f'%{search}%')
|
||||
)
|
||||
)
|
||||
|
||||
# Computer type filter
|
||||
if type_id := request.args.get('type_id'):
|
||||
query = query.filter(Computer.computertypeid == int(type_id))
|
||||
|
||||
# OS filter
|
||||
if os_id := request.args.get('os_id'):
|
||||
query = query.filter(Computer.osid == int(os_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))
|
||||
|
||||
# Shopfloor filter
|
||||
if shopfloor := request.args.get('shopfloor'):
|
||||
query = query.filter(Computer.isshopfloor == (shopfloor.lower() == 'true'))
|
||||
|
||||
# Sorting
|
||||
sort_by = request.args.get('sort', 'hostname')
|
||||
sort_dir = request.args.get('dir', 'asc')
|
||||
|
||||
if sort_by == 'hostname':
|
||||
col = Computer.hostname
|
||||
elif sort_by == 'assetnumber':
|
||||
col = Asset.assetnumber
|
||||
elif sort_by == 'name':
|
||||
col = Asset.name
|
||||
elif sort_by == 'lastreporteddate':
|
||||
col = Computer.lastreporteddate
|
||||
else:
|
||||
col = Computer.hostname
|
||||
|
||||
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 computer data
|
||||
data = []
|
||||
for comp in items:
|
||||
item = comp.asset.to_dict() if comp.asset else {}
|
||||
item['computer'] = comp.to_dict()
|
||||
data.append(item)
|
||||
|
||||
return paginated_response(data, page, per_page, total)
|
||||
|
||||
|
||||
@computers_bp.route('/<int:computer_id>', methods=['GET'])
|
||||
@jwt_required()
|
||||
def get_computer(computer_id: int):
|
||||
"""Get a single computer with full details."""
|
||||
comp = Computer.query.get(computer_id)
|
||||
|
||||
if not comp:
|
||||
return error_response(
|
||||
ErrorCodes.NOT_FOUND,
|
||||
f'Computer with ID {computer_id} not found',
|
||||
http_code=404
|
||||
)
|
||||
|
||||
result = comp.asset.to_dict() if comp.asset else {}
|
||||
result['computer'] = comp.to_dict()
|
||||
|
||||
return success_response(result)
|
||||
|
||||
|
||||
@computers_bp.route('/by-asset/<int:asset_id>', methods=['GET'])
|
||||
@jwt_required()
|
||||
def get_computer_by_asset(asset_id: int):
|
||||
"""Get computer data by asset ID."""
|
||||
comp = Computer.query.filter_by(assetid=asset_id).first()
|
||||
|
||||
if not comp:
|
||||
return error_response(
|
||||
ErrorCodes.NOT_FOUND,
|
||||
f'Computer for asset {asset_id} not found',
|
||||
http_code=404
|
||||
)
|
||||
|
||||
result = comp.asset.to_dict() if comp.asset else {}
|
||||
result['computer'] = comp.to_dict()
|
||||
|
||||
return success_response(result)
|
||||
|
||||
|
||||
@computers_bp.route('/by-hostname/<hostname>', methods=['GET'])
|
||||
@jwt_required()
|
||||
def get_computer_by_hostname(hostname: str):
|
||||
"""Get computer by hostname."""
|
||||
comp = Computer.query.filter_by(hostname=hostname).first()
|
||||
|
||||
if not comp:
|
||||
return error_response(
|
||||
ErrorCodes.NOT_FOUND,
|
||||
f'Computer with hostname {hostname} not found',
|
||||
http_code=404
|
||||
)
|
||||
|
||||
result = comp.asset.to_dict() if comp.asset else {}
|
||||
result['computer'] = comp.to_dict()
|
||||
|
||||
return success_response(result)
|
||||
|
||||
|
||||
@computers_bp.route('', methods=['POST'])
|
||||
@jwt_required()
|
||||
def create_computer():
|
||||
"""
|
||||
Create new computer (creates both Asset and Computer records).
|
||||
|
||||
Required fields:
|
||||
- assetnumber: Business identifier
|
||||
|
||||
Optional fields:
|
||||
- name, serialnumber, statusid, locationid, businessunitid
|
||||
- computertypeid, hostname, osid
|
||||
- isvnc, iswinrm, isshopfloor
|
||||
- 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
|
||||
)
|
||||
|
||||
# Check for duplicate hostname
|
||||
if data.get('hostname'):
|
||||
if Computer.query.filter_by(hostname=data['hostname']).first():
|
||||
return error_response(
|
||||
ErrorCodes.CONFLICT,
|
||||
f"Computer with hostname '{data['hostname']}' already exists",
|
||||
http_code=409
|
||||
)
|
||||
|
||||
# Get computer asset type
|
||||
computer_type = AssetType.query.filter_by(assettype='computer').first()
|
||||
if not computer_type:
|
||||
return error_response(
|
||||
ErrorCodes.INTERNAL_ERROR,
|
||||
'Computer 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=computer_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 computer extension
|
||||
comp = Computer(
|
||||
assetid=asset.assetid,
|
||||
computertypeid=data.get('computertypeid'),
|
||||
hostname=data.get('hostname'),
|
||||
osid=data.get('osid'),
|
||||
loggedinuser=data.get('loggedinuser'),
|
||||
lastreporteddate=data.get('lastreporteddate'),
|
||||
lastboottime=data.get('lastboottime'),
|
||||
isvnc=data.get('isvnc', False),
|
||||
iswinrm=data.get('iswinrm', False),
|
||||
isshopfloor=data.get('isshopfloor', False)
|
||||
)
|
||||
|
||||
db.session.add(comp)
|
||||
db.session.commit()
|
||||
|
||||
result = asset.to_dict()
|
||||
result['computer'] = comp.to_dict()
|
||||
|
||||
return success_response(result, message='Computer created', http_code=201)
|
||||
|
||||
|
||||
@computers_bp.route('/<int:computer_id>', methods=['PUT'])
|
||||
@jwt_required()
|
||||
def update_computer(computer_id: int):
|
||||
"""Update computer (both Asset and Computer records)."""
|
||||
comp = Computer.query.get(computer_id)
|
||||
|
||||
if not comp:
|
||||
return error_response(
|
||||
ErrorCodes.NOT_FOUND,
|
||||
f'Computer with ID {computer_id} not found',
|
||||
http_code=404
|
||||
)
|
||||
|
||||
data = request.get_json()
|
||||
if not data:
|
||||
return error_response(ErrorCodes.VALIDATION_ERROR, 'No data provided')
|
||||
|
||||
asset = comp.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
|
||||
)
|
||||
|
||||
# Check for conflicting hostname
|
||||
if 'hostname' in data and data['hostname'] != comp.hostname:
|
||||
existing = Computer.query.filter_by(hostname=data['hostname']).first()
|
||||
if existing and existing.computerid != computer_id:
|
||||
return error_response(
|
||||
ErrorCodes.CONFLICT,
|
||||
f"Computer with hostname '{data['hostname']}' 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 computer fields
|
||||
computer_fields = ['computertypeid', 'hostname', 'osid', 'loggedinuser',
|
||||
'lastreporteddate', 'lastboottime', 'isvnc', 'iswinrm', 'isshopfloor']
|
||||
for key in computer_fields:
|
||||
if key in data:
|
||||
setattr(comp, key, data[key])
|
||||
|
||||
db.session.commit()
|
||||
|
||||
result = asset.to_dict()
|
||||
result['computer'] = comp.to_dict()
|
||||
|
||||
return success_response(result, message='Computer updated')
|
||||
|
||||
|
||||
@computers_bp.route('/<int:computer_id>', methods=['DELETE'])
|
||||
@jwt_required()
|
||||
def delete_computer(computer_id: int):
|
||||
"""Delete (soft delete) computer."""
|
||||
comp = Computer.query.get(computer_id)
|
||||
|
||||
if not comp:
|
||||
return error_response(
|
||||
ErrorCodes.NOT_FOUND,
|
||||
f'Computer with ID {computer_id} not found',
|
||||
http_code=404
|
||||
)
|
||||
|
||||
# Soft delete the asset
|
||||
comp.asset.isactive = False
|
||||
db.session.commit()
|
||||
|
||||
return success_response(message='Computer deleted')
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Installed Applications
|
||||
# =============================================================================
|
||||
|
||||
@computers_bp.route('/<int:computer_id>/apps', methods=['GET'])
|
||||
@jwt_required()
|
||||
def get_installed_apps(computer_id: int):
|
||||
"""Get all installed applications for a computer."""
|
||||
comp = Computer.query.get(computer_id)
|
||||
|
||||
if not comp:
|
||||
return error_response(
|
||||
ErrorCodes.NOT_FOUND,
|
||||
f'Computer with ID {computer_id} not found',
|
||||
http_code=404
|
||||
)
|
||||
|
||||
apps = ComputerInstalledApp.query.filter_by(
|
||||
computerid=computer_id,
|
||||
isactive=True
|
||||
).all()
|
||||
|
||||
data = [app.to_dict() for app in apps]
|
||||
|
||||
return success_response(data)
|
||||
|
||||
|
||||
@computers_bp.route('/<int:computer_id>/apps', methods=['POST'])
|
||||
@jwt_required()
|
||||
def add_installed_app(computer_id: int):
|
||||
"""Add an installed application to a computer."""
|
||||
comp = Computer.query.get(computer_id)
|
||||
|
||||
if not comp:
|
||||
return error_response(
|
||||
ErrorCodes.NOT_FOUND,
|
||||
f'Computer with ID {computer_id} not found',
|
||||
http_code=404
|
||||
)
|
||||
|
||||
data = request.get_json()
|
||||
if not data or not data.get('appid'):
|
||||
return error_response(ErrorCodes.VALIDATION_ERROR, 'appid is required')
|
||||
|
||||
appid = data['appid']
|
||||
|
||||
# Validate app exists
|
||||
if not Application.query.get(appid):
|
||||
return error_response(ErrorCodes.NOT_FOUND, f'Application {appid} not found', http_code=404)
|
||||
|
||||
# Check for duplicate
|
||||
existing = ComputerInstalledApp.query.filter_by(
|
||||
computerid=computer_id,
|
||||
appid=appid
|
||||
).first()
|
||||
|
||||
if existing:
|
||||
if existing.isactive:
|
||||
return error_response(
|
||||
ErrorCodes.CONFLICT,
|
||||
'This application is already installed on this computer',
|
||||
http_code=409
|
||||
)
|
||||
else:
|
||||
# Reactivate
|
||||
existing.isactive = True
|
||||
existing.appversionid = data.get('appversionid')
|
||||
db.session.commit()
|
||||
return success_response(existing.to_dict(), message='Application reinstalled')
|
||||
|
||||
# Create new installation record
|
||||
installed = ComputerInstalledApp(
|
||||
computerid=computer_id,
|
||||
appid=appid,
|
||||
appversionid=data.get('appversionid')
|
||||
)
|
||||
|
||||
db.session.add(installed)
|
||||
db.session.commit()
|
||||
|
||||
return success_response(installed.to_dict(), message='Application installed', http_code=201)
|
||||
|
||||
|
||||
@computers_bp.route('/<int:computer_id>/apps/<int:app_id>', methods=['DELETE'])
|
||||
@jwt_required()
|
||||
def remove_installed_app(computer_id: int, app_id: int):
|
||||
"""Remove an installed application from a computer."""
|
||||
installed = ComputerInstalledApp.query.filter_by(
|
||||
computerid=computer_id,
|
||||
appid=app_id,
|
||||
isactive=True
|
||||
).first()
|
||||
|
||||
if not installed:
|
||||
return error_response(
|
||||
ErrorCodes.NOT_FOUND,
|
||||
'Installation record not found',
|
||||
http_code=404
|
||||
)
|
||||
|
||||
installed.isactive = False
|
||||
db.session.commit()
|
||||
|
||||
return success_response(message='Application uninstalled')
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Status Reporting
|
||||
# =============================================================================
|
||||
|
||||
@computers_bp.route('/<int:computer_id>/report', methods=['POST'])
|
||||
@jwt_required(optional=True)
|
||||
def report_status(computer_id: int):
|
||||
"""
|
||||
Report computer status (for agent-based reporting).
|
||||
|
||||
This endpoint can be called periodically by a client agent
|
||||
to update status information.
|
||||
"""
|
||||
comp = Computer.query.get(computer_id)
|
||||
|
||||
if not comp:
|
||||
return error_response(
|
||||
ErrorCodes.NOT_FOUND,
|
||||
f'Computer with ID {computer_id} not found',
|
||||
http_code=404
|
||||
)
|
||||
|
||||
data = request.get_json() or {}
|
||||
|
||||
# Update status fields
|
||||
from datetime import datetime
|
||||
comp.lastreporteddate = datetime.utcnow()
|
||||
|
||||
if 'loggedinuser' in data:
|
||||
comp.loggedinuser = data['loggedinuser']
|
||||
if 'lastboottime' in data:
|
||||
comp.lastboottime = data['lastboottime']
|
||||
|
||||
db.session.commit()
|
||||
|
||||
return success_response(message='Status reported')
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Dashboard
|
||||
# =============================================================================
|
||||
|
||||
@computers_bp.route('/dashboard/summary', methods=['GET'])
|
||||
@jwt_required()
|
||||
def dashboard_summary():
|
||||
"""Get computer dashboard summary data."""
|
||||
# Total active computers
|
||||
total = db.session.query(Computer).join(Asset).filter(
|
||||
Asset.isactive == True
|
||||
).count()
|
||||
|
||||
# Count by computer type
|
||||
by_type = db.session.query(
|
||||
ComputerType.computertype,
|
||||
db.func.count(Computer.computerid)
|
||||
).join(Computer, Computer.computertypeid == ComputerType.computertypeid
|
||||
).join(Asset, Asset.assetid == Computer.assetid
|
||||
).filter(Asset.isactive == True
|
||||
).group_by(ComputerType.computertype
|
||||
).all()
|
||||
|
||||
# Count by OS
|
||||
by_os = db.session.query(
|
||||
OperatingSystem.osname,
|
||||
db.func.count(Computer.computerid)
|
||||
).join(Computer, Computer.osid == OperatingSystem.osid
|
||||
).join(Asset, Asset.assetid == Computer.assetid
|
||||
).filter(Asset.isactive == True
|
||||
).group_by(OperatingSystem.osname
|
||||
).all()
|
||||
|
||||
# Count shopfloor vs non-shopfloor
|
||||
shopfloor_count = db.session.query(Computer).join(Asset).filter(
|
||||
Asset.isactive == True,
|
||||
Computer.isshopfloor == True
|
||||
).count()
|
||||
|
||||
return success_response({
|
||||
'total': total,
|
||||
'by_type': [{'type': t, 'count': c} for t, c in by_type],
|
||||
'by_os': [{'os': o, 'count': c} for o, c in by_os],
|
||||
'shopfloor': shopfloor_count,
|
||||
'non_shopfloor': total - shopfloor_count
|
||||
})
|
||||
23
plugins/computers/manifest.json
Normal file
23
plugins/computers/manifest.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "computers",
|
||||
"version": "1.0.0",
|
||||
"description": "Computer management plugin for PCs, servers, and workstations with software tracking",
|
||||
"author": "ShopDB Team",
|
||||
"dependencies": [],
|
||||
"core_version": ">=1.0.0",
|
||||
"api_prefix": "/api/computers",
|
||||
"provides": {
|
||||
"asset_type": "computer",
|
||||
"features": [
|
||||
"computer_tracking",
|
||||
"software_inventory",
|
||||
"remote_access",
|
||||
"os_management"
|
||||
]
|
||||
},
|
||||
"settings": {
|
||||
"enable_winrm": true,
|
||||
"enable_vnc": true,
|
||||
"auto_report_interval_hours": 24
|
||||
}
|
||||
}
|
||||
9
plugins/computers/models/__init__.py
Normal file
9
plugins/computers/models/__init__.py
Normal file
@@ -0,0 +1,9 @@
|
||||
"""Computers plugin models."""
|
||||
|
||||
from .computer import Computer, ComputerType, ComputerInstalledApp
|
||||
|
||||
__all__ = [
|
||||
'Computer',
|
||||
'ComputerType',
|
||||
'ComputerInstalledApp',
|
||||
]
|
||||
184
plugins/computers/models/computer.py
Normal file
184
plugins/computers/models/computer.py
Normal file
@@ -0,0 +1,184 @@
|
||||
"""Computer plugin models."""
|
||||
|
||||
from shopdb.extensions import db
|
||||
from shopdb.core.models.base import BaseModel
|
||||
|
||||
|
||||
class ComputerType(BaseModel):
|
||||
"""
|
||||
Computer type classification.
|
||||
|
||||
Examples: Shopfloor PC, Engineer Workstation, CMM PC, Server, etc.
|
||||
"""
|
||||
__tablename__ = 'computertypes'
|
||||
|
||||
computertypeid = db.Column(db.Integer, primary_key=True)
|
||||
computertype = 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"<ComputerType {self.computertype}>"
|
||||
|
||||
|
||||
class Computer(BaseModel):
|
||||
"""
|
||||
Computer-specific extension data.
|
||||
|
||||
Links to core Asset table via assetid.
|
||||
Stores computer-specific fields like hostname, OS, logged in user, etc.
|
||||
"""
|
||||
__tablename__ = 'computers'
|
||||
|
||||
computerid = 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
|
||||
)
|
||||
|
||||
# Computer classification
|
||||
computertypeid = db.Column(
|
||||
db.Integer,
|
||||
db.ForeignKey('computertypes.computertypeid'),
|
||||
nullable=True
|
||||
)
|
||||
|
||||
# Network identity
|
||||
hostname = db.Column(
|
||||
db.String(100),
|
||||
index=True,
|
||||
comment='Network hostname'
|
||||
)
|
||||
|
||||
# Operating system
|
||||
osid = db.Column(
|
||||
db.Integer,
|
||||
db.ForeignKey('operatingsystems.osid'),
|
||||
nullable=True
|
||||
)
|
||||
|
||||
# Status tracking
|
||||
loggedinuser = db.Column(db.String(100), nullable=True)
|
||||
lastreporteddate = db.Column(db.DateTime, nullable=True)
|
||||
lastboottime = db.Column(db.DateTime, nullable=True)
|
||||
|
||||
# Remote access features
|
||||
isvnc = db.Column(
|
||||
db.Boolean,
|
||||
default=False,
|
||||
comment='VNC remote access enabled'
|
||||
)
|
||||
iswinrm = db.Column(
|
||||
db.Boolean,
|
||||
default=False,
|
||||
comment='WinRM enabled'
|
||||
)
|
||||
|
||||
# Classification flags
|
||||
isshopfloor = db.Column(
|
||||
db.Boolean,
|
||||
default=False,
|
||||
comment='Shopfloor PC (vs office PC)'
|
||||
)
|
||||
|
||||
# Relationships
|
||||
asset = db.relationship(
|
||||
'Asset',
|
||||
backref=db.backref('computer', uselist=False, lazy='joined')
|
||||
)
|
||||
computertype = db.relationship('ComputerType', backref='computers')
|
||||
operatingsystem = db.relationship('OperatingSystem', backref='computers')
|
||||
|
||||
# Installed applications (one-to-many)
|
||||
installedapps = db.relationship(
|
||||
'ComputerInstalledApp',
|
||||
back_populates='computer',
|
||||
cascade='all, delete-orphan',
|
||||
lazy='dynamic'
|
||||
)
|
||||
|
||||
__table_args__ = (
|
||||
db.Index('idx_computer_type', 'computertypeid'),
|
||||
db.Index('idx_computer_hostname', 'hostname'),
|
||||
db.Index('idx_computer_os', 'osid'),
|
||||
)
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Computer {self.hostname or self.assetid}>"
|
||||
|
||||
def to_dict(self):
|
||||
"""Convert to dictionary with related names."""
|
||||
result = super().to_dict()
|
||||
|
||||
# Add related object names
|
||||
if self.computertype:
|
||||
result['computertype_name'] = self.computertype.computertype
|
||||
if self.operatingsystem:
|
||||
result['os_name'] = self.operatingsystem.osname
|
||||
|
||||
return result
|
||||
|
||||
|
||||
class ComputerInstalledApp(db.Model):
|
||||
"""
|
||||
Junction table for applications installed on computers.
|
||||
|
||||
Tracks which applications are installed on which computers,
|
||||
including version information.
|
||||
"""
|
||||
__tablename__ = 'computerinstalledapps'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
computerid = db.Column(
|
||||
db.Integer,
|
||||
db.ForeignKey('computers.computerid', ondelete='CASCADE'),
|
||||
nullable=False
|
||||
)
|
||||
appid = db.Column(
|
||||
db.Integer,
|
||||
db.ForeignKey('applications.appid'),
|
||||
nullable=False
|
||||
)
|
||||
appversionid = db.Column(
|
||||
db.Integer,
|
||||
db.ForeignKey('appversions.appversionid'),
|
||||
nullable=True
|
||||
)
|
||||
isactive = db.Column(db.Boolean, default=True, nullable=False)
|
||||
installeddate = db.Column(db.DateTime, default=db.func.now())
|
||||
|
||||
# Relationships
|
||||
computer = db.relationship('Computer', back_populates='installedapps')
|
||||
application = db.relationship('Application')
|
||||
appversion = db.relationship('AppVersion')
|
||||
|
||||
__table_args__ = (
|
||||
db.UniqueConstraint('computerid', 'appid', name='uq_computer_app'),
|
||||
db.Index('idx_compapp_computer', 'computerid'),
|
||||
db.Index('idx_compapp_app', 'appid'),
|
||||
)
|
||||
|
||||
def to_dict(self):
|
||||
"""Convert to dictionary."""
|
||||
return {
|
||||
'id': self.id,
|
||||
'computerid': self.computerid,
|
||||
'appid': self.appid,
|
||||
'appversionid': self.appversionid,
|
||||
'isactive': self.isactive,
|
||||
'installeddate': self.installeddate.isoformat() + 'Z' if self.installeddate else None,
|
||||
'application': {
|
||||
'appid': self.application.appid,
|
||||
'appname': self.application.appname,
|
||||
'appdescription': self.application.appdescription,
|
||||
} if self.application else None,
|
||||
'version': self.appversion.version if self.appversion else None
|
||||
}
|
||||
|
||||
def __repr__(self):
|
||||
return f"<ComputerInstalledApp computer={self.computerid} app={self.appid}>"
|
||||
209
plugins/computers/plugin.py
Normal file
209
plugins/computers/plugin.py
Normal file
@@ -0,0 +1,209 @@
|
||||
"""Computers 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 Computer, ComputerType, ComputerInstalledApp
|
||||
from .api import computers_bp
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ComputersPlugin(BasePlugin):
|
||||
"""
|
||||
Computers plugin - manages PC, server, and workstation assets.
|
||||
|
||||
Computers include shopfloor PCs, engineer workstations, servers, etc.
|
||||
Uses the new Asset architecture with Computer 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', 'computers'),
|
||||
version=self._manifest.get('version', '1.0.0'),
|
||||
description=self._manifest.get(
|
||||
'description',
|
||||
'Computer management for PCs, servers, and workstations'
|
||||
),
|
||||
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/computers'),
|
||||
)
|
||||
|
||||
def get_blueprint(self) -> Optional[Blueprint]:
|
||||
"""Return Flask Blueprint with API routes."""
|
||||
return computers_bp
|
||||
|
||||
def get_models(self) -> List[Type]:
|
||||
"""Return list of SQLAlchemy model classes."""
|
||||
return [Computer, ComputerType, ComputerInstalledApp]
|
||||
|
||||
def init_app(self, app: Flask, db_instance) -> None:
|
||||
"""Initialize plugin with Flask app."""
|
||||
logger.info(f"Computers 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_computer_types()
|
||||
logger.info("Computers plugin installed")
|
||||
|
||||
def _ensure_asset_type(self) -> None:
|
||||
"""Ensure computer asset type exists."""
|
||||
existing = AssetType.query.filter_by(assettype='computer').first()
|
||||
if not existing:
|
||||
at = AssetType(
|
||||
assettype='computer',
|
||||
plugin_name='computers',
|
||||
table_name='computers',
|
||||
description='PCs, servers, and workstations',
|
||||
icon='desktop'
|
||||
)
|
||||
db.session.add(at)
|
||||
logger.debug("Created asset type: computer")
|
||||
db.session.commit()
|
||||
|
||||
def _ensure_computer_types(self) -> None:
|
||||
"""Ensure basic computer types exist."""
|
||||
computer_types = [
|
||||
('Shopfloor PC', 'PC located on the shop floor for machine operation', 'desktop'),
|
||||
('Engineer Workstation', 'Engineering workstation for CAD/CAM work', 'laptop'),
|
||||
('CMM PC', 'PC dedicated to CMM operation', 'desktop'),
|
||||
('Server', 'Server system', 'server'),
|
||||
('Kiosk', 'Kiosk or info display PC', 'tv'),
|
||||
('Laptop', 'Laptop computer', 'laptop'),
|
||||
('Virtual Machine', 'Virtual machine', 'cloud'),
|
||||
('Other', 'Other computer type', 'desktop'),
|
||||
]
|
||||
|
||||
for name, description, icon in computer_types:
|
||||
existing = ComputerType.query.filter_by(computertype=name).first()
|
||||
if not existing:
|
||||
ct = ComputerType(
|
||||
computertype=name,
|
||||
description=description,
|
||||
icon=icon
|
||||
)
|
||||
db.session.add(ct)
|
||||
logger.debug(f"Created computer type: {name}")
|
||||
|
||||
db.session.commit()
|
||||
|
||||
def on_uninstall(self, app: Flask) -> None:
|
||||
"""Called when plugin is uninstalled."""
|
||||
logger.info("Computers plugin uninstalled")
|
||||
|
||||
def get_cli_commands(self) -> List:
|
||||
"""Return CLI commands for this plugin."""
|
||||
|
||||
@click.group('computers')
|
||||
def computerscli():
|
||||
"""Computers plugin commands."""
|
||||
pass
|
||||
|
||||
@computerscli.command('list-types')
|
||||
def list_types():
|
||||
"""List all computer types."""
|
||||
from flask import current_app
|
||||
|
||||
with current_app.app_context():
|
||||
types = ComputerType.query.filter_by(isactive=True).all()
|
||||
if not types:
|
||||
click.echo('No computer types found.')
|
||||
return
|
||||
|
||||
click.echo('Computer Types:')
|
||||
for t in types:
|
||||
click.echo(f" [{t.computertypeid}] {t.computertype}")
|
||||
|
||||
@computerscli.command('stats')
|
||||
def stats():
|
||||
"""Show computer statistics."""
|
||||
from flask import current_app
|
||||
from shopdb.core.models import Asset
|
||||
|
||||
with current_app.app_context():
|
||||
total = db.session.query(Computer).join(Asset).filter(
|
||||
Asset.isactive == True
|
||||
).count()
|
||||
|
||||
click.echo(f"Total active computers: {total}")
|
||||
|
||||
# Shopfloor count
|
||||
shopfloor = db.session.query(Computer).join(Asset).filter(
|
||||
Asset.isactive == True,
|
||||
Computer.isshopfloor == True
|
||||
).count()
|
||||
|
||||
click.echo(f" Shopfloor PCs: {shopfloor}")
|
||||
click.echo(f" Other: {total - shopfloor}")
|
||||
|
||||
@computerscli.command('find')
|
||||
@click.argument('hostname')
|
||||
def find_by_hostname(hostname):
|
||||
"""Find a computer by hostname."""
|
||||
from flask import current_app
|
||||
|
||||
with current_app.app_context():
|
||||
comp = Computer.query.filter(
|
||||
Computer.hostname.ilike(f'%{hostname}%')
|
||||
).first()
|
||||
|
||||
if not comp:
|
||||
click.echo(f'No computer found matching hostname: {hostname}')
|
||||
return
|
||||
|
||||
click.echo(f'Found: {comp.hostname}')
|
||||
click.echo(f' Asset: {comp.asset.assetnumber}')
|
||||
click.echo(f' Type: {comp.computertype.computertype if comp.computertype else "N/A"}')
|
||||
click.echo(f' OS: {comp.operatingsystem.osname if comp.operatingsystem else "N/A"}')
|
||||
click.echo(f' Logged in: {comp.loggedinuser or "N/A"}')
|
||||
|
||||
return [computerscli]
|
||||
|
||||
def get_dashboard_widgets(self) -> List[Dict]:
|
||||
"""Return dashboard widget definitions."""
|
||||
return [
|
||||
{
|
||||
'name': 'Computer Status',
|
||||
'component': 'ComputerStatusWidget',
|
||||
'endpoint': '/api/computers/dashboard/summary',
|
||||
'size': 'medium',
|
||||
'position': 6,
|
||||
},
|
||||
]
|
||||
|
||||
def get_navigation_items(self) -> List[Dict]:
|
||||
"""Return navigation menu items."""
|
||||
return [
|
||||
{
|
||||
'name': 'Computers',
|
||||
'icon': 'desktop',
|
||||
'route': '/computers',
|
||||
'position': 15,
|
||||
},
|
||||
]
|
||||
Reference in New Issue
Block a user