stash
All checks were successful
/ test (push) Successful in 51s
/ deploy (push) Successful in 4m56s

This commit is contained in:
j3d1 2024-11-29 01:32:54 +01:00
parent b109e5995e
commit fda7b80ca4

View file

@ -26,13 +26,16 @@ class ContainerViewSet(viewsets.ModelViewSet):
class ItemViewSet(viewsets.ModelViewSet):
serializer_class = ItemSerializer
def get_queryset(self):
queryset = Item.objects.all()
def prefetch_queryset(self, queryset):
serializer = self.get_serializer_class()
if hasattr(serializer, 'Meta') and hasattr(serializer.Meta, 'prefetch_related_fields'):
queryset = queryset.prefetch_related(*serializer.Meta.prefetch_related_fields)
return queryset
def get_queryset(self):
queryset = Item.objects.all()
return self.prefetch_queryset(queryset)
def filter_items(items, query):
query_tokens = query.split(' ')
@ -69,7 +72,7 @@ def item(request, event_slug):
if request.method == 'GET':
if not request.user.has_event_perm(event, 'view_item'):
return Response(status=403)
return Response(ItemSerializer(vs.get_queryset().filter(event=event), many=True).data)
return Response(ItemSerializer(vs.prefetch_queryset(Item.objects.filter(event=event)), many=True).data)
elif request.method == 'POST':
if not request.user.has_event_perm(event, 'add_item'):
return Response(status=403)
@ -108,7 +111,7 @@ def add_comment(request, event_slug, id):
def item_by_id(request, event_slug, id):
try:
event = Event.objects.get(slug=event_slug)
item = ItemViewSet().get_queryset().get(event=event, id=id)
item = Item.objects.get(event=event, id=id)
if request.method == 'GET':
if not request.user.has_event_perm(event, 'view_item'):
return Response(status=403)