2014-05-08 18:47:16 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
|
|
from .models import Ticket, Message, Queue
|
|
|
|
|
|
|
|
|
|
|
|
class QueueSerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Queue
|
2015-04-30 09:51:55 +00:00
|
|
|
fields = ('url', 'id', 'name', 'default', 'notify')
|
2014-05-08 18:47:16 +00:00
|
|
|
read_only_fields = ('name', 'default', 'notify')
|
|
|
|
|
|
|
|
|
|
|
|
class MessageSerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Message
|
2015-04-30 09:51:55 +00:00
|
|
|
fields = ('url', 'id', 'author', 'author_name', 'content', 'created_on')
|
2014-05-13 13:46:40 +00:00
|
|
|
read_only_fields = ('author', 'author_name', 'created_on')
|
2014-05-08 18:47:16 +00:00
|
|
|
|
|
|
|
def get_identity(self, data):
|
|
|
|
return data.get('id')
|
|
|
|
|
2015-04-28 15:23:57 +00:00
|
|
|
def create(self, validated_data):
|
|
|
|
validated_data['author'] = self.context['request'].user
|
2015-05-19 13:27:04 +00:00
|
|
|
return super(MessageSerializer, self).create(validated_data)
|
2014-05-08 18:47:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TicketSerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
""" Validates if this zone generates a correct zone file """
|
2015-04-28 15:23:57 +00:00
|
|
|
messages = MessageSerializer(required=False, many=True, read_only=True)
|
2015-04-23 14:34:04 +00:00
|
|
|
is_read = serializers.SerializerMethodField()
|
2014-05-08 18:47:16 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Ticket
|
|
|
|
fields = (
|
2014-05-13 13:46:40 +00:00
|
|
|
'url', 'id', 'creator', 'creator_name', 'owner', 'queue', 'subject',
|
|
|
|
'description', 'state', 'messages', 'is_read'
|
2014-05-08 18:47:16 +00:00
|
|
|
)
|
2014-05-13 13:46:40 +00:00
|
|
|
read_only_fields = ('creator', 'creator_name', 'owner')
|
2014-05-08 18:47:16 +00:00
|
|
|
|
|
|
|
def get_is_read(self, obj):
|
|
|
|
return obj.is_read_by(self.context['request'].user)
|
|
|
|
|
2015-04-28 15:23:57 +00:00
|
|
|
def create(self, validated_data):
|
|
|
|
validated_data['creator'] = self.context['request'].user
|
|
|
|
return super(TicketSerializer, self).create(validated_data)
|