Do the full update ticket flow when followups are posted from the API

This commit is contained in:
Timothy Hobbs 2023-11-29 22:03:09 +00:00 committed by Timothy Hobbs
parent ffb7522e79
commit 9526ca9820
5 changed files with 41 additions and 20 deletions

View File

@ -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:

View File

@ -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):

View File

@ -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',

View File

@ -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

View File

@ -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()