django-orchestra-test/orchestra/apps/issues/serializers.py

46 lines
1.5 KiB
Python
Raw Normal View History

from rest_framework import serializers
from .models import Ticket, Message, Queue
class QueueSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Queue
fields = ('url', 'name', 'default', 'notify')
read_only_fields = ('name', 'default', 'notify')
class MessageSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Message
2014-05-13 13:46:40 +00:00
fields = ('id', 'author', 'author_name', 'content', 'created_on')
read_only_fields = ('author', 'author_name', 'created_on')
def get_identity(self, data):
return data.get('id')
def save_object(self, obj, **kwargs):
obj.author = self.context['request'].user
super(MessageSerializer, self).save_object(obj, **kwargs)
class TicketSerializer(serializers.HyperlinkedModelSerializer):
""" Validates if this zone generates a correct zone file """
messages = MessageSerializer(required=False, many=True)
is_read = serializers.SerializerMethodField('get_is_read')
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 save_object(self, obj, **kwargs):
obj.creator = self.context['request'].user
super(TicketSerializer, self).save_object(obj, **kwargs)