From eb9e9088ca1106f819bcb0d397ed07b4a7084c56 Mon Sep 17 00:00:00 2001 From: jedi Date: Fri, 29 Nov 2024 01:32:54 +0100 Subject: [PATCH] update prefetched fields --- core/inventory/api_v2.py | 11 +++++++---- core/inventory/models.py | 6 +++++- core/inventory/serializers.py | 7 ++++++- core/tickets/serializers.py | 6 +++++- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/core/inventory/api_v2.py b/core/inventory/api_v2.py index 64f9b27..326c049 100644 --- a/core/inventory/api_v2.py +++ b/core/inventory/api_v2.py @@ -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) diff --git a/core/inventory/models.py b/core/inventory/models.py index 10796e1..231094e 100644 --- a/core/inventory/models.py +++ b/core/inventory/models.py @@ -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 diff --git a/core/inventory/serializers.py b/core/inventory/serializers.py index 90a75aa..26a5be4 100644 --- a/core/inventory/serializers.py +++ b/core/inventory/serializers.py @@ -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 diff --git a/core/tickets/serializers.py b/core/tickets/serializers.py index b382b38..50cdb72 100644 --- a/core/tickets/serializers.py +++ b/core/tickets/serializers.py @@ -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)