From b4792f8cbf151c1905c579c28c91d7442af4af22 Mon Sep 17 00:00:00 2001 From: Marc Date: Thu, 8 May 2014 18:47:16 +0000 Subject: [PATCH] Added REST API support for Tickets and Queues --- orchestra/apps/issues/admin.py | 5 ++-- orchestra/apps/issues/api.py | 41 +++++++++++++++++++++++++ orchestra/apps/issues/models.py | 6 +--- orchestra/apps/issues/serializers.py | 45 ++++++++++++++++++++++++++++ orchestra/bin/orchestra-admin | 2 +- 5 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 orchestra/apps/issues/api.py create mode 100644 orchestra/apps/issues/serializers.py diff --git a/orchestra/apps/issues/admin.py b/orchestra/apps/issues/admin.py index 7d50bdc1..6c7cc56e 100644 --- a/orchestra/apps/issues/admin.py +++ b/orchestra/apps/issues/admin.py @@ -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 = '%s
' % summary content = markdown(obj.content) content = content.replace('>\n', '>') + content = '
%s
' % content return header + content content_html.short_description = _("Content") content_html.allow_tags = True diff --git a/orchestra/apps/issues/api.py b/orchestra/apps/issues/api.py new file mode 100644 index 00000000..94630552 --- /dev/null +++ b/orchestra/apps/issues/api.py @@ -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) diff --git a/orchestra/apps/issues/models.py b/orchestra/apps/issues/models.py index 73a2a31f..0c8eb749 100644 --- a/orchestra/apps/issues/models.py +++ b/orchestra/apps/issues/models.py @@ -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() diff --git a/orchestra/apps/issues/serializers.py b/orchestra/apps/issues/serializers.py new file mode 100644 index 00000000..817bad05 --- /dev/null +++ b/orchestra/apps/issues/serializers.py @@ -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) diff --git a/orchestra/bin/orchestra-admin b/orchestra/bin/orchestra-admin index 7149f705..a547ac83 100755 --- a/orchestra/bin/orchestra-admin +++ b/orchestra/bin/orchestra-admin @@ -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 \