events: add mode_verbose to transport, return string on send error

This commit is contained in:
Jens Langhammer 2021-01-12 21:48:39 +01:00
parent f30bdbecd6
commit 8369fa16ae
3 changed files with 20 additions and 3 deletions

View file

@ -2,6 +2,7 @@
from django.http.response import Http404
from guardian.shortcuts import get_objects_for_user
from rest_framework.decorators import action
from rest_framework.fields import SerializerMethodField
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.serializers import ModelSerializer
@ -11,12 +12,20 @@ from authentik.events.models import (
Notification,
NotificationSeverity,
NotificationTransport,
NotificationTransportError,
TransportMode,
)
class NotificationTransportSerializer(ModelSerializer):
"""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:
model = NotificationTransport
@ -24,6 +33,7 @@ class NotificationTransportSerializer(ModelSerializer):
"pk",
"name",
"mode",
"mode_verbose",
"webhook_url",
]
@ -44,10 +54,13 @@ class NotificationTransportViewSet(ModelViewSet):
).filter(pk=pk)
if not transports.exists():
raise Http404
transport = transports.first()
transport: NotificationTransport = transports.first()
notification = Notification(
severity=NotificationSeverity.NOTICE,
body=f"Test Notification from transport {transport.name}",
user=request.user,
)
try:
return Response(transport.send(notification))
except NotificationTransportError as exc:
return Response(str(exc.__cause__ or None), status=503)

View file

@ -67,7 +67,7 @@ def event_trigger_handler(event_uuid: str, trigger_name: str):
@CELERY_APP.task(
bind=True,
autoretry_for=(NotificationTransportError),
autoretry_for=(NotificationTransportError,),
retry_backoff=True,
base=MonitoredTask,
)

View file

@ -7676,6 +7676,10 @@ definitions:
- webhook
- webhook_slack
- email
mode_verbose:
title: Mode verbose
type: string
readOnly: true
webhook_url:
title: Webhook url
type: string