Views various fixes
This commit is contained in:
parent
5af3b95c8d
commit
51415765da
4 changed files with 41 additions and 12 deletions
|
|
@ -80,6 +80,24 @@ class PredictionRequestSerializer(serializers.Serializer):
|
|||
|
||||
return data
|
||||
|
||||
def create(self, validated_data):
|
||||
if 'ascent_curve' in validated_data:
|
||||
validated_data['ascent_curve'] = base64_to_curve(validated_data['ascent_curve'])
|
||||
if 'descent_curve' in validated_data:
|
||||
validated_data['descent_curve'] = base64_to_curve(validated_data['descent_curve'])
|
||||
|
||||
prediction = Prediction(
|
||||
user=validated_data.get('user'),
|
||||
request=validated_data.get('request', {}),
|
||||
result=validated_data.get('result', {}),
|
||||
start_point=validated_data.get('start_point'),
|
||||
template=validated_data.get('template'),
|
||||
rate_profile=validated_data.get('rate_profile')
|
||||
)
|
||||
prediction.save()
|
||||
|
||||
return prediction
|
||||
|
||||
|
||||
|
||||
class PredictionListSerializer(serializers.ModelSerializer):
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ from rest_framework.authtoken.views import obtain_auth_token
|
|||
from .views import (
|
||||
PredictionViewSet,
|
||||
SavedPointViewset,
|
||||
PreditctionTemplateViewset,
|
||||
TelemetryListCreateView,
|
||||
get_csrf,
|
||||
login_view,
|
||||
|
|
@ -17,7 +18,7 @@ from .views import (
|
|||
router = DefaultRouter()
|
||||
router.register(r'predictions', PredictionViewSet, basename='predictions')
|
||||
router.register(r'saved-points', SavedPointViewset, basename='saved-points')
|
||||
|
||||
router.register(r'saved-templates', PreditctionTemplateViewset, basename='saved-templates')
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
|
|
@ -28,8 +29,6 @@ urlpatterns = [
|
|||
path("session/", SessionView.as_view(), name='api-session'),
|
||||
path("whoami/", WhoAmIView.as_view(), name='api-whoami'),
|
||||
path("<uuid:pk>/telemetry/", TelemetryListCreateView.as_view(), name="create_telemetry"),
|
||||
path('saved-points/', SavedPointViewset.as_view({'get': 'list', 'post': 'create'}), name='saved_points'),
|
||||
path('saved-points/<int:pk>/', SavedPointViewset.as_view({'get': 'retrieve', 'put': 'update', 'delete': 'destroy'}), name='saved_point_detail'),
|
||||
path('csrf/', get_csrf, name='api-csrf'),
|
||||
path('login/', login_view, name='api-login'),
|
||||
path('logout/', logout_view, name='api-logout'),
|
||||
|
|
|
|||
28
api/views.py
28
api/views.py
|
|
@ -6,7 +6,7 @@ from rest_framework.response import Response
|
|||
from rest_framework.views import APIView
|
||||
from rest_framework.viewsets import ModelViewSet, ViewSet
|
||||
from rest_framework.permissions import IsAuthenticated, AllowAny
|
||||
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
|
||||
from rest_framework.authentication import SessionAuthentication, BasicAuthentication, TokenAuthentication
|
||||
from rest_framework.decorators import api_view, permission_classes, authentication_classes, action
|
||||
from django.utils import timezone
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
|
|
@ -54,8 +54,13 @@ class PredictionViewSet(ViewSet):
|
|||
except requests.RequestException as e:
|
||||
return Response({"error": f"Tawhiri error: {str(e)}"}, status=status.HTTP_502_BAD_GATEWAY)
|
||||
|
||||
prediction = Prediction.objects.create(
|
||||
result=prediction_result, user=request.user, request=request.data)
|
||||
# prediction = Prediction.objects.create(
|
||||
# result=prediction_result, user=request.user, request=request.data, validated_data=validated_data)
|
||||
prediction = serializer.save(
|
||||
user=request.user,
|
||||
result=prediction_result,
|
||||
request=request.data
|
||||
)
|
||||
|
||||
return Response({
|
||||
"id": prediction.id,
|
||||
|
|
@ -72,7 +77,6 @@ class PredictionViewSet(ViewSet):
|
|||
|
||||
filters = {
|
||||
'user': user,
|
||||
'deleted_at__isnull': True
|
||||
}
|
||||
|
||||
if created_from:
|
||||
|
|
@ -148,7 +152,6 @@ class TelemetryListCreateView(generics.ListCreateAPIView):
|
|||
|
||||
|
||||
class SessionView(APIView):
|
||||
authentication_classes = [SessionAuthentication, BasicAuthentication]
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
@staticmethod
|
||||
|
|
@ -157,7 +160,6 @@ class SessionView(APIView):
|
|||
|
||||
|
||||
class WhoAmIView(APIView):
|
||||
authentication_classes = [SessionAuthentication, BasicAuthentication]
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
@staticmethod
|
||||
|
|
@ -177,7 +179,7 @@ def get_csrf(request):
|
|||
@extend_schema(methods=["POST"], description="Login user")
|
||||
@csrf_exempt
|
||||
@api_view(["POST"])
|
||||
@authentication_classes([])
|
||||
@authentication_classes([BasicAuthentication])
|
||||
@permission_classes([AllowAny])
|
||||
def login_view(request):
|
||||
data = json.loads(request.body)
|
||||
|
|
@ -207,7 +209,6 @@ def logout_view(request):
|
|||
|
||||
|
||||
class SavedPointViewset(ModelViewSet):
|
||||
authentication_classes = [SessionAuthentication, BasicAuthentication]
|
||||
permission_classes = [IsOwner]
|
||||
serializer_class = SavedPointSerializer
|
||||
pagination_class = None
|
||||
|
|
@ -218,6 +219,17 @@ class SavedPointViewset(ModelViewSet):
|
|||
def perform_create(self, serializer):
|
||||
serializer.save(user=self.request.user)
|
||||
|
||||
class PreditctionTemplateViewset(ModelViewSet):
|
||||
permission_classes = [IsOwner]
|
||||
serializer_class = PreditctionTemplateSerializer
|
||||
pagination_class = None
|
||||
|
||||
def get_queryset(self):
|
||||
return PreditctionTemplate.objects.filter(user=self.request.user)
|
||||
|
||||
def perform_create(self, serializer):
|
||||
serializer.save(user=self.request.user)
|
||||
|
||||
# class PredictionCreateView(APIView):
|
||||
# permission_classes = [IsAuthenticated]
|
||||
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ SECRET_KEY = os.getenv(
|
|||
'SECRET_KEY', 'django-insecure-np(nxnh6mw)v4pa2n2z3pl_5&!2z$jshhak9r3v=y1u9rd*sl!')
|
||||
|
||||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = os.getenv('DEBUG', 'False') == 'True'
|
||||
DEBUG = os.getenv('DEBUG', 'True') == 'True'
|
||||
|
||||
ALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS', 'localhost').split(',')
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue