diff --git a/backend/task/src/notify.py b/backend/task/src/notify.py index 3a9b1f23..7a493548 100644 --- a/backend/task/src/notify.py +++ b/backend/task/src/notify.py @@ -32,6 +32,38 @@ class Notifications: apobj.notify(body=body, title=title) + def test(self, url) -> tuple[bool, str]: + """send test notification""" + try: + apobj = apprise.Apprise() + + if not apobj.add(url): + success = False + message = f"Invalid notification URL format: {url}" + return success, message + + title = f"[TA] {self.task_name} process ended with SUCCESS" + body = "This is a test notification. Task completed successfully." + + result = apobj.notify(body=body, title=title) + + if result: + success = True + message = "Test notification sent successfully" + return success, message + + success = False + message = ( + "Notification failed. " + "Please check container logs for more information." + ) + return success, message + + except Exception as err: # pylint: disable=broad-exception-caught + success = False + message = f"Notification error: {str(err)}" + return success, message + def _build_message( self, task_id: str, task_title: str ) -> tuple[str, str | None]: diff --git a/backend/task/views.py b/backend/task/views.py index 8c3a9c1e..a0bdee77 100644 --- a/backend/task/views.py +++ b/backend/task/views.py @@ -362,8 +362,6 @@ class NotificationTestView(ApiBaseView): ) def post(self, request): """test notification""" - import apprise - data_serializer = TaskNotificationTestSerializer(data=request.data) data_serializer.is_valid(raise_exception=True) validated_data = data_serializer.validated_data @@ -371,47 +369,9 @@ class NotificationTestView(ApiBaseView): url = validated_data["url"] task_name = validated_data.get("task_name", "manual_test") - try: - apobj = apprise.Apprise() + success, message = Notifications(task_name).test(url) - if not apobj.add(url): - return Response( - { - "success": False, - "message": f"Invalid notification URL format: {url}", - }, - status=400, - ) - - title = f"[TA] {task_name} process ended with SUCCESS" - body = "This is a test notification. Task completed successfully." - - result = apobj.notify(body=body, title=title) - - if result: - return Response( - { - "success": True, - "message": "Test notification sent successfully", - } - ) - - return Response( - { - "success": False, - "message": ( - "Notification failed. " - "Please check container logs for more information." - ), - }, - status=400, - ) - - except Exception as err: - return Response( - { - "success": False, - "message": f"Notification error: {str(err)}", - }, - status=400, - ) + status = 200 if success else 400 + return Response( + {"success": success, "message": message}, status=status + )