From 4501dce99473470f116e45ac210807fb9d5f27a5 Mon Sep 17 00:00:00 2001 From: jedi Date: Wed, 6 Dec 2023 04:34:22 +0100 Subject: [PATCH] stash --- core/tickets/api_v2.py | 36 +++++++++++++- core/tickets/models.py | 4 +- core/tickets/tests/v2/test_tickets.py | 68 ++++++++++++++++++++++++++- 3 files changed, 103 insertions(+), 5 deletions(-) diff --git a/core/tickets/api_v2.py b/core/tickets/api_v2.py index 60b7150..e013ed6 100644 --- a/core/tickets/api_v2.py +++ b/core/tickets/api_v2.py @@ -1,12 +1,44 @@ from rest_framework import routers, viewsets, serializers -from tickets.models import IssueThread +from tickets.models import IssueThread, Comment, StateChange class IssueSerializer(serializers.ModelSerializer): + timeline = serializers.SerializerMethodField() + class Meta: model = IssueThread - fields = '__all__' + fields = ('id', 'timeline') + read_only_fields = ('id', 'timeline') + + @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']) class IssueViewSet(viewsets.ModelViewSet): diff --git a/core/tickets/models.py b/core/tickets/models.py index fb02df0..6a3810e 100644 --- a/core/tickets/models.py +++ b/core/tickets/models.py @@ -10,13 +10,13 @@ class IssueThread(SoftDeleteModel): class Comment(models.Model): id = models.AutoField(primary_key=True) - issue_thread = models.ForeignKey(IssueThread, on_delete=models.CASCADE) + issue_thread = models.ForeignKey(IssueThread, on_delete=models.CASCADE, related_name='comments') comment = models.TextField() timestamp = models.DateTimeField(auto_now_add=True) class StateChange(models.Model): id = models.AutoField(primary_key=True) - issue_thread = models.ForeignKey(IssueThread, on_delete=models.CASCADE) + issue_thread = models.ForeignKey(IssueThread, on_delete=models.CASCADE, related_name='state_changes') state = models.CharField(max_length=255) timestamp = models.DateTimeField(auto_now_add=True) diff --git a/core/tickets/tests/v2/test_tickets.py b/core/tickets/tests/v2/test_tickets.py index 4a0a802..23395de 100644 --- a/core/tickets/tests/v2/test_tickets.py +++ b/core/tickets/tests/v2/test_tickets.py @@ -1,13 +1,79 @@ +from datetime import datetime, timedelta + from django.test import TestCase, Client +from mail.models import Email +from tickets.models import IssueThread, StateChange, Comment + client = Client() class IssueApiTest(TestCase): - def test_issues(self): + def test_issues_empty(self): response = client.get('/api/2/tickets/') self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), []) + def test_issues(self): + now = datetime.now() + issue = IssueThread.objects.create() + mail1 = Email.objects.create( + subject='test', + body='test', + sender='test', + recipient='test', + issue_thread=issue, + timestamp=now, + ) + state = StateChange.objects.create( + issue_thread=issue, + state="new", + timestamp=now + timedelta(seconds=1), + ) + mail2 = Email.objects.create( + subject='test', + body='test', + sender='test', + recipient='test', + issue_thread=issue, + in_reply_to=mail1.reference, + timestamp=now + timedelta(seconds=2), + ) + comment = Comment.objects.create( + issue_thread=issue, + comment="test", + timestamp=now + timedelta(seconds=3), + ) + response = client.get('/api/2/tickets/') + self.assertEqual(response.status_code, 200) + self.assertEqual(len(response.json()), 1) + self.assertEqual(response.json()[0]['id'], issue.id) + self.assertEqual(len(response.json()[0]['timeline']), 4) + self.assertEqual(response.json()[0]['timeline'][0]['type'], 'mail') + self.assertEqual(response.json()[0]['timeline'][1]['type'], 'state') + self.assertEqual(response.json()[0]['timeline'][2]['type'], 'mail') + self.assertEqual(response.json()[0]['timeline'][3]['type'], 'comment') + self.assertEqual(response.json()[0]['timeline'][0]['id'], mail1.id) + self.assertEqual(response.json()[0]['timeline'][1]['id'], state.id) + self.assertEqual(response.json()[0]['timeline'][2]['id'], mail2.id) + self.assertEqual(response.json()[0]['timeline'][3]['id'], comment.id) + self.assertEqual(response.json()[0]['timeline'][0]['sender'], 'test') + self.assertEqual(response.json()[0]['timeline'][0]['recipient'], 'test') + self.assertEqual(response.json()[0]['timeline'][0]['subject'], 'test') + self.assertEqual(response.json()[0]['timeline'][0]['body'], 'test') + self.assertEqual(response.json()[0]['timeline'][0]['timestamp'], + mail1.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ')) + self.assertEqual(response.json()[0]['timeline'][1]['state'], 'new') + self.assertEqual(response.json()[0]['timeline'][1]['timestamp'], + state.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ')) + self.assertEqual(response.json()[0]['timeline'][2]['sender'], 'test') + self.assertEqual(response.json()[0]['timeline'][2]['recipient'], 'test') + self.assertEqual(response.json()[0]['timeline'][2]['subject'], 'test') + self.assertEqual(response.json()[0]['timeline'][2]['body'], 'test') + self.assertEqual(response.json()[0]['timeline'][2]['timestamp'], + mail2.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ')) + self.assertEqual(response.json()[0]['timeline'][3]['comment'], 'test') + self.assertEqual(response.json()[0]['timeline'][3]['timestamp'], + comment.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))