Speed up sql
This commit is contained in:
parent
fdf5ab8ad1
commit
81a0959547
4 changed files with 18 additions and 12 deletions
|
@ -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):
|
||||
|
|
|
@ -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', 'item_relation_changes', 'shipping_vouchers')
|
||||
|
||||
|
||||
class RelationViewSet(viewsets.ModelViewSet):
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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_relation_changes.all()]) if self.item_relation_changes.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)
|
||||
|
|
Loading…
Reference in a new issue