You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
4.7 KiB
133 lines
4.7 KiB
from rest_framework import status
|
|
from rest_framework.views import APIView
|
|
from rest_framework.response import Response
|
|
from rest_framework.permissions import IsAuthenticated
|
|
from rest_framework.authentication import TokenAuthentication
|
|
from apps.bookmark.models import Rate
|
|
from apps.bookmark.serializers import RateSerializer, RateStatusSerializer, AverageRateSerializer
|
|
from drf_yasg import openapi
|
|
from drf_yasg.utils import swagger_auto_schema
|
|
|
|
class AddRateView(APIView):
|
|
"""
|
|
API view for adding or updating a rate.
|
|
"""
|
|
permission_classes = [IsAuthenticated]
|
|
authentication_classes = [TokenAuthentication]
|
|
|
|
@swagger_auto_schema(
|
|
operation_description="Add or update a rate for a content.",
|
|
tags=["Dobodbi - Bookmarks"],
|
|
request_body=RateSerializer,
|
|
responses={
|
|
201: "Rate created successfully.",
|
|
400: "Invalid input."
|
|
}
|
|
)
|
|
def post(self, request):
|
|
"""
|
|
Add or update a rate for a content.
|
|
"""
|
|
serializer = RateSerializer(data=request.data, context={'request': request})
|
|
if serializer.is_valid():
|
|
serializer.save()
|
|
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
class RemoveRateView(APIView):
|
|
"""
|
|
API view for removing a rate.
|
|
"""
|
|
permission_classes = [IsAuthenticated]
|
|
authentication_classes = [TokenAuthentication]
|
|
|
|
@swagger_auto_schema(
|
|
operation_description="Remove a rate by setting its status to False.",
|
|
tags=["Dobodbi - Bookmarks"],
|
|
request_body=RateStatusSerializer,
|
|
responses={
|
|
200: "Rate removed successfully.",
|
|
404: "Rate not found.",
|
|
400: "Invalid input."
|
|
}
|
|
)
|
|
def post(self, request):
|
|
"""
|
|
Remove a rate by setting its status to False.
|
|
"""
|
|
serializer = RateStatusSerializer(data=request.data)
|
|
if serializer.is_valid():
|
|
service = serializer.validated_data['service']
|
|
content_id = serializer.validated_data['content_id']
|
|
|
|
try:
|
|
rate = Rate.objects.get(
|
|
user=request.user,
|
|
service=service,
|
|
content_id=content_id
|
|
)
|
|
rate.status = False
|
|
rate.save()
|
|
return Response({'message': 'Rate removed successfully'}, status=status.HTTP_200_OK)
|
|
except Rate.DoesNotExist:
|
|
return Response({'message': 'Rate not found'}, status=status.HTTP_404_NOT_FOUND)
|
|
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
class RateStatusView(APIView):
|
|
"""
|
|
API view for checking if a user has rated a content and getting the rate value.
|
|
"""
|
|
permission_classes = [IsAuthenticated]
|
|
authentication_classes = [TokenAuthentication]
|
|
|
|
@swagger_auto_schema(
|
|
operation_description="Check if a user has rated a content and get the rate value.",
|
|
tags=["Dobodbi - Bookmarks"],
|
|
request_body=RateStatusSerializer,
|
|
responses={
|
|
200: "Rate status information.",
|
|
400: "Invalid input."
|
|
}
|
|
)
|
|
def post(self, request):
|
|
"""
|
|
Check if a user has rated a content and get the rate value.
|
|
"""
|
|
serializer = RateStatusSerializer(data=request.data)
|
|
if serializer.is_valid():
|
|
service = serializer.validated_data['service']
|
|
content_id = serializer.validated_data['content_id']
|
|
|
|
rate_info = Rate.get_user_rate(request.user, service, content_id)
|
|
return Response(rate_info, status=status.HTTP_200_OK)
|
|
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
class AverageRateView(APIView):
|
|
"""
|
|
API view for getting the average rate of a content.
|
|
"""
|
|
|
|
@swagger_auto_schema(
|
|
operation_description="Get the average rate of a content.",
|
|
tags=["Dobodbi - Bookmarks"],
|
|
request_body=AverageRateSerializer,
|
|
responses={
|
|
200: "Average rate information.",
|
|
400: "Invalid input."
|
|
}
|
|
)
|
|
def post(self, request):
|
|
"""
|
|
Get the average rate of a content.
|
|
"""
|
|
serializer = AverageRateSerializer(data=request.data)
|
|
if serializer.is_valid():
|
|
service = serializer.validated_data['service']
|
|
content_id = serializer.validated_data['content_id']
|
|
|
|
avg_rate = Rate.get_average_rate(service, content_id)
|
|
return Response({'average_rate': avg_rate}, status=status.HTTP_200_OK)
|
|
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|