2020-12-29 16:19:59 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
|
|
|
from rest_framework import viewsets, mixins
|
|
|
|
from common.exceptions import JMSException
|
|
|
|
from common.utils import lazyproperty
|
2022-03-11 11:31:29 +00:00
|
|
|
from rbac.permissions import RBACPermission
|
2020-12-29 16:19:59 +00:00
|
|
|
from tickets import serializers
|
2022-03-21 03:14:49 +00:00
|
|
|
from tickets.models import Ticket, Comment
|
2020-12-29 16:19:59 +00:00
|
|
|
from tickets.permissions.comment import IsAssignee, IsApplicant, IsSwagger
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = ['CommentViewSet']
|
|
|
|
|
|
|
|
|
|
|
|
class CommentViewSet(mixins.CreateModelMixin, viewsets.ReadOnlyModelViewSet):
|
|
|
|
serializer_class = serializers.CommentSerializer
|
2022-03-15 08:50:51 +00:00
|
|
|
permission_classes = (RBACPermission, IsSwagger | IsAssignee | IsApplicant)
|
2022-03-16 06:43:20 +00:00
|
|
|
rbac_perms = {
|
|
|
|
'*': 'tickets.view_ticket'
|
|
|
|
}
|
2020-12-29 16:19:59 +00:00
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def ticket(self):
|
|
|
|
if getattr(self, 'swagger_fake_view', False):
|
|
|
|
return None
|
|
|
|
ticket_id = self.request.query_params.get('ticket_id')
|
2021-01-13 09:49:03 +00:00
|
|
|
ticket = Ticket.all().filter(pk=ticket_id).first()
|
|
|
|
if not ticket:
|
|
|
|
raise JMSException('Not found Ticket object about `id={}`'.format(ticket_id))
|
|
|
|
return ticket
|
2020-12-29 16:19:59 +00:00
|
|
|
|
|
|
|
def get_serializer_context(self):
|
|
|
|
context = super().get_serializer_context()
|
|
|
|
context['ticket'] = self.ticket
|
|
|
|
return context
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2022-03-21 03:14:49 +00:00
|
|
|
if getattr(self, 'swagger_fake_view', False):
|
|
|
|
return Comment.objects.none()
|
2020-12-29 16:19:59 +00:00
|
|
|
queryset = self.ticket.comments.all()
|
|
|
|
return queryset
|