Merge branch 'dev' of github.com:jumpserver/jumpserver into dev

pull/4092/head
ibuler 2020-06-10 12:58:43 +08:00
commit e1ab453780
3 changed files with 38 additions and 1 deletions

View File

@ -11,7 +11,7 @@ from common.serializers import CeleryTaskSerializer
from orgs.utils import current_org
from ..models import Task, AdHoc, AdHocExecution
from ..serializers import TaskSerializer, AdHocSerializer, \
AdHocExecutionSerializer
AdHocExecutionSerializer, TaskDetailSerializer
from ..tasks import run_ansible_task
__all__ = [
@ -26,6 +26,11 @@ class TaskViewSet(viewsets.ModelViewSet):
serializer_class = TaskSerializer
permission_classes = (IsOrgAdmin,)
def get_serializer_class(self):
if self.action == 'retrieve':
return TaskDetailSerializer
return super().get_serializer_class()
def get_queryset(self):
queryset = super().get_queryset()
queryset = queryset.select_related('latest_execution')

View File

@ -118,6 +118,24 @@ class Task(PeriodTaskModelMixin, OrgModelMixin):
kwargs = {"callback": self.callback}
return name, task, args, kwargs
@lazyproperty
def last_success(self):
return self._last_adhocexecution(True, 'contacted')
@lazyproperty
def last_failure(self):
return self._last_adhocexecution(False, 'dark')
def _last_adhocexecution(self, is_success, key):
obj = AdHocExecution.objects.filter(task_id=self.id, is_success=is_success).order_by('-date_finished').first()
body = obj.summary.get(key)
if body:
asset, body = body.popitem()
action, body = body.popitem()
return asset, action, body.get('msg', '')
else:
return '', '', ''
def __str__(self):
return self.name + '@' + str(self.org_id)

View File

@ -59,6 +59,20 @@ class TaskSerializer(serializers.ModelSerializer):
]
class TaskDetailSerializer(TaskSerializer):
last_success = serializers.SerializerMethodField()
last_failure = serializers.SerializerMethodField()
def get_last_success(self, obj):
return obj.last_success[0], ''
def get_last_failure(self, obj):
return obj.last_failure[0], ' => '.join(obj.last_failure[1:])
class Meta(TaskSerializer.Meta):
fields = TaskSerializer.Meta.fields + ['last_success', 'last_failure']
class AdHocSerializer(serializers.ModelSerializer):
become_display = serializers.ReadOnlyField()