Added REST API support for Tickets and Queues

This commit is contained in:
Marc 2014-05-08 18:47:16 +00:00
parent 2007f34738
commit b4792f8cbf
5 changed files with 91 additions and 8 deletions

View File

@ -54,14 +54,15 @@ class MessageReadOnlyInline(admin.TabularInline):
def content_html(self, obj):
context = {
'num': obj.num,
'number': obj.number,
'time': display_timesince(obj.created_on),
'author': link('author')(self, obj),
}
summary = _("#%(num)i Updated by %(author)s about %(time)s") % context
summary = _("#%(number)i Updated by %(author)s about %(time)s") % context
header = '<strong style="color:#666;">%s</strong><hr />' % summary
content = markdown(obj.content)
content = content.replace('>\n', '>')
content = '<div style="padding-left:20px;">%s</div>' % content
return header + content
content_html.short_description = _("Content")
content_html.allow_tags = True

View File

@ -0,0 +1,41 @@
from rest_framework import viewsets, mixins
from rest_framework.decorators import action
from rest_framework.response import Response
from orchestra.api import router
from .models import Ticket, Queue
from .serializers import TicketSerializer, QueueSerializer
class TicketViewSet(viewsets.ModelViewSet):
model = Ticket
serializer_class = TicketSerializer
@action()
def mark_as_read(self, request, pk=None):
ticket = self.get_object()
ticket.mark_as_read()
return Response({'status': 'Ticket marked as readed'})
@action()
def mark_as_unread(self, request, pk=None):
ticket = self.get_object()
ticket.mark_as_unread()
return Response({'status': 'Ticket marked as unreaded'})
def get_queryset(self):
qs = super(TicketViewSet, self).get_queryset()
return qs.filter(creator__account=self.request.user.account_id)
class QueueViewSet(mixins.ListModelMixin,
mixins.RetrieveModelMixin,
viewsets.GenericViewSet):
model = Queue
serializer_class = QueueSerializer
router.register(r'tickets', TicketViewSet)
router.register(r'ticket-queues', QueueViewSet)

View File

@ -115,10 +115,6 @@ class Ticket(models.Model):
"""
return Ticket.objects.filter(pk=self.pk).involved_by(user).exists()
def is_visible_by(self, user):
""" returns whether ticket is visible by user """
return Ticket.objects.filter(pk=self.pk).visible_by(user).exists()
def get_cc_emails(self):
return self.cc.split(',') if self.cc else []
@ -170,7 +166,7 @@ class Message(models.Model):
super(Message, self).save(*args, **kwargs)
@property
def num(self):
def number(self):
return self.ticket.messages.filter(id__lte=self.id).count()

View File

@ -0,0 +1,45 @@
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
fields = ('id', 'author', 'content', 'created_on')
read_only_fields = ('author', '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 = (
'url', 'id', 'creator', 'owner', 'queue', 'subject', 'description',
'state', 'messages', 'is_read'
)
read_only_fields = ('creator', '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)

View File

@ -140,7 +140,7 @@ function install_requirements () {
kombu==3.0.8 \
Markdown==2.4 \
django-debug-toolbar==1.0.1 \
djangorestframework==2.3.12 \
djangorestframework==2.3.13 \
paramiko==1.12.1 \
Pygments==1.6 \
django-filter==0.7 \