events: fix error in API when specifying max_n

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-05-06 13:15:54 +02:00
parent 303b847cdc
commit 73b87a5e3d
2 changed files with 5 additions and 17 deletions

View File

@ -11,7 +11,7 @@ from rest_framework.response import Response
from rest_framework.serializers import ModelSerializer, Serializer from rest_framework.serializers import ModelSerializer, Serializer
from rest_framework.viewsets import ReadOnlyModelViewSet from rest_framework.viewsets import ReadOnlyModelViewSet
from authentik.core.api.utils import TypeCreateSerializer from authentik.core.api.utils import PassiveSerializer, TypeCreateSerializer
from authentik.events.models import Event, EventAction from authentik.events.models import Event, EventAction
@ -38,31 +38,19 @@ class EventSerializer(ModelSerializer):
] ]
class EventTopPerUserParams(Serializer): class EventTopPerUserParams(PassiveSerializer):
"""Query params for top_per_user""" """Query params for top_per_user"""
top_n = IntegerField(default=15) top_n = IntegerField(default=15)
def create(self, request: Request) -> Response:
raise NotImplementedError
def update(self, request: Request) -> Response: class EventTopPerUserSerializer(PassiveSerializer):
raise NotImplementedError
class EventTopPerUserSerializer(Serializer):
"""Response object of Event's top_per_user""" """Response object of Event's top_per_user"""
application = DictField() application = DictField()
counted_events = IntegerField() counted_events = IntegerField()
unique_users = IntegerField() unique_users = IntegerField()
def create(self, request: Request) -> Response:
raise NotImplementedError
def update(self, request: Request) -> Response:
raise NotImplementedError
class EventsFilter(django_filters.FilterSet): class EventsFilter(django_filters.FilterSet):
"""Filter for events""" """Filter for events"""
@ -132,7 +120,7 @@ class EventViewSet(ReadOnlyModelViewSet):
def top_per_user(self, request: Request): def top_per_user(self, request: Request):
"""Get the top_n events grouped by user count""" """Get the top_n events grouped by user count"""
filtered_action = request.query_params.get("action", EventAction.LOGIN) filtered_action = request.query_params.get("action", EventAction.LOGIN)
top_n = request.query_params.get("top_n", 15) top_n = int(request.query_params.get("top_n", "15"))
return Response( return Response(
get_objects_for_user(request.user, "authentik_events.view_event") get_objects_for_user(request.user, "authentik_events.view_event")
.filter(action=filtered_action) .filter(action=filtered_action)

View File

@ -46,7 +46,7 @@ def pre_delete_cleanup(sender, instance: Outpost, **_):
# Service connection here # Service connection here
try: try:
outpost_pre_delete.delay(instance.pk.hex).get() outpost_pre_delete.delay(instance.pk.hex).get()
except RuntimeError: except RuntimeError: # pragma: no cover
# In e2e/integration tests, this might run inside a thread/process and # In e2e/integration tests, this might run inside a thread/process and
# trigger the celery `Never call result.get() within a task` detection # trigger the celery `Never call result.get() within a task` detection
if settings.TEST: if settings.TEST: