dr.botzo/pi/views.py

23 lines
797 B
Python

"""Provide pi simulation results."""
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(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, 201)