Speed up sql
All checks were successful
/ test (pull_request) Successful in 48s
/ test (push) Successful in 49s
/ deploy (push) Successful in 5m13s

This commit is contained in:
eleon 2024-11-21 21:14:38 +01:00
parent fdf5ab8ad1
commit 81a0959547
4 changed files with 18 additions and 12 deletions

View file

@ -24,7 +24,7 @@ class BasicItemSerializer(serializers.ModelSerializer):
def get_file(self, instance): def get_file(self, instance):
if len(instance.files.all()) > 0: if len(instance.files.all()) > 0:
return instance.files.all().order_by('-created_at')[0].hash return sorted(instance.files.all(), key=lambda x: x.created_at, reverse=True)[0].hash
return None return None
def get_returned(self, instance): def get_returned(self, instance):

View file

@ -21,7 +21,7 @@ from tickets.shared_serializers import RelationSerializer
class IssueViewSet(viewsets.ModelViewSet): class IssueViewSet(viewsets.ModelViewSet):
serializer_class = IssueSerializer serializer_class = IssueSerializer
queryset = IssueThread.objects.all() queryset = IssueThread.objects.all().prefetch_related('state_changes', 'comments', 'emails', 'emails__attachments', 'assignments', 'assignments__assigned_to__username', 'item_relation_changes', 'shipping_vouchers')
class RelationViewSet(viewsets.ModelViewSet): class RelationViewSet(viewsets.ModelViewSet):

View file

@ -52,7 +52,11 @@ class IssueThread(SoftDeleteModel):
@property @property
def state(self): def state(self):
try: try:
return self.state_changes.order_by('-timestamp').first().state state_changes = sorted(self.state_changes.all(), key=lambda x: x.timestamp, reverse=True)
if state_changes:
return state_changes[0].state
else:
return None
except AttributeError: except AttributeError:
return 'none' return 'none'
@ -67,7 +71,11 @@ class IssueThread(SoftDeleteModel):
@property @property
def assigned_to(self): def assigned_to(self):
try: try:
return self.assignments.order_by('-timestamp').first().assigned_to assignments = sorted(self.assignments.all(), key=lambda x: x.timestamp, reverse=True)
if assignments:
return assignments[0].assigned_to
else:
return None
except AttributeError: except AttributeError:
return None return None

View file

@ -63,14 +63,12 @@ class IssueSerializer(BasicIssueSerializer):
@staticmethod @staticmethod
def get_last_activity(self): def get_last_activity(self):
try: try:
last_state_change = self.state_changes.order_by('-timestamp').first().timestamp \ last_state_change = max([t.timestamp for t in self.state_changes.all()]) if self.state_changes.exists() else None
if self.state_changes.count() > 0 else None last_comment = max([t.timestamp for t in self.comments.all()]) if self.comments.exists() else None
last_comment = self.comments.order_by('-timestamp').first().timestamp if self.comments.count() > 0 else None last_mail = max([t.timestamp for t in self.emails.all()]) if self.emails.exists() else None
last_mail = self.emails.order_by('-timestamp').first().timestamp if self.emails.count() > 0 else None last_assignment = max([t.timestamp for t in self.assignments.all()]) if self.assignments.exists() else None
last_assignment = self.assignments.order_by('-timestamp').first().timestamp if \
self.assignments.count() > 0 else None last_relation = max([t.timestamp for t in self.item_relation_changes.all()]) if self.item_relation_changes.exists() else None
last_relation = self.item_relation_changes.order_by('-timestamp').first().timestamp if \
self.item_relation_changes.count() > 0 else None
args = [x for x in [last_state_change, last_comment, last_mail, last_assignment, last_relation] if args = [x for x in [last_state_change, last_comment, last_mail, last_assignment, last_relation] if
x is not None] x is not None]
return max(args) return max(args)