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/notifications/__init__.py
Normal file
5
plugins/notifications/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
"""Notifications plugin package."""
|
||||
|
||||
from .plugin import NotificationsPlugin
|
||||
|
||||
__all__ = ['NotificationsPlugin']
|
||||
5
plugins/notifications/api/__init__.py
Normal file
5
plugins/notifications/api/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
"""Notifications plugin API."""
|
||||
|
||||
from .routes import notifications_bp
|
||||
|
||||
__all__ = ['notifications_bp']
|
||||
617
plugins/notifications/api/routes.py
Normal file
617
plugins/notifications/api/routes.py
Normal file
@@ -0,0 +1,617 @@
|
||||
"""Notifications plugin API endpoints - adapted to existing schema."""
|
||||
|
||||
from datetime import datetime
|
||||
from flask import Blueprint, request
|
||||
from flask_jwt_extended import jwt_required
|
||||
|
||||
from shopdb.extensions import db
|
||||
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 Notification, NotificationType
|
||||
|
||||
notifications_bp = Blueprint('notifications', __name__)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Notification Types
|
||||
# =============================================================================
|
||||
|
||||
@notifications_bp.route('/types', methods=['GET'])
|
||||
def list_notification_types():
|
||||
"""List all notification types."""
|
||||
page, per_page = get_pagination_params(request)
|
||||
|
||||
query = NotificationType.query
|
||||
|
||||
if request.args.get('active', 'true').lower() != 'false':
|
||||
query = query.filter(NotificationType.isactive == True)
|
||||
|
||||
query = query.order_by(NotificationType.typename)
|
||||
|
||||
items, total = paginate_query(query, page, per_page)
|
||||
data = [t.to_dict() for t in items]
|
||||
|
||||
return paginated_response(data, page, per_page, total)
|
||||
|
||||
|
||||
@notifications_bp.route('/types', methods=['POST'])
|
||||
@jwt_required()
|
||||
def create_notification_type():
|
||||
"""Create a new notification type."""
|
||||
data = request.get_json()
|
||||
|
||||
if not data or not data.get('typename'):
|
||||
return error_response(ErrorCodes.VALIDATION_ERROR, 'typename is required')
|
||||
|
||||
if NotificationType.query.filter_by(typename=data['typename']).first():
|
||||
return error_response(
|
||||
ErrorCodes.CONFLICT,
|
||||
f"Notification type '{data['typename']}' already exists",
|
||||
http_code=409
|
||||
)
|
||||
|
||||
t = NotificationType(
|
||||
typename=data['typename'],
|
||||
typedescription=data.get('typedescription') or data.get('description'),
|
||||
typecolor=data.get('typecolor') or data.get('color', '#17a2b8')
|
||||
)
|
||||
|
||||
db.session.add(t)
|
||||
db.session.commit()
|
||||
|
||||
return success_response(t.to_dict(), message='Notification type created', http_code=201)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Notifications CRUD
|
||||
# =============================================================================
|
||||
|
||||
@notifications_bp.route('', methods=['GET'])
|
||||
def list_notifications():
|
||||
"""
|
||||
List all notifications with filtering and pagination.
|
||||
|
||||
Query parameters:
|
||||
- page, per_page: Pagination
|
||||
- active: Filter by active status (default: true)
|
||||
- type_id: Filter by notification type ID
|
||||
- current: Filter to currently active notifications only
|
||||
- search: Search in notification text
|
||||
"""
|
||||
page, per_page = get_pagination_params(request)
|
||||
|
||||
query = Notification.query
|
||||
|
||||
# Active filter
|
||||
if request.args.get('active', 'true').lower() != 'false':
|
||||
query = query.filter(Notification.isactive == True)
|
||||
|
||||
# Type filter
|
||||
if type_id := request.args.get('type_id'):
|
||||
query = query.filter(Notification.notificationtypeid == int(type_id))
|
||||
|
||||
# Current filter (active based on dates)
|
||||
if request.args.get('current', 'false').lower() == 'true':
|
||||
now = datetime.utcnow()
|
||||
query = query.filter(
|
||||
Notification.starttime <= now,
|
||||
db.or_(
|
||||
Notification.endtime.is_(None),
|
||||
Notification.endtime >= now
|
||||
)
|
||||
)
|
||||
|
||||
# Search filter
|
||||
if search := request.args.get('search'):
|
||||
query = query.filter(
|
||||
Notification.notification.ilike(f'%{search}%')
|
||||
)
|
||||
|
||||
# Sorting by start time (newest first)
|
||||
query = query.order_by(Notification.starttime.desc())
|
||||
|
||||
items, total = paginate_query(query, page, per_page)
|
||||
data = [n.to_dict() for n in items]
|
||||
|
||||
return paginated_response(data, page, per_page, total)
|
||||
|
||||
|
||||
@notifications_bp.route('/<int:notification_id>', methods=['GET'])
|
||||
def get_notification(notification_id: int):
|
||||
"""Get a single notification."""
|
||||
n = Notification.query.get(notification_id)
|
||||
|
||||
if not n:
|
||||
return error_response(
|
||||
ErrorCodes.NOT_FOUND,
|
||||
f'Notification with ID {notification_id} not found',
|
||||
http_code=404
|
||||
)
|
||||
|
||||
return success_response(n.to_dict())
|
||||
|
||||
|
||||
@notifications_bp.route('', methods=['POST'])
|
||||
@jwt_required()
|
||||
def create_notification():
|
||||
"""Create a new notification."""
|
||||
data = request.get_json()
|
||||
|
||||
if not data:
|
||||
return error_response(ErrorCodes.VALIDATION_ERROR, 'No data provided')
|
||||
|
||||
# Validate required fields
|
||||
notification_text = data.get('notification') or data.get('message')
|
||||
if not notification_text:
|
||||
return error_response(ErrorCodes.VALIDATION_ERROR, 'notification/message is required')
|
||||
|
||||
# Parse dates
|
||||
starttime = datetime.utcnow()
|
||||
if data.get('starttime') or data.get('startdate'):
|
||||
try:
|
||||
date_str = data.get('starttime') or data.get('startdate')
|
||||
starttime = datetime.fromisoformat(date_str.replace('Z', '+00:00'))
|
||||
except ValueError:
|
||||
return error_response(ErrorCodes.VALIDATION_ERROR, 'Invalid starttime format')
|
||||
|
||||
endtime = None
|
||||
if data.get('endtime') or data.get('enddate'):
|
||||
try:
|
||||
date_str = data.get('endtime') or data.get('enddate')
|
||||
endtime = datetime.fromisoformat(date_str.replace('Z', '+00:00'))
|
||||
except ValueError:
|
||||
return error_response(ErrorCodes.VALIDATION_ERROR, 'Invalid endtime format')
|
||||
|
||||
n = Notification(
|
||||
notification=notification_text,
|
||||
notificationtypeid=data.get('notificationtypeid'),
|
||||
businessunitid=data.get('businessunitid'),
|
||||
appid=data.get('appid'),
|
||||
starttime=starttime,
|
||||
endtime=endtime,
|
||||
ticketnumber=data.get('ticketnumber'),
|
||||
link=data.get('link') or data.get('linkurl'),
|
||||
isactive=True,
|
||||
isshopfloor=data.get('isshopfloor', False),
|
||||
employeesso=data.get('employeesso'),
|
||||
employeename=data.get('employeename')
|
||||
)
|
||||
|
||||
db.session.add(n)
|
||||
db.session.commit()
|
||||
|
||||
return success_response(n.to_dict(), message='Notification created', http_code=201)
|
||||
|
||||
|
||||
@notifications_bp.route('/<int:notification_id>', methods=['PUT'])
|
||||
@jwt_required()
|
||||
def update_notification(notification_id: int):
|
||||
"""Update a notification."""
|
||||
n = Notification.query.get(notification_id)
|
||||
|
||||
if not n:
|
||||
return error_response(
|
||||
ErrorCodes.NOT_FOUND,
|
||||
f'Notification with ID {notification_id} not found',
|
||||
http_code=404
|
||||
)
|
||||
|
||||
data = request.get_json()
|
||||
if not data:
|
||||
return error_response(ErrorCodes.VALIDATION_ERROR, 'No data provided')
|
||||
|
||||
# Update text content
|
||||
if 'notification' in data or 'message' in data:
|
||||
n.notification = data.get('notification') or data.get('message')
|
||||
|
||||
# Update simple fields
|
||||
if 'notificationtypeid' in data:
|
||||
n.notificationtypeid = data['notificationtypeid']
|
||||
if 'businessunitid' in data:
|
||||
n.businessunitid = data['businessunitid']
|
||||
if 'appid' in data:
|
||||
n.appid = data['appid']
|
||||
if 'ticketnumber' in data:
|
||||
n.ticketnumber = data['ticketnumber']
|
||||
if 'link' in data or 'linkurl' in data:
|
||||
n.link = data.get('link') or data.get('linkurl')
|
||||
if 'isactive' in data:
|
||||
n.isactive = data['isactive']
|
||||
if 'isshopfloor' in data:
|
||||
n.isshopfloor = data['isshopfloor']
|
||||
if 'employeesso' in data:
|
||||
n.employeesso = data['employeesso']
|
||||
if 'employeename' in data:
|
||||
n.employeename = data['employeename']
|
||||
|
||||
# Parse and update dates
|
||||
if 'starttime' in data or 'startdate' in data:
|
||||
date_str = data.get('starttime') or data.get('startdate')
|
||||
if date_str:
|
||||
try:
|
||||
n.starttime = datetime.fromisoformat(date_str.replace('Z', '+00:00'))
|
||||
except ValueError:
|
||||
return error_response(ErrorCodes.VALIDATION_ERROR, 'Invalid starttime format')
|
||||
else:
|
||||
n.starttime = datetime.utcnow()
|
||||
|
||||
if 'endtime' in data or 'enddate' in data:
|
||||
date_str = data.get('endtime') or data.get('enddate')
|
||||
if date_str:
|
||||
try:
|
||||
n.endtime = datetime.fromisoformat(date_str.replace('Z', '+00:00'))
|
||||
except ValueError:
|
||||
return error_response(ErrorCodes.VALIDATION_ERROR, 'Invalid endtime format')
|
||||
else:
|
||||
n.endtime = None
|
||||
|
||||
db.session.commit()
|
||||
return success_response(n.to_dict(), message='Notification updated')
|
||||
|
||||
|
||||
@notifications_bp.route('/<int:notification_id>', methods=['DELETE'])
|
||||
@jwt_required()
|
||||
def delete_notification(notification_id: int):
|
||||
"""Delete (soft delete) a notification."""
|
||||
n = Notification.query.get(notification_id)
|
||||
|
||||
if not n:
|
||||
return error_response(
|
||||
ErrorCodes.NOT_FOUND,
|
||||
f'Notification with ID {notification_id} not found',
|
||||
http_code=404
|
||||
)
|
||||
|
||||
n.isactive = False
|
||||
db.session.commit()
|
||||
|
||||
return success_response(message='Notification deleted')
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Special Endpoints
|
||||
# =============================================================================
|
||||
|
||||
@notifications_bp.route('/active', methods=['GET'])
|
||||
def get_active_notifications():
|
||||
"""
|
||||
Get currently active notifications for display.
|
||||
"""
|
||||
now = datetime.utcnow()
|
||||
|
||||
notifications = Notification.query.filter(
|
||||
Notification.isactive == True,
|
||||
db.or_(
|
||||
Notification.starttime.is_(None),
|
||||
Notification.starttime <= now
|
||||
),
|
||||
db.or_(
|
||||
Notification.endtime.is_(None),
|
||||
Notification.endtime >= now
|
||||
)
|
||||
).order_by(Notification.starttime.desc()).all()
|
||||
|
||||
data = [n.to_dict() for n in notifications]
|
||||
|
||||
return success_response({
|
||||
'notifications': data,
|
||||
'total': len(data)
|
||||
})
|
||||
|
||||
|
||||
@notifications_bp.route('/calendar', methods=['GET'])
|
||||
def get_calendar_events():
|
||||
"""
|
||||
Get notifications in FullCalendar event format.
|
||||
|
||||
Query parameters:
|
||||
- start: Start date (ISO format)
|
||||
- end: End date (ISO format)
|
||||
"""
|
||||
query = Notification.query.filter(Notification.isactive == True)
|
||||
|
||||
# Date range filter
|
||||
if start := request.args.get('start'):
|
||||
try:
|
||||
start_date = datetime.fromisoformat(start.replace('Z', '+00:00'))
|
||||
query = query.filter(
|
||||
db.or_(
|
||||
Notification.endtime >= start_date,
|
||||
Notification.endtime.is_(None)
|
||||
)
|
||||
)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
if end := request.args.get('end'):
|
||||
try:
|
||||
end_date = datetime.fromisoformat(end.replace('Z', '+00:00'))
|
||||
query = query.filter(Notification.starttime <= end_date)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
notifications = query.order_by(Notification.starttime).all()
|
||||
|
||||
events = [n.to_calendar_event() for n in notifications]
|
||||
|
||||
return success_response(events)
|
||||
|
||||
|
||||
@notifications_bp.route('/dashboard/summary', methods=['GET'])
|
||||
def dashboard_summary():
|
||||
"""Get notifications dashboard summary."""
|
||||
now = datetime.utcnow()
|
||||
|
||||
# Total active notifications
|
||||
total_active = Notification.query.filter(
|
||||
Notification.isactive == True,
|
||||
db.or_(
|
||||
Notification.starttime.is_(None),
|
||||
Notification.starttime <= now
|
||||
),
|
||||
db.or_(
|
||||
Notification.endtime.is_(None),
|
||||
Notification.endtime >= now
|
||||
)
|
||||
).count()
|
||||
|
||||
# By type
|
||||
by_type = db.session.query(
|
||||
NotificationType.typename,
|
||||
NotificationType.typecolor,
|
||||
db.func.count(Notification.notificationid)
|
||||
).join(Notification
|
||||
).filter(
|
||||
Notification.isactive == True
|
||||
).group_by(NotificationType.typename, NotificationType.typecolor
|
||||
).all()
|
||||
|
||||
return success_response({
|
||||
'active': total_active,
|
||||
'by_type': [{'type': t, 'color': c, 'count': n} for t, c, n in by_type]
|
||||
})
|
||||
|
||||
|
||||
@notifications_bp.route('/employee/<sso>', methods=['GET'])
|
||||
def get_employee_recognitions(sso):
|
||||
"""
|
||||
Get recognitions for a specific employee by SSO.
|
||||
|
||||
Returns all recognition-type notifications where the employee is mentioned.
|
||||
"""
|
||||
if not sso or not sso.isdigit():
|
||||
return error_response(ErrorCodes.VALIDATION_ERROR, 'Valid SSO required')
|
||||
|
||||
# Find recognition type(s)
|
||||
recognition_types = NotificationType.query.filter(
|
||||
db.or_(
|
||||
NotificationType.typecolor == 'recognition',
|
||||
NotificationType.typename.ilike('%recognition%')
|
||||
)
|
||||
).all()
|
||||
|
||||
recognition_type_ids = [rt.notificationtypeid for rt in recognition_types]
|
||||
|
||||
# Find notifications where this employee is mentioned
|
||||
# Check both exact match and comma-separated list
|
||||
query = Notification.query.filter(
|
||||
Notification.isactive == True,
|
||||
db.or_(
|
||||
Notification.employeesso == sso,
|
||||
Notification.employeesso.like(f'{sso},%'),
|
||||
Notification.employeesso.like(f'%,{sso}'),
|
||||
Notification.employeesso.like(f'%,{sso},%')
|
||||
)
|
||||
)
|
||||
|
||||
# Optionally filter to recognition types only
|
||||
if recognition_type_ids:
|
||||
query = query.filter(Notification.notificationtypeid.in_(recognition_type_ids))
|
||||
|
||||
query = query.order_by(Notification.starttime.desc())
|
||||
|
||||
notifications = query.all()
|
||||
data = [n.to_dict() for n in notifications]
|
||||
|
||||
return success_response({
|
||||
'recognitions': data,
|
||||
'total': len(data)
|
||||
})
|
||||
|
||||
|
||||
@notifications_bp.route('/shopfloor', methods=['GET'])
|
||||
def get_shopfloor_notifications():
|
||||
"""
|
||||
Get notifications for shopfloor TV dashboard.
|
||||
|
||||
Returns current and upcoming notifications with isshopfloor=1.
|
||||
Splits multi-employee recognition into separate entries.
|
||||
|
||||
Query parameters:
|
||||
- businessunit: Filter by business unit ID (null = all units)
|
||||
"""
|
||||
from datetime import timedelta
|
||||
|
||||
now = datetime.utcnow()
|
||||
business_unit = request.args.get('businessunit')
|
||||
|
||||
# Base query for shopfloor notifications
|
||||
base_query = Notification.query.filter(Notification.isshopfloor == True)
|
||||
|
||||
# Business unit filter
|
||||
if business_unit and business_unit.isdigit():
|
||||
# Specific BU: show that BU's notifications AND null (all units)
|
||||
base_query = base_query.filter(
|
||||
db.or_(
|
||||
Notification.businessunitid == int(business_unit),
|
||||
Notification.businessunitid.is_(None)
|
||||
)
|
||||
)
|
||||
else:
|
||||
# All units: only show notifications with NULL businessunitid
|
||||
base_query = base_query.filter(Notification.businessunitid.is_(None))
|
||||
|
||||
# Current notifications (active now or ended within 30 minutes)
|
||||
thirty_min_ago = now - timedelta(minutes=30)
|
||||
current_query = base_query.filter(
|
||||
db.or_(
|
||||
# Active and currently showing
|
||||
db.and_(
|
||||
Notification.isactive == True,
|
||||
db.or_(Notification.starttime.is_(None), Notification.starttime <= now),
|
||||
db.or_(Notification.endtime.is_(None), Notification.endtime >= now)
|
||||
),
|
||||
# Recently ended (within 30 min) - show as resolved
|
||||
db.and_(
|
||||
Notification.endtime.isnot(None),
|
||||
Notification.endtime >= thirty_min_ago,
|
||||
Notification.endtime < now
|
||||
)
|
||||
)
|
||||
).order_by(Notification.notificationid.desc())
|
||||
|
||||
current_notifications = current_query.all()
|
||||
|
||||
# Upcoming notifications (starts within next 5 days)
|
||||
five_days = now + timedelta(days=5)
|
||||
upcoming_query = base_query.filter(
|
||||
Notification.isactive == True,
|
||||
Notification.starttime > now,
|
||||
Notification.starttime <= five_days
|
||||
).order_by(Notification.starttime)
|
||||
|
||||
upcoming_notifications = upcoming_query.all()
|
||||
|
||||
def notification_to_shopfloor(n, employee_override=None):
|
||||
"""Convert notification to shopfloor format."""
|
||||
is_resolved = n.endtime and n.endtime < now
|
||||
|
||||
result = {
|
||||
'notificationid': n.notificationid,
|
||||
'notification': n.notification,
|
||||
'starttime': n.starttime.isoformat() if n.starttime else None,
|
||||
'endtime': n.endtime.isoformat() if n.endtime else None,
|
||||
'ticketnumber': n.ticketnumber,
|
||||
'link': n.link,
|
||||
'isactive': n.isactive,
|
||||
'isshopfloor': True,
|
||||
'resolved': is_resolved,
|
||||
'typename': n.notificationtype.typename if n.notificationtype else None,
|
||||
'typecolor': n.notificationtype.typecolor if n.notificationtype else None,
|
||||
}
|
||||
|
||||
# Employee info
|
||||
if employee_override:
|
||||
result['employeesso'] = employee_override.get('sso')
|
||||
result['employeename'] = employee_override.get('name')
|
||||
result['employeepicture'] = employee_override.get('picture')
|
||||
else:
|
||||
result['employeesso'] = n.employeesso
|
||||
result['employeename'] = n.employeename
|
||||
result['employeepicture'] = None
|
||||
|
||||
# Try to get picture from wjf_employees
|
||||
if n.employeesso and n.employeesso.isdigit():
|
||||
try:
|
||||
import pymysql
|
||||
conn = pymysql.connect(
|
||||
host='localhost', user='root', password='rootpassword',
|
||||
database='wjf_employees', cursorclass=pymysql.cursors.DictCursor
|
||||
)
|
||||
with conn.cursor() as cur:
|
||||
cur.execute('SELECT Picture FROM employees WHERE SSO = %s', (int(n.employeesso),))
|
||||
emp = cur.fetchone()
|
||||
if emp and emp.get('Picture'):
|
||||
result['employeepicture'] = emp['Picture']
|
||||
conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return result
|
||||
|
||||
# Process current notifications (split multi-employee recognition)
|
||||
current_data = []
|
||||
for n in current_notifications:
|
||||
is_recognition = n.notificationtype and n.notificationtype.typecolor == 'recognition'
|
||||
|
||||
if is_recognition and n.employeesso and ',' in n.employeesso:
|
||||
# Split into individual cards for each employee
|
||||
ssos = [s.strip() for s in n.employeesso.split(',')]
|
||||
names = n.employeename.split(', ') if n.employeename else []
|
||||
|
||||
for i, sso in enumerate(ssos):
|
||||
name = names[i] if i < len(names) else sso
|
||||
|
||||
# Look up picture
|
||||
picture = None
|
||||
if sso.isdigit():
|
||||
try:
|
||||
import pymysql
|
||||
conn = pymysql.connect(
|
||||
host='localhost', user='root', password='rootpassword',
|
||||
database='wjf_employees', cursorclass=pymysql.cursors.DictCursor
|
||||
)
|
||||
with conn.cursor() as cur:
|
||||
cur.execute('SELECT Picture FROM employees WHERE SSO = %s', (int(sso),))
|
||||
emp = cur.fetchone()
|
||||
if emp:
|
||||
picture = emp.get('Picture')
|
||||
conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
current_data.append(notification_to_shopfloor(n, {
|
||||
'sso': sso,
|
||||
'name': name,
|
||||
'picture': picture
|
||||
}))
|
||||
else:
|
||||
current_data.append(notification_to_shopfloor(n))
|
||||
|
||||
# Process upcoming notifications
|
||||
upcoming_data = []
|
||||
for n in upcoming_notifications:
|
||||
is_recognition = n.notificationtype and n.notificationtype.typecolor == 'recognition'
|
||||
|
||||
if is_recognition and n.employeesso and ',' in n.employeesso:
|
||||
ssos = [s.strip() for s in n.employeesso.split(',')]
|
||||
names = n.employeename.split(', ') if n.employeename else []
|
||||
|
||||
for i, sso in enumerate(ssos):
|
||||
name = names[i] if i < len(names) else sso
|
||||
picture = None
|
||||
if sso.isdigit():
|
||||
try:
|
||||
import pymysql
|
||||
conn = pymysql.connect(
|
||||
host='localhost', user='root', password='rootpassword',
|
||||
database='wjf_employees', cursorclass=pymysql.cursors.DictCursor
|
||||
)
|
||||
with conn.cursor() as cur:
|
||||
cur.execute('SELECT Picture FROM employees WHERE SSO = %s', (int(sso),))
|
||||
emp = cur.fetchone()
|
||||
if emp:
|
||||
picture = emp.get('Picture')
|
||||
conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
upcoming_data.append(notification_to_shopfloor(n, {
|
||||
'sso': sso,
|
||||
'name': name,
|
||||
'picture': picture
|
||||
}))
|
||||
else:
|
||||
upcoming_data.append(notification_to_shopfloor(n))
|
||||
|
||||
return success_response({
|
||||
'timestamp': now.isoformat(),
|
||||
'current': current_data,
|
||||
'upcoming': upcoming_data
|
||||
})
|
||||
12
plugins/notifications/manifest.json
Normal file
12
plugins/notifications/manifest.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "notifications",
|
||||
"version": "1.0.0",
|
||||
"description": "Notifications and announcements management plugin",
|
||||
"author": "ShopDB Team",
|
||||
"dependencies": [],
|
||||
"core_version": ">=1.0.0",
|
||||
"api_prefix": "/api/notifications",
|
||||
"provides": {
|
||||
"features": ["notifications", "announcements", "calendar_events"]
|
||||
}
|
||||
}
|
||||
5
plugins/notifications/models/__init__.py
Normal file
5
plugins/notifications/models/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
"""Notifications plugin models."""
|
||||
|
||||
from .notification import Notification, NotificationType
|
||||
|
||||
__all__ = ['Notification', 'NotificationType']
|
||||
157
plugins/notifications/models/notification.py
Normal file
157
plugins/notifications/models/notification.py
Normal file
@@ -0,0 +1,157 @@
|
||||
"""Notifications plugin models - adapted to existing database schema."""
|
||||
|
||||
from datetime import datetime
|
||||
from shopdb.extensions import db
|
||||
|
||||
|
||||
class NotificationType(db.Model):
|
||||
"""
|
||||
Notification type classification.
|
||||
Matches existing notificationtypes table.
|
||||
"""
|
||||
__tablename__ = 'notificationtypes'
|
||||
|
||||
notificationtypeid = db.Column(db.Integer, primary_key=True)
|
||||
typename = db.Column(db.String(50), nullable=False)
|
||||
typedescription = db.Column(db.Text)
|
||||
typecolor = db.Column(db.String(20), default='#17a2b8')
|
||||
isactive = db.Column(db.Boolean, default=True)
|
||||
|
||||
def __repr__(self):
|
||||
return f"<NotificationType {self.typename}>"
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
'notificationtypeid': self.notificationtypeid,
|
||||
'typename': self.typename,
|
||||
'typedescription': self.typedescription,
|
||||
'typecolor': self.typecolor,
|
||||
'isactive': self.isactive
|
||||
}
|
||||
|
||||
|
||||
class Notification(db.Model):
|
||||
"""
|
||||
Notification/announcement model.
|
||||
Matches existing notifications table schema.
|
||||
"""
|
||||
__tablename__ = 'notifications'
|
||||
|
||||
notificationid = db.Column(db.Integer, primary_key=True)
|
||||
notificationtypeid = db.Column(
|
||||
db.Integer,
|
||||
db.ForeignKey('notificationtypes.notificationtypeid'),
|
||||
nullable=True
|
||||
)
|
||||
businessunitid = db.Column(db.Integer, nullable=True)
|
||||
appid = db.Column(db.Integer, nullable=True)
|
||||
notification = db.Column(db.Text, nullable=False, comment='The message content')
|
||||
starttime = db.Column(db.DateTime, nullable=True)
|
||||
endtime = db.Column(db.DateTime, nullable=True)
|
||||
ticketnumber = db.Column(db.String(50), nullable=True)
|
||||
link = db.Column(db.String(500), nullable=True)
|
||||
isactive = db.Column(db.Boolean, default=True)
|
||||
isshopfloor = db.Column(db.Boolean, default=False)
|
||||
employeesso = db.Column(db.String(100), nullable=True)
|
||||
employeename = db.Column(db.String(100), nullable=True)
|
||||
|
||||
# Relationships
|
||||
notificationtype = db.relationship('NotificationType', backref='notifications')
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Notification {self.notificationid}>"
|
||||
|
||||
@property
|
||||
def is_current(self):
|
||||
"""Check if notification is currently active based on dates."""
|
||||
now = datetime.utcnow()
|
||||
if not self.isactive:
|
||||
return False
|
||||
if self.starttime and now < self.starttime:
|
||||
return False
|
||||
if self.endtime and now > self.endtime:
|
||||
return False
|
||||
return True
|
||||
|
||||
@property
|
||||
def title(self):
|
||||
"""Get title - first line or first 100 chars of notification."""
|
||||
if not self.notification:
|
||||
return ''
|
||||
lines = self.notification.split('\n')
|
||||
return lines[0][:100] if lines else self.notification[:100]
|
||||
|
||||
def to_dict(self):
|
||||
"""Convert to dictionary with related data."""
|
||||
result = {
|
||||
'notificationid': self.notificationid,
|
||||
'notificationtypeid': self.notificationtypeid,
|
||||
'businessunitid': self.businessunitid,
|
||||
'appid': self.appid,
|
||||
'notification': self.notification,
|
||||
'title': self.title,
|
||||
'message': self.notification,
|
||||
'starttime': self.starttime.isoformat() if self.starttime else None,
|
||||
'endtime': self.endtime.isoformat() if self.endtime else None,
|
||||
'startdate': self.starttime.isoformat() if self.starttime else None,
|
||||
'enddate': self.endtime.isoformat() if self.endtime else None,
|
||||
'ticketnumber': self.ticketnumber,
|
||||
'link': self.link,
|
||||
'linkurl': self.link,
|
||||
'isactive': bool(self.isactive) if self.isactive is not None else True,
|
||||
'isshopfloor': bool(self.isshopfloor) if self.isshopfloor is not None else False,
|
||||
'employeesso': self.employeesso,
|
||||
'employeename': self.employeename,
|
||||
'iscurrent': self.is_current
|
||||
}
|
||||
|
||||
# Add type info
|
||||
if self.notificationtype:
|
||||
result['typename'] = self.notificationtype.typename
|
||||
result['typecolor'] = self.notificationtype.typecolor
|
||||
|
||||
return result
|
||||
|
||||
def to_calendar_event(self):
|
||||
"""Convert to FullCalendar event format."""
|
||||
# Map Bootstrap color names to hex colors
|
||||
color_map = {
|
||||
'success': '#04b962',
|
||||
'warning': '#ff8800',
|
||||
'danger': '#f5365c',
|
||||
'info': '#14abef',
|
||||
'primary': '#7934f3',
|
||||
'secondary': '#94614f',
|
||||
'recognition': '#14abef', # Blue for recognition
|
||||
}
|
||||
|
||||
raw_color = self.notificationtype.typecolor if self.notificationtype else 'info'
|
||||
# Use mapped color if it's a Bootstrap name, otherwise use as-is (hex)
|
||||
color = color_map.get(raw_color, raw_color if raw_color.startswith('#') else '#14abef')
|
||||
|
||||
# For recognition notifications, include employee name (or SSO as fallback) in title
|
||||
title = self.title
|
||||
if raw_color == 'recognition':
|
||||
employee_display = self.employeename or self.employeesso
|
||||
if employee_display:
|
||||
title = f"{employee_display}: {title}"
|
||||
|
||||
return {
|
||||
'id': self.notificationid,
|
||||
'title': title,
|
||||
'start': self.starttime.isoformat() if self.starttime else None,
|
||||
'end': self.endtime.isoformat() if self.endtime else None,
|
||||
'allDay': True,
|
||||
'backgroundColor': color,
|
||||
'borderColor': color,
|
||||
'extendedProps': {
|
||||
'notificationid': self.notificationid,
|
||||
'message': self.notification,
|
||||
'typename': self.notificationtype.typename if self.notificationtype else None,
|
||||
'typecolor': raw_color,
|
||||
'linkurl': self.link,
|
||||
'ticketnumber': self.ticketnumber,
|
||||
'employeename': self.employeename,
|
||||
'employeesso': self.employeesso,
|
||||
}
|
||||
}
|
||||
204
plugins/notifications/plugin.py
Normal file
204
plugins/notifications/plugin.py
Normal file
@@ -0,0 +1,204 @@
|
||||
"""Notifications 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 .models import Notification, NotificationType
|
||||
from .api import notifications_bp
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class NotificationsPlugin(BasePlugin):
|
||||
"""
|
||||
Notifications plugin - manages announcements and notifications.
|
||||
|
||||
Provides functionality for:
|
||||
- Creating and managing notifications/announcements
|
||||
- Displaying banner notifications
|
||||
- Calendar view of notifications
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self._manifest = self._load_manifest()
|
||||
|
||||
def _load_manifest(self) -> Dict:
|
||||
"""Load plugin manifest from JSON file."""
|
||||
manifest_path = Path(__file__).parent / 'manifest.json'
|
||||
if manifest_path.exists():
|
||||
with open(manifest_path, 'r') as f:
|
||||
return json.load(f)
|
||||
return {}
|
||||
|
||||
@property
|
||||
def meta(self) -> PluginMeta:
|
||||
"""Return plugin metadata."""
|
||||
return PluginMeta(
|
||||
name=self._manifest.get('name', 'notifications'),
|
||||
version=self._manifest.get('version', '1.0.0'),
|
||||
description=self._manifest.get(
|
||||
'description',
|
||||
'Notifications and announcements management'
|
||||
),
|
||||
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/notifications'),
|
||||
)
|
||||
|
||||
def get_blueprint(self) -> Optional[Blueprint]:
|
||||
"""Return Flask Blueprint with API routes."""
|
||||
return notifications_bp
|
||||
|
||||
def get_models(self) -> List[Type]:
|
||||
"""Return list of SQLAlchemy model classes."""
|
||||
return [Notification, NotificationType]
|
||||
|
||||
def init_app(self, app: Flask, db_instance) -> None:
|
||||
"""Initialize plugin with Flask app."""
|
||||
logger.info(f"Notifications plugin initialized (v{self.meta.version})")
|
||||
|
||||
def on_install(self, app: Flask) -> None:
|
||||
"""Called when plugin is installed."""
|
||||
with app.app_context():
|
||||
self._ensure_notification_types()
|
||||
logger.info("Notifications plugin installed")
|
||||
|
||||
def _ensure_notification_types(self) -> None:
|
||||
"""Ensure default notification types exist."""
|
||||
default_types = [
|
||||
('Awareness', 'General awareness notification', '#17a2b8', 'info-circle'),
|
||||
('Change', 'Planned change notification', '#ffc107', 'exchange-alt'),
|
||||
('Incident', 'Incident or outage notification', '#dc3545', 'exclamation-triangle'),
|
||||
('Maintenance', 'Scheduled maintenance notification', '#6c757d', 'wrench'),
|
||||
('General', 'General announcement', '#28a745', 'bullhorn'),
|
||||
]
|
||||
|
||||
for typename, description, color, icon in default_types:
|
||||
existing = NotificationType.query.filter_by(typename=typename).first()
|
||||
if not existing:
|
||||
t = NotificationType(
|
||||
typename=typename,
|
||||
description=description,
|
||||
color=color,
|
||||
icon=icon
|
||||
)
|
||||
db.session.add(t)
|
||||
logger.debug(f"Created notification type: {typename}")
|
||||
|
||||
db.session.commit()
|
||||
|
||||
def on_uninstall(self, app: Flask) -> None:
|
||||
"""Called when plugin is uninstalled."""
|
||||
logger.info("Notifications plugin uninstalled")
|
||||
|
||||
def get_cli_commands(self) -> List:
|
||||
"""Return CLI commands for this plugin."""
|
||||
|
||||
@click.group('notifications')
|
||||
def notifications_cli():
|
||||
"""Notifications plugin commands."""
|
||||
pass
|
||||
|
||||
@notifications_cli.command('list-types')
|
||||
def list_types():
|
||||
"""List all notification types."""
|
||||
from flask import current_app
|
||||
|
||||
with current_app.app_context():
|
||||
types = NotificationType.query.filter_by(isactive=True).all()
|
||||
if not types:
|
||||
click.echo('No notification types found.')
|
||||
return
|
||||
|
||||
click.echo('Notification Types:')
|
||||
for t in types:
|
||||
click.echo(f" [{t.notificationtypeid}] {t.typename} ({t.color})")
|
||||
|
||||
@notifications_cli.command('stats')
|
||||
def stats():
|
||||
"""Show notification statistics."""
|
||||
from flask import current_app
|
||||
from datetime import datetime
|
||||
|
||||
with current_app.app_context():
|
||||
now = datetime.utcnow()
|
||||
|
||||
total = Notification.query.filter(
|
||||
Notification.isactive == True
|
||||
).count()
|
||||
|
||||
active = Notification.query.filter(
|
||||
Notification.isactive == True,
|
||||
Notification.startdate <= now,
|
||||
db.or_(
|
||||
Notification.enddate.is_(None),
|
||||
Notification.enddate >= now
|
||||
)
|
||||
).count()
|
||||
|
||||
click.echo(f"Total notifications: {total}")
|
||||
click.echo(f"Currently active: {active}")
|
||||
|
||||
@notifications_cli.command('create')
|
||||
@click.option('--title', required=True, help='Notification title')
|
||||
@click.option('--message', required=True, help='Notification message')
|
||||
@click.option('--type', 'type_name', default='General', help='Notification type')
|
||||
def create_notification(title, message, type_name):
|
||||
"""Create a new notification."""
|
||||
from flask import current_app
|
||||
|
||||
with current_app.app_context():
|
||||
ntype = NotificationType.query.filter_by(typename=type_name).first()
|
||||
if not ntype:
|
||||
click.echo(f"Error: Notification type '{type_name}' not found.")
|
||||
return
|
||||
|
||||
n = Notification(
|
||||
title=title,
|
||||
message=message,
|
||||
notificationtypeid=ntype.notificationtypeid
|
||||
)
|
||||
db.session.add(n)
|
||||
db.session.commit()
|
||||
|
||||
click.echo(f"Created notification #{n.notificationid}: {title}")
|
||||
|
||||
return [notifications_cli]
|
||||
|
||||
def get_dashboard_widgets(self) -> List[Dict]:
|
||||
"""Return dashboard widget definitions."""
|
||||
return [
|
||||
{
|
||||
'name': 'Active Notifications',
|
||||
'component': 'NotificationsWidget',
|
||||
'endpoint': '/api/notifications/dashboard/summary',
|
||||
'size': 'small',
|
||||
'position': 1,
|
||||
},
|
||||
]
|
||||
|
||||
def get_navigation_items(self) -> List[Dict]:
|
||||
"""Return navigation menu items."""
|
||||
return [
|
||||
{
|
||||
'name': 'Notifications',
|
||||
'icon': 'bell',
|
||||
'route': '/notifications',
|
||||
'position': 5,
|
||||
},
|
||||
{
|
||||
'name': 'Calendar',
|
||||
'icon': 'calendar',
|
||||
'route': '/calendar',
|
||||
'position': 6,
|
||||
},
|
||||
]
|
||||
Reference in New Issue
Block a user