Skip to content

Commit

Permalink
add filtering and sorting to rounds list
Browse files Browse the repository at this point in the history
  • Loading branch information
Prometheo committed Sep 20, 2024
1 parent 8b14697 commit a09cda3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
7 changes: 6 additions & 1 deletion api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
)
from base.api import StatsAPI
from donations.api import DonationContractConfigAPI
from grantpicks.api import ProjectListAPI, ProjectRoundVotesAPI, RoundDetailAPI, RoundsListAPI
from grantpicks.api import ProjectListAPI, ProjectRoundVotesAPI, RoundApplicationsAPI, RoundDetailAPI, RoundsListAPI
from lists.api import (
ListDetailAPI,
ListRandomRegistrationAPI,
Expand Down Expand Up @@ -136,5 +136,10 @@
path("v1/round/<int:round_id>/", RoundDetailAPI.as_view(), name="rounds_api_by_id"),
path("v1/round/<int:round_id>/<int:project_id>/votes", ProjectRoundVotesAPI.as_view(), name="project_round_votes_api_by_id"),
path("v1/projects", ProjectListAPI.as_view(), name="projects_api"),
path(
"v1/rounds/<str:round_id>/applications",
RoundApplicationsAPI.as_view(),
name="rounds_applications_api",
),

]
51 changes: 49 additions & 2 deletions grantpicks/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
PaginatedDonationsResponseSerializer,
)

from .models import Project, Round
from .models import Project, ProjectStatus, Round
from .serializers import (
PAGINATED_PROJECT_EXAMPLE,
PAGINATED_ROUND_APPLICATION_EXAMPLE,
Expand All @@ -50,6 +50,12 @@ class RoundsListAPI(APIView, CustomSizePageNumberPagination):

@extend_schema(
parameters=[
OpenApiParameter(
name="sort",
type=str,
location=OpenApiParameter.QUERY,
description="Sort by field, e.g., deployed_at, vault_total_deposits",
),
*pagination_parameters,
],
responses={
Expand All @@ -66,11 +72,21 @@ class RoundsListAPI(APIView, CustomSizePageNumberPagination):
),
],
),
500: OpenApiResponse(description="Internal server error"),
},
)
@method_decorator(cache_page(60 * 1))
def get(self, request: Request, *args, **kwargs):
rounds = Round.objects.all()
sort = request.query_params.get("sort", None)
if sort == "deployed_at":
rounds = rounds.order_by(
"-deployed_at"
)
if sort == "vault_total_deposits":
rounds = rounds.order_by(
"-vault_total_deposits"
)
results = self.paginate_queryset(rounds, request, view=self)
serializer = RoundSerializer(results, many=True)
return self.get_paginated_response(serializer.data)
Expand All @@ -88,7 +104,7 @@ class RoundDetailAPI(APIView):
examples=[
OpenApiExample(
"example-1",
summary="Simple round example",
summary="Simple Round example",
description="Example response for round detail",
value=SIMPLE_ROUND_EXAMPLE,
response_only=True,
Expand Down Expand Up @@ -197,6 +213,18 @@ class ProjectListAPI(APIView, CustomSizePageNumberPagination):

@extend_schema(
parameters=[
OpenApiParameter(
"status",
str,
OpenApiParameter.QUERY,
description="Filter projects by status",
),
OpenApiParameter(
"owner",
str,
OpenApiParameter.QUERY,
description="Filter projects by owner id",
),
*pagination_parameters,
],
responses={
Expand All @@ -213,11 +241,30 @@ class ProjectListAPI(APIView, CustomSizePageNumberPagination):
),
],
),
400: OpenApiResponse(description="Invalid status value"),
404: OpenApiResponse(description="owner not found"),
500: OpenApiResponse(description="Internal server error"),
},
)
@method_decorator(cache_page(60 * 5))
def get(self, request: Request, *args, **kwargs):
projects = Project.objects.all()
status_param = request.query_params.get("status")
if status_param:
if status_param not in ProjectStatus.values:
return Response(
{"message": f"Invalid status value: {status_param}"}, status=400
)
projects = projects.filter(status=status_param)
project_owner = request.query_params.get("owner")
if project_owner:
try:
account = Account.objects.get(id=project_owner)
except Account.DoesNotExist:
return Response(
{"message": f"Account with ID {project_owner} not found."}, status=404
)
projects = projects.filter(owner=account)
results = self.paginate_queryset(projects, request, view=self)
serializer = ProjectSerializer(results, many=True)
return self.get_paginated_response(serializer.data)

0 comments on commit a09cda3

Please sign in to comment.