added validation, tawhiri request creation
This commit is contained in:
parent
456551cd4e
commit
2aef4d4756
12 changed files with 184 additions and 18 deletions
43
api/views.py
43
api/views.py
|
|
@ -3,9 +3,14 @@ from rest_framework.response import Response
|
|||
from rest_framework.views import APIView
|
||||
from django.utils import timezone
|
||||
from .models import Prediction, User, UserPrediction
|
||||
from .serializers import PredictionSerializer
|
||||
from .serializers import PredictionSerializer, PredictionRequestSerializer
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
import requests
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
from django.utils.decorators import method_decorator
|
||||
from rest_framework.permissions import AllowAny
|
||||
from .services.tawhiri import TawhiriClient
|
||||
|
||||
|
||||
def get_prediction_from_tawhiri(params):
|
||||
base_url = "https://fly.stratonautica.ru/api/v2"
|
||||
|
|
@ -15,25 +20,31 @@ def get_prediction_from_tawhiri(params):
|
|||
return response.json() # получаем результат предсказания
|
||||
else:
|
||||
raise Exception(f"Tawhiri error: {response.status_code} {response.text}")
|
||||
|
||||
|
||||
|
||||
class PredictionCreateView(APIView):
|
||||
def post(self, request):
|
||||
user_id = request.data.get('user_id')
|
||||
user = User.objects.get(id=user_id)
|
||||
permission_classes = [AllowAny]
|
||||
|
||||
def post(self, request):
|
||||
serializer = PredictionRequestSerializer(data=request.data)
|
||||
if not serializer.is_valid():
|
||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
validated_data = serializer.validated_data
|
||||
|
||||
# Передаём остальные параметры (кроме user_id) в Tawhiri
|
||||
tawhiri_params = {k: v for k, v in request.data.items() if k != 'user_id'}
|
||||
|
||||
try:
|
||||
prediction_result = get_prediction_from_tawhiri(tawhiri_params)
|
||||
except Exception as e:
|
||||
return Response({"error": str(e)}, status=500)
|
||||
prediction_result = TawhiriClient.get_prediction(validated_data)
|
||||
except requests.RequestException as e:
|
||||
print("❌ Tawhiri error:", str(e), e.response.text if e.response else "no response")
|
||||
return Response({"error": f"Tawhiri error: {str(e)}"}, status=status.HTTP_502_BAD_GATEWAY)
|
||||
|
||||
prediction = Prediction.objects.create(result=prediction_result)
|
||||
UserPrediction.objects.create(user=user, prediction=prediction)
|
||||
UserPrediction.objects.create(user=request.user, prediction=prediction, created_at=timezone.now())
|
||||
|
||||
return Response(PredictionSerializer(prediction).data)
|
||||
return Response({
|
||||
"id": prediction.id,
|
||||
"created_at": prediction.created_at,
|
||||
"result": prediction_result
|
||||
}, status=status.HTTP_201_CREATED)
|
||||
|
||||
class PredictionListView(APIView):
|
||||
def get(self, request):
|
||||
|
|
@ -59,5 +70,5 @@ class PredictionDeleteView(APIView):
|
|||
except Prediction.DoesNotExist:
|
||||
return Response({"error": "Not found"}, status=404)
|
||||
|
||||
class PredictionCreateView(APIView):
|
||||
permission_classes = [IsAuthenticated]
|
||||
#class PredictionCreateView(APIView):
|
||||
#permission_classes = [IsAuthenticated]
|
||||
Loading…
Add table
Add a link
Reference in a new issue