import random from rest_framework.generics import GenericAPIView, ListAPIView from rest_framework.response import Response from rest_framework import serializers from rest_framework.permissions import AllowAny from drf_yasg.utils import swagger_auto_schema from drf_yasg import openapi from rest_framework.authtoken.models import Token from apps.account.models import User from apps.api.models import Comment, AppVersion from apps.api.serializers import CommentSerializer, AppVersionSerializer class HomeSerializer(serializers.Serializer): token = serializers.CharField() from utils.countries import countries # test class generate token class HomeView(GenericAPIView): serializer_class = HomeSerializer @swagger_auto_schema( operation_description="Health check and token test endpoint. Optionally reads BUILD_NUMBER from headers.", manual_parameters=[ openapi.Parameter( name='BUILD_NUMBER', in_=openapi.IN_HEADER, description='Client build number', type=openapi.TYPE_STRING, required=False ) ], responses={ 200: openapi.Response( description="OK", schema=HomeSerializer() ) } ) def get(self, request): # Get build_number from headers build_number = request.META.get('HTTP_BUILD_NUMBER') # Print the build_number print(f"Build Number: {build_number}") return Response({'token': "ok", 'build_number': build_number}) class CountryView(GenericAPIView): @swagger_auto_schema( operation_description="List of countries with dialing codes and flags", responses={200: openapi.Response(description="Countries list")} ) def get(self, request): return Response(countries, status=200) class CommentListAPIView(ListAPIView): """ API view to list comments ordered by order field and creation date """ queryset = Comment.objects.all() serializer_class = CommentSerializer permission_classes = [AllowAny] ordering = ['order', '-created_at'] # Order by order field first, then by newest @swagger_auto_schema( operation_description="List comments ordered by 'order' then '-created_at'", responses={ 200: openapi.Response( description="List of comments", schema=CommentSerializer(many=True) ) } ) def get(self, request, *args, **kwargs): return super().get(request, *args, **kwargs) def get_queryset(self): queryset = super().get_queryset() return queryset.order_by('order', '-created_at') class AppVersionListAPIView(ListAPIView): queryset = AppVersion.objects.all().order_by('-created_at') serializer_class = AppVersionSerializer permission_classes = [AllowAny] @swagger_auto_schema( operation_description="List all app versions with fields.", responses={ 200: openapi.Response( description="List of app versions", schema=AppVersionSerializer(many=True) ) } ) def get(self, request, *args, **kwargs): return super().get(request, *args, **kwargs)