-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
57 lines (50 loc) · 1.96 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from models import WorkOrder, db
from app import socketio
from flask import url_for
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def generate_work_order(maintenance_log):
# Simple logic to generate a work order based on maintenance log
is_urgent = "urgent" in maintenance_log.details.lower() or "immediate attention" in maintenance_log.details.lower()
priority = "Urgent" if is_urgent else "Normal"
work_order = WorkOrder(
title=f"{'Urgent: ' if is_urgent else ''}Maintenance required for Lot {maintenance_log.lot}",
description=f"Based on maintenance log: {maintenance_log.details}",
status="Pending",
priority=priority,
created_by=maintenance_log.user_id
)
db.session.add(work_order)
db.session.commit()
logger.info(f"Work order created: {work_order.id}, Is urgent: {is_urgent}")
# Send a notification for all work orders, with emphasis on urgent ones
send_notification(
work_order.title,
'danger' if is_urgent else 'info',
{
'id': work_order.id,
'description': work_order.description,
'priority': work_order.priority,
'url': url_for('view_workorder', id=work_order.id)
}
)
def send_notification(message, category, data=None):
logger.info(f"Sending notification: {message}, Category: {category}, Data: {data}")
socketio.emit('notification', {
'message': message,
'category': category,
'data': data
}, namespace='/notifications')
logger.info("Notification sent")
def send_urgent_notification(work_order):
send_notification(
f"Urgent: New work order for Lot {work_order.maintenance_log.lot}",
'danger',
{
'id': work_order.id,
'description': work_order.description,
'priority': work_order.priority,
'url': url_for('view_workorder', id=work_order.id)
}
)