jumpserver/apps/ops/api/adhoc.py

80 lines
2.3 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
#
2017-12-10 16:29:25 +00:00
from django.shortcuts import get_object_or_404
from rest_framework import viewsets, generics
from rest_framework.views import Response
2018-07-23 04:55:13 +00:00
from common.permissions import IsOrgAdmin
from orgs.utils import current_org
from ..models import Task, AdHoc, AdHocRunHistory
from ..serializers import TaskSerializer, AdHocSerializer, \
2018-04-02 07:54:49 +00:00
AdHocRunHistorySerializer
from ..tasks import run_ansible_task
__all__ = [
'TaskViewSet', 'TaskRun', 'AdHocViewSet', 'AdHocRunHistoryViewSet'
]
class TaskViewSet(viewsets.ModelViewSet):
2018-07-14 16:55:05 +00:00
queryset = Task.objects.all()
serializer_class = TaskSerializer
2018-07-23 04:55:13 +00:00
permission_classes = (IsOrgAdmin,)
def get_queryset(self):
queryset = super().get_queryset()
if current_org.is_real():
queryset = queryset.filter(created_by=current_org.id)
else:
queryset = queryset.filter(created_by='')
return queryset
2017-12-10 16:29:25 +00:00
class TaskRun(generics.RetrieveAPIView):
2018-07-14 16:55:05 +00:00
queryset = Task.objects.all()
# serializer_class = TaskViewSet
2018-07-23 04:55:13 +00:00
permission_classes = (IsOrgAdmin,)
def retrieve(self, request, *args, **kwargs):
task = self.get_object()
2018-04-01 15:45:37 +00:00
t = run_ansible_task.delay(str(task.id))
return Response({"task": t.id})
2017-12-10 16:29:25 +00:00
class AdHocViewSet(viewsets.ModelViewSet):
2018-07-14 16:55:05 +00:00
queryset = AdHoc.objects.all()
2017-12-10 16:29:25 +00:00
serializer_class = AdHocSerializer
2018-07-23 04:55:13 +00:00
permission_classes = (IsOrgAdmin,)
2017-12-10 16:29:25 +00:00
def get_queryset(self):
task_id = self.request.query_params.get('task')
if task_id:
task = get_object_or_404(Task, id=task_id)
self.queryset = self.queryset.filter(task=task)
return self.queryset
class AdHocRunHistoryViewSet(viewsets.ModelViewSet):
2018-07-14 16:55:05 +00:00
queryset = AdHocRunHistory.objects.all()
2017-12-10 16:29:25 +00:00
serializer_class = AdHocRunHistorySerializer
2018-07-23 04:55:13 +00:00
permission_classes = (IsOrgAdmin,)
2017-12-10 16:29:25 +00:00
def get_queryset(self):
task_id = self.request.query_params.get('task')
2017-12-20 03:30:15 +00:00
adhoc_id = self.request.query_params.get('adhoc')
2017-12-10 16:29:25 +00:00
if task_id:
task = get_object_or_404(Task, id=task_id)
adhocs = task.adhoc.all()
self.queryset = self.queryset.filter(adhoc__in=adhocs)
2017-12-20 03:30:15 +00:00
if adhoc_id:
adhoc = get_object_or_404(AdHoc, id=adhoc_id)
self.queryset = self.queryset.filter(adhoc=adhoc)
2017-12-10 16:29:25 +00:00
return self.queryset
2018-03-30 14:03:43 +00:00
2018-04-01 15:45:37 +00:00
2018-03-30 14:03:43 +00:00
2018-04-02 07:54:49 +00:00