update prefetched fields
All checks were successful
/ test (push) Successful in 51s
/ deploy (push) Successful in 3m35s

This commit is contained in:
j3d1 2024-11-29 01:32:54 +01:00
parent b109e5995e
commit eb9e9088ca
4 changed files with 23 additions and 7 deletions

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)

View file

@ -32,7 +32,11 @@ class Item(SoftDeleteModel):
@property
def container(self):
try:
return self.container_history.order_by('-timestamp').first().container
history = sorted(self.container_history.all(), key=lambda x: x.timestamp, reverse=True)
if history:
return history[0].container
else:
return None
except AttributeError:
return None

View file

@ -60,7 +60,12 @@ class ItemSerializer(BasicItemSerializer):
fields = ['cid', 'box', 'id', 'description', 'file', 'dataImage', 'returned', 'event', 'related_issues',
'timeline']
read_only_fields = ['id']
prefetch_related_fields = ['comments', 'issue_relation_changes', 'container_history']
prefetch_related_fields = ['comments', 'issue_relation_changes', 'container_history',
'container_history__container', 'files', 'event',
'issue_relation_changes__issue_thread',
'issue_relation_changes__issue_thread__event',
'issue_relation_changes__issue_thread__state_changes',
'issue_relation_changes__issue_thread__assignments']
def to_internal_value(self, data):
container = None

View file

@ -48,7 +48,11 @@ class IssueSerializer(BasicIssueSerializer):
fields = ('id', 'timeline', 'name', 'state', 'assigned_to', 'last_activity', 'uuid', 'related_items', 'event')
read_only_fields = ('id', 'timeline', 'last_activity', 'uuid', 'related_items')
prefetch_related_fields = ['state_changes', 'comments', 'emails', 'emails__attachments', 'assignments',
'item_relation_changes', 'shipping_vouchers']
'item_relation_changes', 'shipping_vouchers', 'item_relation_changes__item',
'item_relation_changes__item__container_history', 'event',
'item_relation_changes__item__container_history__container',
'item_relation_changes__item__files', 'item_relation_changes__item__event',
'item_relation_changes__item__event']
def to_internal_value(self, data):
ret = super().to_internal_value(data)