Files
goodtimes/chat/api/views.py
rizwanisready db213d3228 wrong commit
2024-02-29 13:25:50 +05:30

109 lines
3.6 KiB
Python

from django.shortcuts import get_object_or_404
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from accounts.models import IAmPrincipal
from goodtimes import services, constants
from rest_framework import generics, pagination
from chat import models
# from stock.models import Team
from django.conf import settings
from . import serializers
from goodtimes.utils import ApiResponse
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework_simplejwt.authentication import JWTAuthentication
class EnterRoomApi(APIView):
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]
def post(self, request, room_name):
principal_id = request.user.id
return Response({"room_name": room_name})
class ChatGroupAPIView(APIView):
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]
def get(self, request, game_id):
group_name = f"game_{game_id}"
chat_group, created = models.ChatGroup.objects.get_or_create(name=group_name)
serializer = serializers.ChatGroupSerializer(chat_group)
response_data = {
"status": status.HTTP_200_OK,
"message": constants.SUCCESS,
"data": serializer.data,
}
return ApiResponse.success(**response_data)
class CustomPagination(pagination.PageNumberPagination):
page_size = 30
page_size_query_param = "page_size"
max_page_size = 100
class ChatMessageAPIView(generics.ListAPIView):
serializer_class = serializers.ChatMessageSerializer
pagination_class = CustomPagination
def get_queryset(self):
game_name = f"game_{self.kwargs['game_name']}"
return models.ChatMessage.objects.filter(group__name=game_name).order_by(
"-timestamp"
)
def get(self, request, *args, **kwargs):
queryset = self.get_queryset()
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
response_data = {
"status": status.HTTP_200_OK,
"message": constants.SUCCESS,
"data": serializer.data,
}
return ApiResponse.success(**response_data)
response_empty = {
"status": status.HTTP_200_OK,
"message": constants.SUCCESS,
"data": serializer.data,
}
return ApiResponse.success(**response_empty)
# class TeamCheckAPIView(generics.RetrieveAPIView):
# serializer_class = serializers.TeamCheckSerializer
# def get(self, request, *args, **kwargs):
# user = request.user
# game_id = kwargs.get("game_id")
# if not user or not game_id:
# response_error = {
# "status": status.HTTP_404_NOT_FOUND,
# "message": constants.FAILURE,
# "errors": "User and Game ID are required parameters.",
# }
# return ApiResponse.error(**response_error)
# teams = Team.objects.filter(game_id=game_id, principal=request.user)
# has_team = teams.exists()
# data = {"has_team": has_team}
# serializer = serializers.TeamCheckSerializer(data=data)
# serializer.is_valid(raise_exception=True) # Ensure serializer is valid
# response_data = {
# "status": status.HTTP_200_OK,
# "message": constants.SUCCESS,
# "data": serializer.validated_data,
# }
# return ApiResponse.success(**response_data)