mirror of https://github.com/jumpserver/jumpserver
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.4 KiB
81 lines
2.4 KiB
8 years ago
|
# ~*~ coding: utf-8 ~*~
|
||
|
from __future__ import unicode_literals
|
||
|
from rest_framework import serializers
|
||
6 years ago
|
from django.shortcuts import reverse
|
||
8 years ago
|
|
||
5 years ago
|
from ..models import Task, AdHoc, AdHocRunHistory, CommandExecution
|
||
8 years ago
|
|
||
|
|
||
|
class TaskSerializer(serializers.ModelSerializer):
|
||
|
class Meta:
|
||
7 years ago
|
model = Task
|
||
5 years ago
|
fields = [
|
||
|
'id', 'name', 'interval', 'crontab', 'is_periodic',
|
||
|
'is_deleted', 'comment', 'created_by', 'date_created',
|
||
|
'versions', 'is_success', 'timedelta', 'assets_amount',
|
||
|
'date_updated', 'history_summary',
|
||
|
]
|
||
8 years ago
|
|
||
|
|
||
7 years ago
|
class AdHocSerializer(serializers.ModelSerializer):
|
||
|
class Meta:
|
||
|
model = AdHoc
|
||
|
exclude = ('_tasks', '_options', '_hosts', '_become')
|
||
|
|
||
|
def get_field_names(self, declared_fields, info):
|
||
|
fields = super().get_field_names(declared_fields, info)
|
||
|
fields.extend(['tasks', 'options', 'hosts', 'become', 'short_id'])
|
||
|
return fields
|
||
|
|
||
|
|
||
|
class AdHocRunHistorySerializer(serializers.ModelSerializer):
|
||
|
task = serializers.SerializerMethodField()
|
||
|
adhoc_short_id = serializers.SerializerMethodField()
|
||
7 years ago
|
stat = serializers.SerializerMethodField()
|
||
7 years ago
|
|
||
|
class Meta:
|
||
|
model = AdHocRunHistory
|
||
|
exclude = ('_result', '_summary')
|
||
|
|
||
|
@staticmethod
|
||
|
def get_adhoc_short_id(obj):
|
||
|
return obj.adhoc.short_id
|
||
|
|
||
|
@staticmethod
|
||
|
def get_task(obj):
|
||
|
return obj.adhoc.task.id
|
||
|
|
||
7 years ago
|
@staticmethod
|
||
|
def get_stat(obj):
|
||
|
return {
|
||
6 years ago
|
"total": obj.adhoc.hosts.count(),
|
||
7 years ago
|
"success": len(obj.summary.get("contacted", [])),
|
||
|
"failed": len(obj.summary.get("dark", [])),
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
def get_field_names(self, declared_fields, info):
|
||
|
fields = super().get_field_names(declared_fields, info)
|
||
|
fields.extend(['summary', 'short_id'])
|
||
|
return fields
|
||
6 years ago
|
|
||
|
|
||
|
class CommandExecutionSerializer(serializers.ModelSerializer):
|
||
|
result = serializers.JSONField(read_only=True)
|
||
|
log_url = serializers.SerializerMethodField()
|
||
|
|
||
|
class Meta:
|
||
|
model = CommandExecution
|
||
|
fields = [
|
||
|
'id', 'hosts', 'run_as', 'command', 'result', 'log_url',
|
||
|
'is_finished', 'date_created', 'date_finished'
|
||
|
]
|
||
|
read_only_fields = [
|
||
6 years ago
|
'result', 'is_finished', 'log_url', 'date_created',
|
||
6 years ago
|
'date_finished'
|
||
|
]
|
||
|
|
||
|
@staticmethod
|
||
|
def get_log_url(obj):
|
||
|
return reverse('api-ops:celery-task-log', kwargs={'pk': obj.id})
|
||
5 years ago
|
|