add dropdown selection to change state of tickets

This commit is contained in:
j3d1 2023-12-28 21:08:26 +01:00
parent 515648ffa8
commit 626c9f23fe
7 changed files with 166 additions and 13 deletions

View file

@ -13,7 +13,7 @@ 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
from tickets.models import IssueThread, Comment, STATE_CHOICES, StateChange
class IssueSerializer(serializers.ModelSerializer):
@ -53,12 +53,33 @@ class IssueSerializer(serializers.ModelSerializer):
})
return sorted(timeline, key=lambda x: x['timestamp'])
def update(self, instance, validated_data):
if 'state' in validated_data:
instance.state = validated_data['state']
instance.save()
StateChange.objects.create(
issue_thread=instance,
state=validated_data['state'],
)
return instance
class IssueViewSet(viewsets.ModelViewSet):
serializer_class = IssueSerializer
queryset = IssueThread.objects.all()
class CommentSerializer(serializers.ModelSerializer):
class Meta:
model = Comment
fields = ('id', 'comment', 'timestamp', 'issue_thread')
class CommentViewSet(viewsets.ModelViewSet):
serializer_class = CommentSerializer
queryset = Comment.objects.all()
@api_view(['POST'])
@permission_classes([IsAuthenticated])
@permission_required('tickets.add_issuethread', raise_exception=True)
@ -116,10 +137,32 @@ def manual_ticket(request):
return Response(IssueSerializer(issue).data, status=status.HTTP_201_CREATED)
class StateSerializer(serializers.Serializer):
text = serializers.SerializerMethodField()
value = serializers.SerializerMethodField()
def get_text(self, obj):
return obj['text']
def get_value(self, obj):
return obj['value']
@api_view(['GET'])
@permission_classes([IsAuthenticated])
def get_available_states(request):
def get_state_choices():
for state in STATE_CHOICES:
yield {'value': list(state)[0], 'text': list(state)[1]}
return Response(get_state_choices())
router = routers.SimpleRouter()
router.register(r'tickets', IssueViewSet, basename='issues')
router.register(r'comments', CommentViewSet, basename='comments')
urlpatterns = ([
re_path(r'^tickets/(?P<pk>\d+)/reply/$', reply, name='reply'),
re_path(r'^tickets/manual/$', manual_ticket, name='manual_ticket'),
re_path(r'^tickets/states/$', get_available_states, name='get_available_states'),
] + router.urls)

View file

@ -3,11 +3,28 @@ from django_softdelete.models import SoftDeleteModel
from inventory.models import Event
STATE_CHOICES = (
('pending_new', 'New'),
('pending_open', 'Open'),
('pending_shipping', 'Needs to be shipped'),
('pending_physical_confirmation', 'Needs to be confirmed physically'),
('pending_return', 'Needs to be returned'),
('waiting_details', 'Waiting for details'),
('waiting_pre_shipping', 'Waiting for Address/Shipping Info'),
('closed_returned', 'Closed: Returned'),
('closed_shipped', 'Closed: Shipped'),
('closed_not_found', 'Closed: Not found'),
('closed_not_our_problem', 'Closed: Not our problem'),
('closed_duplicate', 'Closed: Duplicate'),
('closed_timeout', 'Closed: Timeout'),
('closed_spam', 'Closed: Spam'),
)
class IssueThread(SoftDeleteModel):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255)
state = models.CharField(max_length=255, default='new')
state = models.CharField('state', choices=STATE_CHOICES, max_length=32, default='pending_new')
assigned_to = models.CharField(max_length=255, null=True)
last_activity = models.DateTimeField(auto_now=True)
manually_created = models.BooleanField(default=False)

View file

@ -97,7 +97,7 @@ class IssueApiTest(TestCase):
response = self.client.post('/api/2/tickets/manual/', {'name': 'test issue', 'sender': 'test',
'recipient': 'test', 'body': 'test'})
self.assertEqual(response.status_code, 201)
self.assertEqual(response.json()['state'], 'new')
self.assertEqual(response.json()['state'], 'pending_new')
self.assertEqual(response.json()['name'], 'test issue')
self.assertEqual(response.json()['assigned_to'], None)
timeline = response.json()['timeline']
@ -108,3 +108,35 @@ class IssueApiTest(TestCase):
self.assertEqual(timeline[0]['subject'], 'test issue')
self.assertEqual(timeline[0]['body'], 'test')
def test_post_comment(self):
issue = IssueThread.objects.create(
name="test issue",
)
response = self.client.post('/api/2/comments/', {'comment': 'test', 'issue_thread': issue.id})
self.assertEqual(response.status_code, 201)
self.assertEqual(response.json()['comment'], 'test')
self.assertEqual(response.json()['issue_thread'], issue.id)
self.assertEqual(response.json()['timestamp'], response.json()['timestamp'])
def test_state_change(self):
issue = IssueThread.objects.create(
name="test issue",
)
response = self.client.patch(f'/api/2/tickets/{issue.id}/', {'state': 'pending_open'}, content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['state'], 'pending_open')
self.assertEqual(response.json()['name'], 'test issue')
self.assertEqual(response.json()['assigned_to'], None)
timeline = response.json()['timeline']
self.assertEqual(len(timeline), 1)
self.assertEqual(timeline[0]['type'], 'state')
self.assertEqual(timeline[0]['state'], 'pending_open')
def test_state_change_invalid_state(self):
issue = IssueThread.objects.create(
name="test issue",
)
response = self.client.patch(f'/api/2/tickets/{issue.id}/', {'state': 'invalid'}, content_type='application/json')
self.assertEqual(response.status_code, 400)