update prefetched fields

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

View file

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

View file

@ -32,7 +32,11 @@ class Item(SoftDeleteModel):
@property @property
def container(self): def container(self):
try: 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: except AttributeError:
return None return None

View file

@ -60,7 +60,12 @@ class ItemSerializer(BasicItemSerializer):
fields = ['cid', 'box', 'id', 'description', 'file', 'dataImage', 'returned', 'event', 'related_issues', fields = ['cid', 'box', 'id', 'description', 'file', 'dataImage', 'returned', 'event', 'related_issues',
'timeline'] 'timeline']
read_only_fields = ['id'] 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): def to_internal_value(self, data):
container = None container = None

View file

@ -48,7 +48,11 @@ class IssueSerializer(BasicIssueSerializer):
fields = ('id', 'timeline', 'name', 'state', 'assigned_to', 'last_activity', 'uuid', 'related_items', 'event') fields = ('id', 'timeline', 'name', 'state', 'assigned_to', 'last_activity', 'uuid', 'related_items', 'event')
read_only_fields = ('id', 'timeline', 'last_activity', 'uuid', 'related_items') read_only_fields = ('id', 'timeline', 'last_activity', 'uuid', 'related_items')
prefetch_related_fields = ['state_changes', 'comments', 'emails', 'emails__attachments', 'assignments', 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): def to_internal_value(self, data):
ret = super().to_internal_value(data) ret = super().to_internal_value(data)