make the 'last_activity' field in /tickets show the actual last activity by making it a virtual field
This commit is contained in:
parent
9aeb6a319f
commit
1804939407
7 changed files with 135 additions and 6 deletions
|
@ -13,11 +13,12 @@ from core.settings import MAIL_DOMAIN
|
|||
from mail.models import Email
|
||||
from mail.protocol import send_smtp, make_reply, collect_references
|
||||
from notify_sessions.models import SystemEvent
|
||||
from tickets.models import IssueThread, Comment, STATE_CHOICES, StateChange
|
||||
from tickets.models import IssueThread, Comment, STATE_CHOICES
|
||||
|
||||
|
||||
class IssueSerializer(serializers.ModelSerializer):
|
||||
timeline = serializers.SerializerMethodField()
|
||||
last_activity = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = IssueThread
|
||||
|
@ -36,6 +37,18 @@ class IssueSerializer(serializers.ModelSerializer):
|
|||
raise serializers.ValidationError('invalid state')
|
||||
return attrs
|
||||
|
||||
@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
|
||||
args = [x for x in [last_state_change, last_comment, last_mail] if x is not None]
|
||||
return max(args)
|
||||
except AttributeError:
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def get_timeline(obj):
|
||||
timeline = []
|
||||
|
@ -65,6 +78,9 @@ class IssueSerializer(serializers.ModelSerializer):
|
|||
})
|
||||
return sorted(timeline, key=lambda x: x['timestamp'])
|
||||
|
||||
def get_queryset(self):
|
||||
return IssueThread.objects.all().order_by('-last_activity')
|
||||
|
||||
|
||||
class IssueViewSet(viewsets.ModelViewSet):
|
||||
serializer_class = IssueSerializer
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue