Prepare to send E-mail notifications
This commit is contained in:
parent
e5d9d051ea
commit
7548de7609
@ -6,6 +6,7 @@ from BreCal.database.update_database import evaluate_shipcall_state
|
|||||||
from BreCal.database.sql_queries import create_sql_query_shipcall_get
|
from BreCal.database.sql_queries import create_sql_query_shipcall_get
|
||||||
from BreCal.database.sql_queries import SQLQuery
|
from BreCal.database.sql_queries import SQLQuery
|
||||||
from BreCal.database.sql_utils import get_notification_for_shipcall_and_type
|
from BreCal.database.sql_utils import get_notification_for_shipcall_and_type
|
||||||
|
from BreCal.services.email_handling import EmailHandler
|
||||||
|
|
||||||
import threading
|
import threading
|
||||||
import schedule
|
import schedule
|
||||||
@ -66,6 +67,74 @@ def UpdateNotifications():
|
|||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
logging.error(ex)
|
logging.error(ex)
|
||||||
|
|
||||||
|
def SendNotifications():
|
||||||
|
# perhaps this will be moved somewhere else later
|
||||||
|
try:
|
||||||
|
# find all notifications in level 1
|
||||||
|
pooledConnection = getPoolConnection()
|
||||||
|
query = "SELECT * from notification WHERE level = 1"
|
||||||
|
commands = pydapper.using(pooledConnection)
|
||||||
|
data = commands.query(query, model=model.Notification)
|
||||||
|
if len(data) == 0:
|
||||||
|
return
|
||||||
|
|
||||||
|
# cache participants and users for performance beforehand
|
||||||
|
query = "SELECT * from participant";
|
||||||
|
participants = commands.query(query, model=model.Participant)
|
||||||
|
|
||||||
|
email_dict = dict()
|
||||||
|
users_dict = dict()
|
||||||
|
user_query = "SELECT * from user"
|
||||||
|
users = commands.query(user_query, model=model.User)
|
||||||
|
for participant in participants:
|
||||||
|
for user in users:
|
||||||
|
if user.participant_id == participant.id:
|
||||||
|
if not participant.id in users_dict:
|
||||||
|
users_dict[participant.id] = []
|
||||||
|
users_dict[participant.id].append(user)
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
|
for notification in data:
|
||||||
|
if notification.participant_id not in users_dict:
|
||||||
|
continue
|
||||||
|
users = users_dict[notification.participant_id]
|
||||||
|
for user in users:
|
||||||
|
# send notification to user
|
||||||
|
if user.notify_email:
|
||||||
|
match notification.type:
|
||||||
|
case 1: # assignment
|
||||||
|
email_dict[user] += "\n"
|
||||||
|
email_dict[user] += "You have been assigned to a new shipcall: " + notification.message
|
||||||
|
case 2: # next 24 hours
|
||||||
|
email_dict[user] += "\n"
|
||||||
|
email_dict[user] += "A shipcall is scheduled for the next 24 hours: " + notification.message
|
||||||
|
case 3: # Time conflict
|
||||||
|
email_dict[user] += "\n"
|
||||||
|
email_dict[user] += "There is a time conflict in a shipcall: " + notification.message
|
||||||
|
case 4: # Time conflict resolved
|
||||||
|
email_dict[user] += "\n"
|
||||||
|
email_dict[user] += "A time conflict in a shipcall has been resolved: " + notification.message
|
||||||
|
case 5: # unassigned
|
||||||
|
email_dict[user] += "\n"
|
||||||
|
email_dict[user] += "You have been unassigned from a shipcall: " + notification.message
|
||||||
|
if user.notify_whatsapp:
|
||||||
|
# TBD
|
||||||
|
pass
|
||||||
|
if user.notify_signal:
|
||||||
|
# TBD
|
||||||
|
pass
|
||||||
|
# mark as sent
|
||||||
|
commands.execute("UPDATE notification SET level = 2 WHERE id = ?id?", param={"id":notification.id})
|
||||||
|
|
||||||
|
# send emails
|
||||||
|
if len(email_dict) > 0:
|
||||||
|
email_handler = EmailHandler(mail_server="smtp.gmail.com", mail_port=465, mail_address="")
|
||||||
|
for user, message in email_dict.items():
|
||||||
|
email_handler.send_email(user.user_email, "BreCal Notification", message)
|
||||||
|
except Exception as ex:
|
||||||
|
logging.error(ex)
|
||||||
|
|
||||||
def add_function_to_schedule__update_shipcalls(interval_in_minutes:int, options:dict={'past_days':2}):
|
def add_function_to_schedule__update_shipcalls(interval_in_minutes:int, options:dict={'past_days':2}):
|
||||||
kwargs_ = {"options":options}
|
kwargs_ = {"options":options}
|
||||||
schedule.every(interval_in_minutes).minutes.do(UpdateShipcalls, **kwargs_)
|
schedule.every(interval_in_minutes).minutes.do(UpdateShipcalls, **kwargs_)
|
||||||
@ -75,8 +144,8 @@ def add_function_to_evaluate_notifications(interval_in_minutes:int=1):
|
|||||||
schedule.every(interval_in_minutes).minutes.do(UpdateNotifications)
|
schedule.every(interval_in_minutes).minutes.do(UpdateNotifications)
|
||||||
return
|
return
|
||||||
|
|
||||||
def add_function_to_schedule__send_notifications(vr, interval_in_minutes:int=10):
|
def add_function_to_schedule_send_notifications(interval_in_minutes:int=10):
|
||||||
schedule.every(interval_in_minutes).minutes.do(vr.notifier.send_notifications)
|
schedule.every(interval_in_minutes).minutes.do(SendNotifications)
|
||||||
return
|
return
|
||||||
|
|
||||||
def eval_next_24_hrs():
|
def eval_next_24_hrs():
|
||||||
@ -93,8 +162,8 @@ def eval_next_24_hrs():
|
|||||||
for participant in participants:
|
for participant in participants:
|
||||||
if participant["type"] == 1: # BSMD
|
if participant["type"] == 1: # BSMD
|
||||||
continue
|
continue
|
||||||
if participant["type"] == 32: # PORT AUTHORITY
|
# if participant["type"] == 32: # PORT AUTHORITY # Christin: Brake möchte sie vielleicht doch haben
|
||||||
continue
|
# continue
|
||||||
# check if "open" notification already exists
|
# check if "open" notification already exists
|
||||||
found_notification = False
|
found_notification = False
|
||||||
for existing_notification in existing_notifications:
|
for existing_notification in existing_notifications:
|
||||||
@ -122,8 +191,7 @@ def setup_schedule(update_shipcalls_interval_in_minutes:int=60):
|
|||||||
|
|
||||||
schedule.every().day.at("09:00").do(eval_next_24_hrs)
|
schedule.every().day.at("09:00").do(eval_next_24_hrs)
|
||||||
|
|
||||||
# placeholder: create/send notifications
|
add_function_to_schedule_send_notifications(1)
|
||||||
# add_function_to_schedule__send_notifications(...)
|
|
||||||
|
|
||||||
# TODO: Add schedule function to evaluate all notifications in level 1 and create actions
|
# TODO: Add schedule function to evaluate all notifications in level 1 and create actions
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user