From 9526ca98208889cb6f6df734e23c21c8f350a067 Mon Sep 17 00:00:00 2001 From: Timothy Hobbs Date: Wed, 29 Nov 2023 22:03:09 +0000 Subject: [PATCH] Do the full update ticket flow when followups are posted from the API --- docs/integration.rst | 5 ++++- helpdesk/serializers.py | 34 +++++++++++++++++++++++----------- helpdesk/tests/test_api.py | 8 ++++---- helpdesk/update_ticket.py | 11 +++++++---- helpdesk/views/api.py | 3 +++ 5 files changed, 41 insertions(+), 20 deletions(-) diff --git a/docs/integration.rst b/docs/integration.rst index 5f131a90..138306ec 100644 --- a/docs/integration.rst +++ b/docs/integration.rst @@ -1,5 +1,8 @@ Integrating django-helpdesk into your application -------------------------------------------------- +================================================= + +Ticket submission with embeded iframe +------------------------------------- Django-helpdesk associates an email address with each submitted ticket. If you integrate django-helpdesk directly into your django application, logged in users will automatically have their email address set when they visit the `/tickets/submit/` form. If you wish to pre-fill fields in this form, you can do so simply by setting the following query parameters: diff --git a/helpdesk/serializers.py b/helpdesk/serializers.py index 0f43d303..f6d34857 100644 --- a/helpdesk/serializers.py +++ b/helpdesk/serializers.py @@ -1,12 +1,14 @@ -from .forms import TicketForm -from .lib import format_time_spent, process_attachments -from .models import CustomField, FollowUp, FollowUpAttachment, Ticket -from .user import HelpdeskUser from django.contrib.auth import get_user_model from django.contrib.humanize.templatetags import humanize from rest_framework import serializers from rest_framework.exceptions import ValidationError +from .forms import TicketForm +from .lib import format_time_spent +from .models import CustomField, FollowUp, FollowUpAttachment, Ticket +from .user import HelpdeskUser +from .update_ticket import update_ticket + class DatatablesTicketSerializer(serializers.ModelSerializer): """ @@ -124,20 +126,30 @@ class FollowUpSerializer(serializers.ModelSerializer): write_only=True, required=False ) + date = serializers.DateTimeField(read_only=True) class Meta: model = FollowUp fields = ( - 'id', 'ticket', 'date', 'title', 'comment', 'public', 'user', 'new_status', 'message_id', - 'time_spent', 'followupattachment_set', 'attachments' + 'id', 'ticket', 'user', 'title', 'comment', 'public', 'new_status', + 'time_spent', 'attachments', 'followupattachment_set', 'date', 'message_id', ) def create(self, validated_data): - attachments = validated_data.pop('attachments', None) - followup = super().create(validated_data) - if attachments: - process_attachments(followup, attachments) - return followup + if validated_data["user"]: + user = validated_data["user"] + else: + user = self.context['request'].user + return update_ticket( + user=user, + ticket=validated_data["ticket"], + title=validated_data.get("title", None), + comment=validated_data.get("comment", ""), + files=validated_data.get("attachments", None), + public=validated_data.get("public", False), + new_status=validated_data.get("new_status", None), + time_spent=validated_data.get("time_spent", None), + ) class TicketSerializer(serializers.ModelSerializer): diff --git a/helpdesk/tests/test_api.py b/helpdesk/tests/test_api.py index 71e3c418..b2c93aa8 100644 --- a/helpdesk/tests/test_api.py +++ b/helpdesk/tests/test_api.py @@ -274,15 +274,15 @@ class TicketTest(APITestCase): 'followup_set': [OrderedDict([ ('id', 1), ('ticket', 1), - ('date', '2022-06-30T23:09:44'), + ('user', 1), ('title', 'Ticket Opened'), ('comment', 'Test description\nMulti lines'), ('public', True), - ('user', 1), ('new_status', None), - ('message_id', None), ('time_spent', None), - ('followupattachment_set', []) + ('followupattachment_set', []), + ('date', '2022-06-30T23:09:44'), + ('message_id', None), ])], 'custom_varchar': 'test', 'custom_text': 'multi\nline', diff --git a/helpdesk/update_ticket.py b/helpdesk/update_ticket.py index d26a9ed3..658c4b17 100644 --- a/helpdesk/update_ticket.py +++ b/helpdesk/update_ticket.py @@ -198,11 +198,13 @@ def update_ticket( files=None, public=False, owner=-1, + ticket_title=None, priority=-1, new_status=None, time_spent=None, due_date=None, new_checklists=None, + message_id=None, ): # We need to allow the 'ticket' and 'queue' contexts to be applied to the # comment. @@ -235,7 +237,7 @@ def update_ticket( owner = ticket.assigned_to.id f = FollowUp(ticket=ticket, date=timezone.now(), comment=comment, - time_spent=time_spent) + time_spent=time_spent, message_id=message_id, title=title) if is_helpdesk_staff(user): f.user = user @@ -262,15 +264,15 @@ def update_ticket( files = process_attachments(f, files) if files else [] - if title and title != ticket.title: + if ticket_title and ticket_title != ticket.title: c = TicketChange( followup=f, field=_('Title'), old_value=ticket.title, - new_value=title, + new_value=ticket_title, ) c.save() - ticket.title = title + ticket.title = ticket_title if new_status != old_status: c = TicketChange( @@ -386,4 +388,5 @@ def update_ticket( # auto subscribe user if enabled add_staff_subscription(user, ticket) + return f diff --git a/helpdesk/views/api.py b/helpdesk/views/api.py index 6ee52a7f..f6cbe9e9 100644 --- a/helpdesk/views/api.py +++ b/helpdesk/views/api.py @@ -54,6 +54,9 @@ class FollowUpViewSet(viewsets.ModelViewSet): pagination_class = ConservativePagination permission_classes = [IsAdminUser] + def perform_create(self, serializer): + serializer.save(user=self.request.user) + class FollowUpAttachmentViewSet(viewsets.ModelViewSet): queryset = FollowUpAttachment.objects.all()