Create FollowUpAttachment serializer + handle attachment in TicketSerializer and in FollowUpSerializer in order to attach directly one or multiple attachments to the created followup.

This commit is contained in:
Benbb96 2022-06-24 23:45:26 +02:00
parent bd413837c2
commit 9dbe283dd4
3 changed files with 39 additions and 11 deletions

View File

@ -4,8 +4,8 @@ from django.contrib.humanize.templatetags import humanize
from rest_framework.exceptions import ValidationError
from .forms import TicketForm
from .models import Ticket, CustomField, FollowUp
from .lib import format_time_spent
from .models import Ticket, CustomField, FollowUp, FollowUpAttachment
from .lib import format_time_spent, process_attachments
from .user import HelpdeskUser
@ -71,22 +71,44 @@ class DatatablesTicketSerializer(serializers.ModelSerializer):
return obj.kbitem.title if obj.kbitem else ""
class FollowUpAttachmentSerializer(serializers.ModelSerializer):
class Meta:
model = FollowUpAttachment
fields = ('id', 'followup', 'file', 'filename', 'mime_type', 'size')
class FollowUpSerializer(serializers.ModelSerializer):
followupattachment_set = FollowUpAttachmentSerializer(many=True, read_only=True)
attachments = serializers.ListField(
child=serializers.FileField(),
write_only=True,
required=False
)
class Meta:
model = FollowUp
fields = (
'id', 'ticket', 'date', 'title', 'comment', 'public', 'user', 'new_status', 'message_id', 'time_spent'
'id', 'ticket', 'date', 'title', 'comment', 'public', 'user', 'new_status', 'message_id',
'time_spent', 'followupattachment_set', 'attachments'
)
def create(self, validated_data):
attachments = validated_data.pop('attachments', None)
followup = super().create(validated_data)
if attachments:
process_attachments(followup, attachments)
return followup
class TicketSerializer(serializers.ModelSerializer):
followup_set = FollowUpSerializer(many=True, read_only=True)
attachment = serializers.FileField(write_only=True, required=False)
class Meta:
model = Ticket
fields = (
'id', 'queue', 'title', 'description', 'resolution', 'submitter_email', 'assigned_to', 'status', 'on_hold',
'priority', 'due_date', 'merged_to', 'followup_set'
'priority', 'due_date', 'merged_to', 'attachment', 'followup_set'
)
def __init__(self, *args, **kwargs):
@ -109,7 +131,9 @@ class TicketSerializer(serializers.ModelSerializer):
if data.get('merged_to'):
data['merged_to'] = data['merged_to'].id
ticket_form = TicketForm(data=data, queue_choices=queue_choices)
files = {'attachment': data.pop('attachment', None)}
ticket_form = TicketForm(data=data, files=files, queue_choices=queue_choices)
if ticket_form.is_valid():
ticket = ticket_form.save(user=self.context['request'].user)
ticket.set_custom_field_values()

View File

@ -17,7 +17,7 @@ from rest_framework.routers import DefaultRouter
from helpdesk.decorators import helpdesk_staff_member_required, protect_view
from helpdesk.views import feeds, staff, public, login
from helpdesk import settings as helpdesk_settings
from helpdesk.views.api import TicketViewSet, CreateUserView, FollowUpViewSet
from helpdesk.views.api import TicketViewSet, CreateUserView, FollowUpViewSet, FollowUpAttachmentViewSet
if helpdesk_settings.HELPDESK_KB_ENABLED:
from helpdesk.views import kb
@ -177,6 +177,7 @@ if helpdesk_settings.HELPDESK_ACTIVATE_API_ENDPOINT:
router = DefaultRouter()
router.register(r"tickets", TicketViewSet, basename="ticket")
router.register(r"followups", FollowUpViewSet, basename="followups")
router.register(r"followups-attachments", FollowUpAttachmentViewSet, basename="followupattachments")
router.register(r"users", CreateUserView, basename="user")
urlpatterns += [re_path(r"^api/", include(router.urls))]

View File

@ -4,8 +4,8 @@ from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import CreateModelMixin
from django.contrib.auth import get_user_model
from helpdesk.models import Ticket, FollowUp
from helpdesk.serializers import TicketSerializer, UserSerializer, FollowUpSerializer
from helpdesk.models import Ticket, FollowUp, FollowUpAttachment
from helpdesk.serializers import TicketSerializer, UserSerializer, FollowUpSerializer, FollowUpAttachmentSerializer
class TicketViewSet(viewsets.ModelViewSet):
@ -29,14 +29,17 @@ class TicketViewSet(viewsets.ModelViewSet):
class FollowUpViewSet(viewsets.ModelViewSet):
"""
A viewset that provides the standard actions to handle FollowUp
"""
queryset = FollowUp.objects.all()
serializer_class = FollowUpSerializer
permission_classes = [IsAdminUser]
class FollowUpAttachmentViewSet(viewsets.ModelViewSet):
queryset = FollowUpAttachment.objects.all()
serializer_class = FollowUpAttachmentSerializer
permission_classes = [IsAdminUser]
class CreateUserView(CreateModelMixin, GenericViewSet):
queryset = get_user_model().objects.all()
serializer_class = UserSerializer