diff --git a/accounts/api.py b/accounts/api.py index 8d2bb8b..242ffbc 100644 --- a/accounts/api.py +++ b/accounts/api.py @@ -348,7 +348,8 @@ def get(self, request: Request, *args, **kwargs): {"message": f"Account with ID {account_id} not found."}, status=404 ) - donations = Donation.objects.select_related('donor', 'pot', 'recipient', 'referrer', 'chef', 'token').prefetch_related('pot__admins').filter(donor=account) #TODO: this takes more time than just doing a prefetch_related for the fields. + # donations = account.donations.select_related('donor', 'pot', 'recipient', 'referrer', 'chef', 'token').prefetch_related('pot__admins').all() + donations = account.donations.select_related('donor', 'pot', 'pot__deployer', 'pot__owner', 'pot__chef', 'recipient', 'referrer', 'chef', 'token').prefetch_related('pot__admins').all()# (donor=account) #TODO: this takes more time than just doing a prefetch_related for the fields. results = self.paginate_queryset(donations, request, view=self) serializer = DonationSerializer(results, many=True) return self.get_paginated_response(serializer.data) diff --git a/lists/api.py b/lists/api.py index 0a3e86e..64745ce 100644 --- a/lists/api.py +++ b/lists/api.py @@ -18,7 +18,7 @@ from api.pagination import pagination_parameters from api.pagination import CustomSizePageNumberPagination -from .models import List, ListRegistrationStatus +from .models import List, ListRegistration, ListRegistrationStatus from .serializers import ( PAGINATED_LIST_EXAMPLE, PAGINATED_LIST_REGISTRATION_EXAMPLE, @@ -69,7 +69,7 @@ class ListsListAPI(APIView, CustomSizePageNumberPagination): ) @method_decorator(cache_page(60 * 1)) def get(self, request: Request, *args, **kwargs): - lists = List.objects.all() + lists = List.objects.all().select_related("owner").prefetch_related("admins") account_id = request.query_params.get("account") if account_id: try: @@ -121,7 +121,7 @@ class ListDetailAPI(APIView): def get(self, request: Request, *args, **kwargs): list_id = kwargs.get("list_id") try: - list_obj = List.objects.get(on_chain_id=list_id) + list_obj = List.objects.select_related("owner").prefetch_related("admins").get(on_chain_id=list_id) except List.DoesNotExist: return Response( {"message": f"List with onchain ID {list_id} not found."}, status=404 @@ -176,14 +176,10 @@ class ListRegistrationsAPI(APIView, CustomSizePageNumberPagination): @method_decorator(cache_page(60 * 1)) def get(self, request: Request, *args, **kwargs): list_id = kwargs.get("list_id") - try: - list_obj = List.objects.prefetch_related('registrations').get(on_chain_id=list_id) - except List.DoesNotExist: - return Response( - {"message": f"List with on chain ID {list_id} not found."}, status=404 - ) + #list_obj = List.objects.prefetch_related('registrations').get(on_chain_id=list_id) + registrations = ListRegistration.objects.filter(list__on_chain_id=list_id).select_related("list__owner", "registrant", "registered_by").prefetch_related("list__admins") - registrations = list_obj.registrations.select_related().all() + # registrations = list_obj.registrations.select_related().all() status_param = request.query_params.get("status") category_param = request.query_params.get("category") search_param = request.query_params.get("search") @@ -239,14 +235,10 @@ class ListRandomRegistrationAPI(APIView): ) def get(self, request: Request, *args, **kwargs): list_id = kwargs.get("list_id") - try: - list_obj = List.objects.get(on_chain_id=list_id) - except List.DoesNotExist: - return Response( - {"message": f"List on chain ID {list_id} not found."}, status=404 - ) + # list_obj = List.objects.get(on_chain_id=list_id) + registrations = ListRegistration.objects.filter(list__on_chain_id=list_id).select_related("list__owner", "registrant", "registered_by").prefetch_related("list__admins") - registrations = list_obj.registrations.all() + # registrations = list_obj.registrations.all() status_param = request.query_params.get("status") if status_param: if status_param not in ListRegistrationStatus.values: diff --git a/pots/api.py b/pots/api.py index 2d90ac7..3363983 100644 --- a/pots/api.py +++ b/pots/api.py @@ -79,7 +79,7 @@ class PotsListAPI(APIView, CustomSizePageNumberPagination): ) @method_decorator(cache_page(60 * 5)) def get(self, request: Request, *args, **kwargs): - pots = Pot.objects.all() + pots = Pot.objects.select_related('account', 'pot_factory', 'deployer', 'owner', 'chef').prefetch_related('admins').all() results = self.paginate_queryset(pots, request, view=self) serializer = PotSerializer(results, many=True) return self.get_paginated_response(serializer.data) @@ -250,7 +250,7 @@ def get(self, request: Request, *args, **kwargs): except Pot.DoesNotExist: return Response({"message": f"Pot with ID {pot_id} not found."}, status=404) - donations = pot.donations.all() + donations = pot.donations.select_related("donor", "token", 'pot', 'pot__deployer', 'pot__owner', 'pot__chef', 'recipient', 'referrer', 'chef').prefetch_related('pot__admins').all() results = self.paginate_queryset(donations, request, view=self) serializer = DonationSerializer(results, many=True) return self.get_paginated_response(serializer.data)