jumpserver/apps/tickets/api/ticket.py

45 lines
1.3 KiB
Python
Raw Normal View History

2019-11-07 10:06:58 +00:00
# -*- coding: utf-8 -*-
#
2019-11-08 07:48:01 +00:00
from rest_framework import viewsets
from django.shortcuts import get_object_or_404
2019-11-07 10:06:58 +00:00
2019-11-15 10:55:35 +00:00
from common.permissions import IsValidUser
2019-11-08 07:48:01 +00:00
from common.utils import lazyproperty
from .. import serializers, models, mixins
2019-11-07 10:06:58 +00:00
2019-11-08 07:48:01 +00:00
class TicketViewSet(mixins.TicketMixin, viewsets.ModelViewSet):
serializer_class = serializers.TicketSerializer
queryset = models.Ticket.objects.all()
2019-11-15 10:55:35 +00:00
permission_classes = (IsValidUser,)
filter_fields = ['status', 'title', 'action', 'user_display']
2019-11-15 10:55:35 +00:00
search_fields = ['user_display', 'title']
2019-11-07 10:06:58 +00:00
2019-11-08 07:48:01 +00:00
class TicketCommentViewSet(viewsets.ModelViewSet):
2019-11-07 10:06:58 +00:00
serializer_class = serializers.CommentSerializer
2019-11-15 10:55:35 +00:00
http_method_names = ['get', 'post']
2019-11-07 10:06:58 +00:00
2019-11-08 07:48:01 +00:00
def check_permissions(self, request):
ticket = self.ticket
2019-11-15 10:55:35 +00:00
if request.user == ticket.user or \
request.user in ticket.assignees.all():
2019-11-08 07:48:01 +00:00
return True
return False
def get_serializer_context(self):
context = super().get_serializer_context()
context['ticket'] = self.ticket
return context
@lazyproperty
def ticket(self):
ticket_id = self.kwargs.get('ticket_id')
ticket = get_object_or_404(models.Ticket, pk=ticket_id)
return ticket
2019-11-07 10:06:58 +00:00
def get_queryset(self):
2019-11-08 07:48:01 +00:00
queryset = self.ticket.comments.all()
2019-11-07 10:06:58 +00:00
return queryset