fixed some smaller issues

This commit is contained in:
Daniel Schick 2024-12-17 14:51:04 +01:00
parent 47da3ff475
commit 622ab6b4a3
2 changed files with 90 additions and 31 deletions

View File

@ -81,7 +81,7 @@ def SendEmails(email_dict):
commands = pydapper.using(pooledConnection)
conn = smtplib.SMTP(defs.email_credentials["server"], defs.email_credentials["port"])
conn.set_debuglevel(1)
conn.set_debuglevel(1) # set this to 0 to disable debug output to log
conn.ehlo()
conn.starttls()
conn.ehlo()
@ -107,7 +107,10 @@ def SendEmails(email_dict):
for notification in notifications:
message_type = defs.message_types[notification.type]
message_type = next((x for x in defs.message_types if x["type"] == notification.type), None)
if message_type is None:
logging.error(f"Message type {notification.type} not found")
continue
with open(os.path.join(current_path,'../msg/notification_element.html'), mode="r", encoding="utf-8") as file:
element = file.read()
@ -185,21 +188,38 @@ def SendNotifications():
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:
if user not in email_dict:
email_dict[user] = []
email_dict[user].append(notification)
if user.notify_whatsapp:
# TBD
pass
if user.notify_signal:
# TBD
pass
if not notification.participant_id: # no participant defined, this update goes to all participants of this shipcall
p_query = "SELECT * from shipcall_participant_map where shipcall_id = ?id?"
assigned_participants = commands.query(p_query, model=model.ShipcallParticipantMap, param={"id":notification.shipcall_id})
for assigned_participant in assigned_participants:
users = users_dict[assigned_participant.participant_id]
for user in users:
# send notification to user
if user.notify_email:
if user not in email_dict:
email_dict[user] = []
email_dict[user].append(notification)
if user.notify_whatsapp:
# TBD
pass
if user.notify_signal:
# TBD
pass
else:
users = users_dict[notification.participant_id]
for user in users:
# send notification to user
if user.notify_email:
if user not in email_dict:
email_dict[user] = []
email_dict[user].append(notification)
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})
@ -273,11 +293,8 @@ def setup_schedule(update_shipcalls_interval_in_minutes:int=60):
schedule.every().day.at("09:00").do(eval_next_24_hrs)
SendNotifications()
add_function_to_schedule_send_notifications(1)
# TODO: Add schedule function to evaluate all notifications in level 1 and create actions
return

View File

@ -1,5 +1,6 @@
import copy
import logging
import pydapper
import re
import numpy as np
import pandas as pd
@ -7,6 +8,7 @@ import datetime
from BreCal.database.enums import StatusFlags
from BreCal.validators.validation_rule_functions import ValidationRuleFunctions
from BreCal.schemas.model import Shipcall
from BreCal.local_db import getPoolConnection
class ValidationRules(ValidationRuleFunctions):
@ -84,9 +86,49 @@ class ValidationRules(ValidationRuleFunctions):
# build the list of evaluation times ('now', as isoformat)
#evaluation_time = self.get_notification_times(evaluation_states_new)
send_notification = False
if evaluation_states_old is not None and evaluation_states_new is not None:
if len(evaluation_states_old) == 1 and len(evaluation_states_new) == 1:
if evaluation_states_old[0] != evaluation_states_new[0]:
pooledConnection = getPoolConnection()
commands = pydapper.using(pooledConnection)
if evaluation_states_new[0] == 2:
match evaluation_states_old[0]:
case 0:
send_notification = True
case 1:
send_notification = True
if evaluation_states_new[0] == 3:
match evaluation_states_old[0]:
case 0:
send_notification = True
case 1:
send_notification = True
case 2:
send_notification = True
if send_notification:
query = "INSERT INTO notification (shipcall_id, type, level, message) VALUES (?shipcall_id?, 3, 0, ?message?)"
commands.execute(query, param={"shipcall_id" : int(shipcall_df.index[0]), "message" : violations[0]})
if evaluation_states_new[0] == 1 and evaluation_states_old[0] != 0: # this resolves the conflict
query = "SELECT * from notification where shipcall_id = ?shipcall_id? and type = 3 and level = 0"
existing_notification = commands.query(query, param={"shipcall_id" : int(shipcall_df.index[0])})
if len(existing_notification) > 0:
query = "DELETE from notification where id = ?id?"
commands.execute(query, param={"id" : existing_notification[0]["id"]})
else:
query = "INSERT INTO notification (shipcall_id, type, level) VALUES (?shipcall_id?, 4, 0)"
commands.execute(query, param={"shipcall_id" : int(shipcall_df.index[0])})
pooledConnection.close()
# build the list of 'evaluation_notifications_sent'. The value is 'False', when a notification should be created
#evaluation_notifications_sent = self.get_notification_states(evaluation_states_old, evaluation_states_new)
# TODO: detect evaluation state changes and create notifications
shipcall_df.loc[:,"evaluation"] = evaluation_states_new
shipcall_df.loc[:,"evaluation_message"] = violations
#shipcall_df.loc[:,"evaluation_time"] = evaluation_time