2018-10-15 02:23:28 +02:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
|
|
from .models import Ticket
|
2019-06-16 10:25:29 +02:00
|
|
|
from .lib import format_time_spent
|
2018-10-15 02:23:28 +02:00
|
|
|
|
|
|
|
from django.contrib.humanize.templatetags import humanize
|
|
|
|
|
|
|
|
"""
|
|
|
|
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 19:56:54 +02:00
|
|
|
|
2019-10-11 12:41:06 +02:00
|
|
|
class DatatablesTicketSerializer(serializers.ModelSerializer):
|
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 ""
|