events: add mode_verbose to transport, return string on send error
This commit is contained in:
parent
f30bdbecd6
commit
8369fa16ae
|
@ -2,6 +2,7 @@
|
||||||
from django.http.response import Http404
|
from django.http.response import Http404
|
||||||
from guardian.shortcuts import get_objects_for_user
|
from guardian.shortcuts import get_objects_for_user
|
||||||
from rest_framework.decorators import action
|
from rest_framework.decorators import action
|
||||||
|
from rest_framework.fields import SerializerMethodField
|
||||||
from rest_framework.request import Request
|
from rest_framework.request import Request
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.serializers import ModelSerializer
|
from rest_framework.serializers import ModelSerializer
|
||||||
|
@ -11,12 +12,20 @@ from authentik.events.models import (
|
||||||
Notification,
|
Notification,
|
||||||
NotificationSeverity,
|
NotificationSeverity,
|
||||||
NotificationTransport,
|
NotificationTransport,
|
||||||
|
NotificationTransportError,
|
||||||
|
TransportMode,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class NotificationTransportSerializer(ModelSerializer):
|
class NotificationTransportSerializer(ModelSerializer):
|
||||||
"""NotificationTransport Serializer"""
|
"""NotificationTransport Serializer"""
|
||||||
|
|
||||||
|
mode_verbose = SerializerMethodField()
|
||||||
|
|
||||||
|
def get_mode_verbose(self, instance: NotificationTransport):
|
||||||
|
"""Return selected mode with a UI Label"""
|
||||||
|
return TransportMode(instance.mode).label
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|
||||||
model = NotificationTransport
|
model = NotificationTransport
|
||||||
|
@ -24,6 +33,7 @@ class NotificationTransportSerializer(ModelSerializer):
|
||||||
"pk",
|
"pk",
|
||||||
"name",
|
"name",
|
||||||
"mode",
|
"mode",
|
||||||
|
"mode_verbose",
|
||||||
"webhook_url",
|
"webhook_url",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -44,10 +54,13 @@ class NotificationTransportViewSet(ModelViewSet):
|
||||||
).filter(pk=pk)
|
).filter(pk=pk)
|
||||||
if not transports.exists():
|
if not transports.exists():
|
||||||
raise Http404
|
raise Http404
|
||||||
transport = transports.first()
|
transport: NotificationTransport = transports.first()
|
||||||
notification = Notification(
|
notification = Notification(
|
||||||
severity=NotificationSeverity.NOTICE,
|
severity=NotificationSeverity.NOTICE,
|
||||||
body=f"Test Notification from transport {transport.name}",
|
body=f"Test Notification from transport {transport.name}",
|
||||||
user=request.user,
|
user=request.user,
|
||||||
)
|
)
|
||||||
|
try:
|
||||||
return Response(transport.send(notification))
|
return Response(transport.send(notification))
|
||||||
|
except NotificationTransportError as exc:
|
||||||
|
return Response(str(exc.__cause__ or None), status=503)
|
||||||
|
|
|
@ -67,7 +67,7 @@ def event_trigger_handler(event_uuid: str, trigger_name: str):
|
||||||
|
|
||||||
@CELERY_APP.task(
|
@CELERY_APP.task(
|
||||||
bind=True,
|
bind=True,
|
||||||
autoretry_for=(NotificationTransportError),
|
autoretry_for=(NotificationTransportError,),
|
||||||
retry_backoff=True,
|
retry_backoff=True,
|
||||||
base=MonitoredTask,
|
base=MonitoredTask,
|
||||||
)
|
)
|
||||||
|
|
|
@ -7676,6 +7676,10 @@ definitions:
|
||||||
- webhook
|
- webhook
|
||||||
- webhook_slack
|
- webhook_slack
|
||||||
- email
|
- email
|
||||||
|
mode_verbose:
|
||||||
|
title: Mode verbose
|
||||||
|
type: string
|
||||||
|
readOnly: true
|
||||||
webhook_url:
|
webhook_url:
|
||||||
title: Webhook url
|
title: Webhook url
|
||||||
type: string
|
type: string
|
||||||
|
|
Reference in a new issue