Skip to content

Commit 99682da

Browse files
authored
Merge pull request #153 from PotLock/testnet
use select and prefetch related
2 parents 57da50f + a88c3ed commit 99682da

File tree

3 files changed

+13
-20
lines changed

3 files changed

+13
-20
lines changed

accounts/api.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,8 @@ def get(self, request: Request, *args, **kwargs):
348348
{"message": f"Account with ID {account_id} not found."}, status=404
349349
)
350350

351-
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.
351+
# donations = account.donations.select_related('donor', 'pot', 'recipient', 'referrer', 'chef', 'token').prefetch_related('pot__admins').all()
352+
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.
352353
results = self.paginate_queryset(donations, request, view=self)
353354
serializer = DonationSerializer(results, many=True)
354355
return self.get_paginated_response(serializer.data)

lists/api.py

+9-17
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from api.pagination import pagination_parameters
1919
from api.pagination import CustomSizePageNumberPagination
2020

21-
from .models import List, ListRegistrationStatus
21+
from .models import List, ListRegistration, ListRegistrationStatus
2222
from .serializers import (
2323
PAGINATED_LIST_EXAMPLE,
2424
PAGINATED_LIST_REGISTRATION_EXAMPLE,
@@ -69,7 +69,7 @@ class ListsListAPI(APIView, CustomSizePageNumberPagination):
6969
)
7070
@method_decorator(cache_page(60 * 1))
7171
def get(self, request: Request, *args, **kwargs):
72-
lists = List.objects.all()
72+
lists = List.objects.all().select_related("owner").prefetch_related("admins")
7373
account_id = request.query_params.get("account")
7474
if account_id:
7575
try:
@@ -121,7 +121,7 @@ class ListDetailAPI(APIView):
121121
def get(self, request: Request, *args, **kwargs):
122122
list_id = kwargs.get("list_id")
123123
try:
124-
list_obj = List.objects.get(on_chain_id=list_id)
124+
list_obj = List.objects.select_related("owner").prefetch_related("admins").get(on_chain_id=list_id)
125125
except List.DoesNotExist:
126126
return Response(
127127
{"message": f"List with onchain ID {list_id} not found."}, status=404
@@ -176,14 +176,10 @@ class ListRegistrationsAPI(APIView, CustomSizePageNumberPagination):
176176
@method_decorator(cache_page(60 * 1))
177177
def get(self, request: Request, *args, **kwargs):
178178
list_id = kwargs.get("list_id")
179-
try:
180-
list_obj = List.objects.prefetch_related('registrations').get(on_chain_id=list_id)
181-
except List.DoesNotExist:
182-
return Response(
183-
{"message": f"List with on chain ID {list_id} not found."}, status=404
184-
)
179+
#list_obj = List.objects.prefetch_related('registrations').get(on_chain_id=list_id)
180+
registrations = ListRegistration.objects.filter(list__on_chain_id=list_id).select_related("list__owner", "registrant", "registered_by").prefetch_related("list__admins")
185181

186-
registrations = list_obj.registrations.select_related().all()
182+
# registrations = list_obj.registrations.select_related().all()
187183
status_param = request.query_params.get("status")
188184
category_param = request.query_params.get("category")
189185
search_param = request.query_params.get("search")
@@ -239,14 +235,10 @@ class ListRandomRegistrationAPI(APIView):
239235
)
240236
def get(self, request: Request, *args, **kwargs):
241237
list_id = kwargs.get("list_id")
242-
try:
243-
list_obj = List.objects.get(on_chain_id=list_id)
244-
except List.DoesNotExist:
245-
return Response(
246-
{"message": f"List on chain ID {list_id} not found."}, status=404
247-
)
238+
# list_obj = List.objects.get(on_chain_id=list_id)
239+
registrations = ListRegistration.objects.filter(list__on_chain_id=list_id).select_related("list__owner", "registrant", "registered_by").prefetch_related("list__admins")
248240

249-
registrations = list_obj.registrations.all()
241+
# registrations = list_obj.registrations.all()
250242
status_param = request.query_params.get("status")
251243
if status_param:
252244
if status_param not in ListRegistrationStatus.values:

pots/api.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class PotsListAPI(APIView, CustomSizePageNumberPagination):
7979
)
8080
@method_decorator(cache_page(60 * 5))
8181
def get(self, request: Request, *args, **kwargs):
82-
pots = Pot.objects.all()
82+
pots = Pot.objects.select_related('account', 'pot_factory', 'deployer', 'owner', 'chef').prefetch_related('admins').all()
8383
results = self.paginate_queryset(pots, request, view=self)
8484
serializer = PotSerializer(results, many=True)
8585
return self.get_paginated_response(serializer.data)
@@ -250,7 +250,7 @@ def get(self, request: Request, *args, **kwargs):
250250
except Pot.DoesNotExist:
251251
return Response({"message": f"Pot with ID {pot_id} not found."}, status=404)
252252

253-
donations = pot.donations.all()
253+
donations = pot.donations.select_related("donor", "token", 'pot', 'pot__deployer', 'pot__owner', 'pot__chef', 'recipient', 'referrer', 'chef').prefetch_related('pot__admins').all()
254254
results = self.paginate_queryset(donations, request, view=self)
255255
serializer = DonationSerializer(results, many=True)
256256
return self.get_paginated_response(serializer.data)

0 commit comments

Comments
 (0)