root: add more API methods
This commit is contained in:
		| @ -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, | ||||
|             ) | ||||
|         ) | ||||
|  | ||||
| @ -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): | ||||
|  | ||||
| @ -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): | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Jens Langhammer
					Jens Langhammer