root: add more API methods

This commit is contained in:
Jens Langhammer 2020-11-27 18:37:45 +01:00
parent 28980d932a
commit 47fe867803
3 changed files with 35 additions and 1 deletions

View File

@ -1,11 +1,14 @@
"""Application API Views"""
from django.db.models import QuerySet
from rest_framework.decorators import action
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.serializers import ModelSerializer
from rest_framework.viewsets import ModelViewSet
from rest_framework_guardian.filters import ObjectPermissionsFilter
from passbook.admin.api.overview_metrics import get_events_per_1h
from passbook.audit.models import EventAction
from passbook.core.models import Application
from passbook.policies.engine import PolicyEngine
@ -59,3 +62,14 @@ class ApplicationViewSet(ModelViewSet):
allowed_applications.append(application)
serializer = self.get_serializer(allowed_applications, many=True)
return self.get_paginated_response(serializer.data)
@action(detail=True)
def metrics(self, request: Request, slug: str):
# TODO: Check app read and audit read perms
app = Application.objects.get(slug=slug)
return Response(
get_events_per_1h(
action=EventAction.AUTHORIZE_APPLICATION,
context__authorized_application__pk=app.pk.hex,
)
)

View File

@ -1,17 +1,35 @@
"""Flow API Views"""
from django.core.cache import cache
from rest_framework.serializers import ModelSerializer, SerializerMethodField
from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet
from passbook.flows.models import Flow, FlowStageBinding, Stage
from passbook.flows.planner import cache_key
class FlowSerializer(ModelSerializer):
"""Flow Serializer"""
cache_count = SerializerMethodField()
# pylint: disable=invalid-name
def get_cache_count(self, flow: Flow):
"""Get count of cached flows"""
return len(cache.keys(f"{cache_key(flow)}*"))
class Meta:
model = Flow
fields = ["pk", "name", "slug", "title", "designation", "stages", "policies"]
fields = [
"pk",
"name",
"slug",
"title",
"designation",
"stages",
"policies",
"cache_count",
]
class FlowViewSet(ModelViewSet):

View File

@ -1,4 +1,5 @@
"""policy API Views"""
import django_filters.rest_framework
from django.core.exceptions import ObjectDoesNotExist
from rest_framework.serializers import (
ModelSerializer,
@ -62,6 +63,7 @@ class PolicyBindingViewSet(ModelViewSet):
queryset = PolicyBinding.objects.all()
serializer_class = PolicyBindingSerializer
filterset_fields = ["policy", "target", "enabled", "order", "timeout"]
class PolicySerializer(ModelSerializer):