c3lf-system-3/core/tickets/api_v2.py

55 lines
1.7 KiB
Python
Raw Normal View History

2023-11-23 22:17:20 +00:00
from rest_framework import routers, viewsets, serializers
2023-12-06 03:34:22 +00:00
from tickets.models import IssueThread, Comment, StateChange
2023-11-23 22:17:20 +00:00
class IssueSerializer(serializers.ModelSerializer):
2023-12-06 03:34:22 +00:00
timeline = serializers.SerializerMethodField()
2023-11-23 22:17:20 +00:00
class Meta:
model = IssueThread
2023-12-06 03:51:47 +00:00
fields = ('id', 'timeline', 'name', 'state', 'assigned_to', 'last_activity')
read_only_fields = ('id', 'timeline', 'last_activity')
2023-12-06 03:34:22 +00:00
@staticmethod
def get_timeline(obj):
timeline = []
for comment in obj.comments.all():
timeline.append({
'type': 'comment',
'id': comment.id,
'timestamp': comment.timestamp,
'comment': comment.comment,
})
for state_change in obj.state_changes.all():
timeline.append({
'type': 'state',
'id': state_change.id,
'timestamp': state_change.timestamp,
'state': state_change.state,
})
for email in obj.emails.all():
timeline.append({
'type': 'mail',
'id': email.id,
'timestamp': email.timestamp,
'sender': email.sender,
'recipient': email.recipient,
'subject': email.subject,
'body': email.body,
})
return sorted(timeline, key=lambda x: x['timestamp'])
2023-11-23 22:17:20 +00:00
class IssueViewSet(viewsets.ModelViewSet):
serializer_class = IssueSerializer
queryset = IssueThread.objects.all()
permission_classes = []
authentication_classes = []
router = routers.SimpleRouter()
router.register(r'tickets', IssueViewSet, basename='issues')
urlpatterns = router.urls