refac move to test method on notification class

This commit is contained in:
Simon 2025-07-11 17:27:52 +07:00
parent e717eead8d
commit fae3a8b2cd
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
2 changed files with 37 additions and 45 deletions

View File

@ -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]:

View File

@ -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
)