django-orchestra/orchestra/contrib/issues/serializers.py

46 lines
1.6 KiB
Python
Raw Permalink Normal View History

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')
read_only_fields = ('name', 'default', 'notify')
class MessageSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Message
2016-03-31 16:02:50 +00:00
fields = ('id', 'author', 'author_name', 'content', 'created_at')
read_only_fields = ('author', 'author_name', 'created_at')
def get_identity(self, data):
return data.get('id')
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)
class TicketSerializer(serializers.HyperlinkedModelSerializer):
""" Validates if this zone generates a correct zone file """
messages = MessageSerializer(required=False, many=True, read_only=True)
2015-04-23 14:34:04 +00:00
is_read = serializers.SerializerMethodField()
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-13 13:46:40 +00:00
read_only_fields = ('creator', 'creator_name', 'owner')
def get_is_read(self, obj):
return obj.is_read_by(self.context['request'].user)
def create(self, validated_data):
validated_data['creator'] = self.context['request'].user
return super(TicketSerializer, self).create(validated_data)