From 34a5a5ebbc1b20d39e0b45bc13cf11545814c5aa Mon Sep 17 00:00:00 2001 From: Boluwatife Popoola Date: Thu, 3 Oct 2024 05:51:46 +0100 Subject: [PATCH] endpoint for user upvoted lists, add pairs to vote --- grantpicks/api.py | 5 ++--- grantpicks/serializers.py | 27 ++++++++++++++------------- indexer_app/utils.py | 4 +++- lists/api.py | 17 +++++++++++++++++ 4 files changed, 36 insertions(+), 17 deletions(-) diff --git a/grantpicks/api.py b/grantpicks/api.py index 2be0ec5..d4c301d 100644 --- a/grantpicks/api.py +++ b/grantpicks/api.py @@ -39,11 +39,10 @@ PaginatedProjectsResponseSerializer, PaginatedRoundApplicationsResponseSerializer, PaginatedRoundsResponseSerializer, - PaginatedVotePairResponseSerializer, + PaginatedVotesResponseSerializer, ProjectSerializer, RoundApplicationSerializer, RoundSerializer, - VotePairSerializer, VoteSerializer, ) @@ -175,7 +174,7 @@ class ProjectRoundVotesAPI(APIView, CustomSizePageNumberPagination): ], responses={ 200: OpenApiResponse( - response=PaginatedVotePairResponseSerializer, # Update to use the appropriate serializer + response=PaginatedVotesResponseSerializer, # Update to use the appropriate serializer description="Returns votes for a project in the round", examples=[ OpenApiExample( diff --git a/grantpicks/serializers.py b/grantpicks/serializers.py index 9569288..5c3c90e 100644 --- a/grantpicks/serializers.py +++ b/grantpicks/serializers.py @@ -266,9 +266,22 @@ class PaginatedRoundApplicationsResponseSerializer(serializers.Serializer): +class VotePairSerializer(serializers.ModelSerializer): + project = ProjectSerializer() + + class Meta: + model = VotePair + fields = [ + 'pair_id', + 'project', + ] + + + class VoteSerializer(serializers.ModelSerializer): round = serializers.PrimaryKeyRelatedField(queryset=Round.objects.all()) voter = AccountSerializer() + pairs = VotePairSerializer(many=True) class Meta: model = Vote @@ -276,6 +289,7 @@ class Meta: 'id', 'round', 'voter', + 'pairs', 'tx_hash', 'voted_at', ] @@ -289,19 +303,6 @@ class PaginatedVotesResponseSerializer(serializers.Serializer): results = VoteSerializer(many=True) - -class VotePairSerializer(serializers.ModelSerializer): - vote = VoteSerializer() - project = ProjectSerializer() - - class Meta: - model = VotePair - fields = [ - 'vote', - 'pair_id', - 'project', - ] - class PaginatedVotePairResponseSerializer(serializers.Serializer): count = serializers.IntegerField() next = serializers.CharField(allow_null=True) diff --git a/indexer_app/utils.py b/indexer_app/utils.py index b732426..0716ad9 100644 --- a/indexer_app/utils.py +++ b/indexer_app/utils.py @@ -669,8 +669,10 @@ async def handle_list_upvote( "created_at": created_at } + list_obj = List.objects.get(on_chain_id=data.get("list_id")) + await ListUpvote.objects.aupdate_or_create( - list_id=data.get("list_id") or receiver_id, + list=list_obj, account_id=signer_id, defaults=up_default ) diff --git a/lists/api.py b/lists/api.py index cc73e13..8c87118 100644 --- a/lists/api.py +++ b/lists/api.py @@ -14,6 +14,7 @@ from rest_framework.response import Response from rest_framework.views import APIView +from accounts.models import Account from api.pagination import pagination_parameters from api.pagination import CustomSizePageNumberPagination @@ -34,6 +35,12 @@ class ListsListAPI(APIView, CustomSizePageNumberPagination): @extend_schema( parameters=[ + OpenApiParameter( + "account", + str, + OpenApiParameter.QUERY, + description="Filter lists by account", + ), *pagination_parameters, ], responses={ @@ -50,12 +57,22 @@ class ListsListAPI(APIView, CustomSizePageNumberPagination): ), ], ), + 404: OpenApiResponse(description="Account not found"), 500: OpenApiResponse(description="Internal server error"), }, ) @method_decorator(cache_page(60 * 5)) def get(self, request: Request, *args, **kwargs): lists = List.objects.all() + account_id = request.query_params.get("account") + if account_id: + try: + account = Account.objects.get(id=account_id) + lists = lists.filter(owner=account) + except Account.DoesNotExist: + return Response( + {"message": f"Account with ID {account_id} not found."}, status=404 + ) results = self.paginate_queryset(lists, request, view=self) serializer = ListSerializer(results, many=True) return self.get_paginated_response(serializer.data)