2023-11-09 19:07:26 +01:00
|
|
|
from django.contrib.auth import get_user_model
|
2022-07-22 03:26:41 +02:00
|
|
|
from django.contrib.humanize.templatetags import humanize
|
|
|
|
from rest_framework import serializers
|
|
|
|
from rest_framework.exceptions import ValidationError
|
2018-10-15 02:23:28 +02:00
|
|
|
|
2023-11-29 23:03:09 +01:00
|
|
|
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
|
|
|
|
|
2018-10-15 19:56:54 +02:00
|
|
|
|
2019-10-11 12:41:06 +02:00
|
|
|
class DatatablesTicketSerializer(serializers.ModelSerializer):
|
2022-03-31 17:19:49 +02:00
|
|
|
"""
|
|
|
|
A serializer for the Ticket model, returns data in the format as required by
|
|
|
|
datatables for ticket_list.html. Called from staff.datatables_ticket_list.
|
|
|
|
"""
|
2018-10-15 02:23:28 +02:00
|
|
|
ticket = serializers.SerializerMethodField()
|
|
|
|
assigned_to = serializers.SerializerMethodField()
|
2020-01-13 15:43:47 +01:00
|
|
|
submitter = serializers.SerializerMethodField()
|
2018-10-15 02:23:28 +02:00
|
|
|
created = serializers.SerializerMethodField()
|
|
|
|
due_date = serializers.SerializerMethodField()
|
|
|
|
status = serializers.SerializerMethodField()
|
|
|
|
row_class = serializers.SerializerMethodField()
|
2019-02-19 23:01:51 +01:00
|
|
|
time_spent = serializers.SerializerMethodField()
|
2019-03-10 12:09:10 +01:00
|
|
|
queue = serializers.SerializerMethodField()
|
2020-02-03 14:06:45 +01:00
|
|
|
kbitem = serializers.SerializerMethodField()
|
2018-10-15 02:23:28 +02:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Ticket
|
|
|
|
# fields = '__all__'
|
2019-02-06 14:24:43 +01:00
|
|
|
fields = ('ticket', 'id', 'priority', 'title', 'queue', 'status',
|
2020-01-13 15:43:47 +01:00
|
|
|
'created', 'due_date', 'assigned_to', 'submitter', 'row_class',
|
2020-02-03 14:06:45 +01:00
|
|
|
'time_spent', 'kbitem')
|
2018-10-15 02:23:28 +02:00
|
|
|
|
2019-03-10 12:09:10 +01:00
|
|
|
def get_queue(self, obj):
|
2020-10-08 22:47:52 +02:00
|
|
|
return {"title": obj.queue.title, "id": obj.queue.id}
|
2019-03-10 12:09:10 +01:00
|
|
|
|
2018-10-15 02:23:28 +02:00
|
|
|
def get_ticket(self, obj):
|
2020-10-08 22:47:52 +02:00
|
|
|
return str(obj.id) + " " + obj.ticket
|
2018-10-15 02:23:28 +02:00
|
|
|
|
|
|
|
def get_status(self, obj):
|
2020-10-08 22:47:52 +02:00
|
|
|
return obj.get_status
|
2018-10-15 02:23:28 +02:00
|
|
|
|
|
|
|
def get_created(self, obj):
|
2020-10-08 22:47:52 +02:00
|
|
|
return humanize.naturaltime(obj.created)
|
2018-10-15 02:23:28 +02:00
|
|
|
|
|
|
|
def get_due_date(self, obj):
|
2020-10-08 22:47:52 +02:00
|
|
|
return humanize.naturaltime(obj.due_date)
|
2018-10-15 02:23:28 +02:00
|
|
|
|
|
|
|
def get_assigned_to(self, obj):
|
|
|
|
if obj.assigned_to:
|
2020-01-07 04:18:48 +01:00
|
|
|
if obj.assigned_to.get_full_name():
|
2020-10-08 22:47:52 +02:00
|
|
|
return obj.assigned_to.get_full_name()
|
|
|
|
elif obj.assigned_to.email:
|
|
|
|
return obj.assigned_to.email
|
2018-10-15 02:23:28 +02:00
|
|
|
else:
|
2020-10-08 22:47:52 +02:00
|
|
|
return obj.assigned_to.username
|
2018-10-15 02:23:28 +02:00
|
|
|
else:
|
2020-10-08 22:47:52 +02:00
|
|
|
return "None"
|
2018-10-15 02:23:28 +02:00
|
|
|
|
2020-01-13 15:43:47 +01:00
|
|
|
def get_submitter(self, obj):
|
|
|
|
return obj.submitter_email
|
|
|
|
|
2019-02-19 23:01:51 +01:00
|
|
|
def get_time_spent(self, obj):
|
2019-06-16 10:25:29 +02:00
|
|
|
return format_time_spent(obj.time_spent)
|
2019-02-19 23:01:51 +01:00
|
|
|
|
2018-10-15 02:23:28 +02:00
|
|
|
def get_row_class(self, obj):
|
2020-10-08 22:47:52 +02:00
|
|
|
return obj.get_priority_css_class
|
2020-02-03 14:06:45 +01:00
|
|
|
|
|
|
|
def get_kbitem(self, obj):
|
|
|
|
return obj.kbitem.title if obj.kbitem else ""
|
2022-03-31 17:19:49 +02:00
|
|
|
|
|
|
|
|
2022-06-24 23:45:26 +02:00
|
|
|
class FollowUpAttachmentSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = FollowUpAttachment
|
|
|
|
fields = ('id', 'followup', 'file', 'filename', 'mime_type', 'size')
|
|
|
|
|
|
|
|
|
2022-06-24 22:22:08 +02:00
|
|
|
class FollowUpSerializer(serializers.ModelSerializer):
|
2022-07-13 10:22:11 +02:00
|
|
|
followupattachment_set = FollowUpAttachmentSerializer(
|
|
|
|
many=True, read_only=True)
|
2022-06-24 23:45:26 +02:00
|
|
|
attachments = serializers.ListField(
|
|
|
|
child=serializers.FileField(),
|
|
|
|
write_only=True,
|
|
|
|
required=False
|
|
|
|
)
|
2023-11-29 23:03:09 +01:00
|
|
|
date = serializers.DateTimeField(read_only=True)
|
2022-06-24 23:45:26 +02:00
|
|
|
|
2022-06-24 22:22:08 +02:00
|
|
|
class Meta:
|
|
|
|
model = FollowUp
|
|
|
|
fields = (
|
2023-11-29 23:03:09 +01:00
|
|
|
'id', 'ticket', 'user', 'title', 'comment', 'public', 'new_status',
|
|
|
|
'time_spent', 'attachments', 'followupattachment_set', 'date', 'message_id',
|
2022-06-24 22:22:08 +02:00
|
|
|
)
|
|
|
|
|
2022-06-24 23:45:26 +02:00
|
|
|
def create(self, validated_data):
|
2023-11-29 23:03:09 +01:00
|
|
|
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),
|
|
|
|
)
|
2022-06-24 23:45:26 +02:00
|
|
|
|
2022-06-24 22:22:08 +02:00
|
|
|
|
2024-01-16 23:19:32 +01:00
|
|
|
class UserSerializer(serializers.ModelSerializer):
|
|
|
|
password = serializers.CharField(write_only=True)
|
2022-06-24 22:22:08 +02:00
|
|
|
|
2022-03-31 17:19:49 +02:00
|
|
|
class Meta:
|
2024-01-16 23:19:32 +01:00
|
|
|
model = get_user_model()
|
|
|
|
fields = ('first_name', 'last_name', 'username', 'email', 'password')
|
2022-03-31 17:19:49 +02:00
|
|
|
|
2024-01-16 23:19:32 +01:00
|
|
|
def create(self, validated_data):
|
|
|
|
user = super(UserSerializer, self).create(validated_data)
|
|
|
|
user.is_active = True
|
|
|
|
user.set_password(validated_data['password'])
|
|
|
|
user.save()
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
|
|
class BaseTicketSerializer(serializers.ModelSerializer):
|
2022-03-31 17:19:49 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
# Add custom fields
|
|
|
|
for field in CustomField.objects.all():
|
|
|
|
self.fields['custom_%s' % field.name] = field.build_api_field()
|
|
|
|
|
2024-01-16 23:19:32 +01:00
|
|
|
|
|
|
|
class PublicTicketListingSerializer(BaseTicketSerializer):
|
|
|
|
"""
|
|
|
|
A serializer to be used by the public API for listing tickets. Don't expose private fields here!
|
|
|
|
"""
|
|
|
|
ticket = serializers.SerializerMethodField()
|
|
|
|
submitter = serializers.SerializerMethodField()
|
|
|
|
created = serializers.SerializerMethodField()
|
|
|
|
due_date = serializers.SerializerMethodField()
|
|
|
|
status = serializers.SerializerMethodField()
|
|
|
|
queue = serializers.SerializerMethodField()
|
|
|
|
kbitem = serializers.SerializerMethodField()
|
|
|
|
secret_key = serializers.CharField(read_only=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Ticket
|
|
|
|
# fields = '__all__'
|
|
|
|
fields = ('ticket', 'id', 'title', 'queue', 'status',
|
|
|
|
'created', 'due_date', 'submitter', 'kbitem', 'secret_key')
|
|
|
|
|
|
|
|
def get_queue(self, obj):
|
|
|
|
return {"title": obj.queue.title, "id": obj.queue.id}
|
|
|
|
|
|
|
|
def get_ticket(self, obj):
|
|
|
|
return str(obj.id) + " " + obj.ticket
|
|
|
|
|
|
|
|
def get_status(self, obj):
|
|
|
|
return obj.get_status
|
|
|
|
|
|
|
|
def get_created(self, obj):
|
|
|
|
return humanize.naturaltime(obj.created)
|
|
|
|
|
|
|
|
def get_due_date(self, obj):
|
|
|
|
return humanize.naturaltime(obj.due_date)
|
|
|
|
|
|
|
|
def get_submitter(self, obj):
|
|
|
|
return obj.submitter_email
|
|
|
|
|
|
|
|
def get_kbitem(self, obj):
|
|
|
|
return obj.kbitem.title if obj.kbitem else ""
|
|
|
|
|
|
|
|
|
|
|
|
class TicketSerializer(BaseTicketSerializer):
|
|
|
|
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', 'attachment', 'followup_set'
|
|
|
|
)
|
|
|
|
|
2022-03-31 17:19:49 +02:00
|
|
|
def create(self, validated_data):
|
|
|
|
""" Use TicketForm to validate and create ticket """
|
|
|
|
queues = HelpdeskUser(self.context['request'].user).get_queues()
|
|
|
|
queue_choices = [(q.id, q.title) for q in queues]
|
|
|
|
data = validated_data.copy()
|
|
|
|
data['body'] = data['description']
|
|
|
|
# TicketForm needs id for ForeignKey (not the instance themselves)
|
|
|
|
data['queue'] = data['queue'].id
|
2022-04-07 00:42:12 +02:00
|
|
|
if data.get('assigned_to'):
|
2022-03-31 17:19:49 +02:00
|
|
|
data['assigned_to'] = data['assigned_to'].id
|
2022-04-07 00:42:12 +02:00
|
|
|
if data.get('merged_to'):
|
2022-03-31 17:19:49 +02:00
|
|
|
data['merged_to'] = data['merged_to'].id
|
|
|
|
|
2022-06-24 23:45:26 +02:00
|
|
|
files = {'attachment': data.pop('attachment', None)}
|
|
|
|
|
2022-07-13 10:22:11 +02:00
|
|
|
ticket_form = TicketForm(
|
|
|
|
data=data, files=files, queue_choices=queue_choices)
|
2022-03-31 17:19:49 +02:00
|
|
|
if ticket_form.is_valid():
|
|
|
|
ticket = ticket_form.save(user=self.context['request'].user)
|
|
|
|
ticket.set_custom_field_values()
|
|
|
|
return ticket
|
|
|
|
|
|
|
|
raise ValidationError(ticket_form.errors)
|
|
|
|
|
|
|
|
def update(self, instance, validated_data):
|
|
|
|
instance = super().update(instance, validated_data)
|
|
|
|
instance.save_custom_field_values(validated_data)
|
|
|
|
return instance
|