diff --git a/core/inventory/shared_serializers.py b/core/inventory/shared_serializers.py index 0bd44c3..2cafd5f 100644 --- a/core/inventory/shared_serializers.py +++ b/core/inventory/shared_serializers.py @@ -24,7 +24,7 @@ class BasicItemSerializer(serializers.ModelSerializer): def get_file(self, instance): 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 def get_returned(self, instance): diff --git a/core/tickets/api_v2.py b/core/tickets/api_v2.py index 1ed7e02..ea84db5 100644 --- a/core/tickets/api_v2.py +++ b/core/tickets/api_v2.py @@ -21,7 +21,7 @@ from tickets.shared_serializers import RelationSerializer class IssueViewSet(viewsets.ModelViewSet): serializer_class = IssueSerializer - queryset = IssueThread.objects.all() + queryset = IssueThread.objects.all().prefetch_related('state_changes', 'comments', 'emails', 'emails__attachments', 'assignments', 'assignments__assigned_to__username', 'related_items', 'item_relations', 'shipping_vouchers') class RelationViewSet(viewsets.ModelViewSet): diff --git a/core/tickets/models.py b/core/tickets/models.py index 2c14687..f7ddb7b 100644 --- a/core/tickets/models.py +++ b/core/tickets/models.py @@ -52,7 +52,11 @@ class IssueThread(SoftDeleteModel): @property def state(self): 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: return 'none' @@ -67,7 +71,11 @@ class IssueThread(SoftDeleteModel): @property def assigned_to(self): 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: return None diff --git a/core/tickets/serializers.py b/core/tickets/serializers.py index aae37ba..6fd2e68 100644 --- a/core/tickets/serializers.py +++ b/core/tickets/serializers.py @@ -63,14 +63,12 @@ class IssueSerializer(BasicIssueSerializer): @staticmethod def get_last_activity(self): try: - last_state_change = self.state_changes.order_by('-timestamp').first().timestamp \ - if self.state_changes.count() > 0 else None - last_comment = self.comments.order_by('-timestamp').first().timestamp if self.comments.count() > 0 else None - last_mail = self.emails.order_by('-timestamp').first().timestamp if self.emails.count() > 0 else None - last_assignment = self.assignments.order_by('-timestamp').first().timestamp if \ - self.assignments.count() > 0 else None - last_relation = self.item_relation_changes.order_by('-timestamp').first().timestamp if \ - self.item_relation_changes.count() > 0 else None + last_state_change = max([t.timestamp for t in self.state_changes.all()]) if self.state_changes.exists() else None + last_comment = max([t.timestamp for t in self.comments.all()]) if self.comments.exists() else None + last_mail = max([t.timestamp for t in self.emails.all()]) if self.emails.exists() else None + last_assignment = max([t.timestamp for t in self.assignments.all()]) if self.assignments.exists() else None + + last_relation = max([t.timestamp for t in self.item_relations.all()]) if self.item_relations.exists() else None args = [x for x in [last_state_change, last_comment, last_mail, last_assignment, last_relation] if x is not None] return max(args)