authentik.events.api.notifications

Notification API Views

 1"""Notification API Views"""
 2
 3from drf_spectacular.types import OpenApiTypes
 4from drf_spectacular.utils import OpenApiResponse, extend_schema
 5from rest_framework import mixins
 6from rest_framework.decorators import action
 7from rest_framework.fields import ReadOnlyField
 8from rest_framework.permissions import IsAuthenticated
 9from rest_framework.request import Request
10from rest_framework.response import Response
11from rest_framework.viewsets import GenericViewSet
12
13from authentik.core.api.used_by import UsedByMixin
14from authentik.core.api.utils import ModelSerializer
15from authentik.events.api.events import EventSerializer
16from authentik.events.models import Notification
17
18
19class NotificationSerializer(ModelSerializer):
20    """Notification Serializer"""
21
22    body = ReadOnlyField()
23    severity = ReadOnlyField()
24    event = EventSerializer(required=False)
25
26    class Meta:
27        model = Notification
28        fields = [
29            "pk",
30            "severity",
31            "body",
32            "hyperlink",
33            "hyperlink_label",
34            "created",
35            "event",
36            "seen",
37        ]
38
39
40class NotificationViewSet(
41    mixins.RetrieveModelMixin,
42    mixins.UpdateModelMixin,
43    mixins.DestroyModelMixin,
44    UsedByMixin,
45    mixins.ListModelMixin,
46    GenericViewSet,
47):
48    """Notification Viewset"""
49
50    queryset = Notification.objects.all()
51    serializer_class = NotificationSerializer
52    filterset_fields = [
53        "severity",
54        "body",
55        "created",
56        "event",
57        "seen",
58        "user",
59    ]
60    owner_field = "user"
61
62    @extend_schema(
63        request=OpenApiTypes.NONE,
64        responses={
65            204: OpenApiResponse(description="Marked tasks as read successfully."),
66        },
67    )
68    @action(detail=False, methods=["post"], permission_classes=[IsAuthenticated])
69    def mark_all_seen(self, request: Request) -> Response:
70        """Mark all the user's notifications as seen"""
71        Notification.objects.filter(user=request.user, seen=False).update(seen=True)
72        return Response({}, status=204)
class NotificationSerializer(authentik.core.api.utils.ModelSerializer):
20class NotificationSerializer(ModelSerializer):
21    """Notification Serializer"""
22
23    body = ReadOnlyField()
24    severity = ReadOnlyField()
25    event = EventSerializer(required=False)
26
27    class Meta:
28        model = Notification
29        fields = [
30            "pk",
31            "severity",
32            "body",
33            "hyperlink",
34            "hyperlink_label",
35            "created",
36            "event",
37            "seen",
38        ]

Notification Serializer

body
severity
event
class NotificationSerializer.Meta:
27    class Meta:
28        model = Notification
29        fields = [
30            "pk",
31            "severity",
32            "body",
33            "hyperlink",
34            "hyperlink_label",
35            "created",
36            "event",
37            "seen",
38        ]
fields = ['pk', 'severity', 'body', 'hyperlink', 'hyperlink_label', 'created', 'event', 'seen']
class NotificationViewSet(rest_framework.mixins.RetrieveModelMixin, rest_framework.mixins.UpdateModelMixin, rest_framework.mixins.DestroyModelMixin, authentik.core.api.used_by.UsedByMixin, rest_framework.mixins.ListModelMixin, rest_framework.viewsets.GenericViewSet):
41class NotificationViewSet(
42    mixins.RetrieveModelMixin,
43    mixins.UpdateModelMixin,
44    mixins.DestroyModelMixin,
45    UsedByMixin,
46    mixins.ListModelMixin,
47    GenericViewSet,
48):
49    """Notification Viewset"""
50
51    queryset = Notification.objects.all()
52    serializer_class = NotificationSerializer
53    filterset_fields = [
54        "severity",
55        "body",
56        "created",
57        "event",
58        "seen",
59        "user",
60    ]
61    owner_field = "user"
62
63    @extend_schema(
64        request=OpenApiTypes.NONE,
65        responses={
66            204: OpenApiResponse(description="Marked tasks as read successfully."),
67        },
68    )
69    @action(detail=False, methods=["post"], permission_classes=[IsAuthenticated])
70    def mark_all_seen(self, request: Request) -> Response:
71        """Mark all the user's notifications as seen"""
72        Notification.objects.filter(user=request.user, seen=False).update(seen=True)
73        return Response({}, status=204)

Notification Viewset

queryset = <QuerySet []>
serializer_class = <class 'NotificationSerializer'>
filterset_fields = ['severity', 'body', 'created', 'event', 'seen', 'user']
owner_field = 'user'
@extend_schema(request=OpenApiTypes.NONE, responses={204: OpenApiResponse(description='Marked tasks as read successfully.')})
@action(detail=False, methods=['post'], permission_classes=[IsAuthenticated])
def mark_all_seen( self, request: rest_framework.request.Request) -> rest_framework.response.Response:
63    @extend_schema(
64        request=OpenApiTypes.NONE,
65        responses={
66            204: OpenApiResponse(description="Marked tasks as read successfully."),
67        },
68    )
69    @action(detail=False, methods=["post"], permission_classes=[IsAuthenticated])
70    def mark_all_seen(self, request: Request) -> Response:
71        """Mark all the user's notifications as seen"""
72        Notification.objects.filter(user=request.user, seen=False).update(seen=True)
73        return Response({}, status=204)

Mark all the user's notifications as seen

name = None
description = None
suffix = None
detail = None
basename = None