From da815a1fc33f5effb172c8ad16aab09e1bbb1a3a Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Sat, 24 Oct 2020 23:58:45 -0500 Subject: [PATCH] provide DRF action to run a pi simulation --- pi/views.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pi/views.py b/pi/views.py index 185bfac..29d932b 100644 --- a/pi/views.py +++ b/pi/views.py @@ -1,12 +1,22 @@ """Provide pi simulation results.""" -from rest_framework import viewsets +from rest_framework.decorators import action +from rest_framework.permissions import IsAuthenticated +from rest_framework.response import Response +from rest_framework.viewsets import ReadOnlyModelViewSet from pi.models import PiLog from pi.serializers import PiLogSerializer -class PiLogViewSet(viewsets.ReadOnlyModelViewSet): +class PiLogViewSet(ReadOnlyModelViewSet): """Provide list and detail actions for pi simulation log entries.""" queryset = PiLog.objects.all() serializer_class = PiLogSerializer + permission_classes = [IsAuthenticated] + + @action(detail=False, methods=['post']) + def simulate(self, request): + """Run one simulation of the pi estimator.""" + simulation, _, _ = PiLog.objects.simulate() + return Response(self.get_serializer(simulation).data)