diff --git a/src/notification.py b/src/notification.py index e3381ff..06a2035 100644 --- a/src/notification.py +++ b/src/notification.py @@ -3,7 +3,7 @@ import urllib from sqlalchemy.orm import Session -from tables import engine, TableNotification +from tables import TableNotification import log logger = log.get_logger(__name__) @@ -46,10 +46,8 @@ class Notification: response = conn.getresponse() logger.debug(f"Pushover response code: {response.getcode()}") if response.getcode() == 200: - n = TableNotification(type=type_of_error, message=f"{message}", medium='pushover', success=True) + success = True else: logger.error(f"Pushover notification failed. Code {response.getcode()}: {response.read().decode()}") - n = TableNotification(type=type_of_error, message=f"{message}", medium='pushover', success=False) - with Session(engine) as session: - session.add(n) - session.commit() + success = False + TableNotification.insert(type_of_error, f"{message}", 'pushover', success) diff --git a/src/tables.py b/src/tables.py index b86a382..9a2e1ed 100644 --- a/src/tables.py +++ b/src/tables.py @@ -22,6 +22,13 @@ class TableNotification(Base): __mapper_args__ = {"eager_defaults": True} + @classmethod + def insert(cls, type_of_error, message, medium, success): + with Session(engine) as session: + n = TableNotification(type=type_of_error, message=message, medium=medium, success=success) + session.add(n) + session.commit() + @classmethod def get_won_notifications_today(cls): with Session(engine) as session: