jumpserver/apps/ops/serializers/job.py

55 lines
2.3 KiB
Python
Raw Normal View History

2022-12-02 04:21:56 +00:00
from django.utils.translation import ugettext as _
2022-11-11 11:20:17 +00:00
from rest_framework import serializers
2022-12-26 11:02:05 +00:00
from assets.models import Node
2022-11-11 11:20:17 +00:00
from common.drf.fields import ReadableHiddenField
2022-11-18 10:46:48 +00:00
from ops.mixin import PeriodTaskSerializerMixin
2022-11-11 11:20:17 +00:00
from ops.models import Job, JobExecution
2022-12-16 07:52:02 +00:00
from ops.models.job import JobAuditLog
2022-12-15 09:25:21 +00:00
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
2022-11-11 11:20:17 +00:00
2022-12-15 09:25:21 +00:00
class JobSerializer(BulkOrgResourceModelSerializer, PeriodTaskSerializerMixin):
creator = ReadableHiddenField(default=serializers.CurrentUserDefault())
2022-12-21 09:14:07 +00:00
run_after_save = serializers.BooleanField(label=_("Run after save"), default=False, required=False)
2022-12-26 11:02:05 +00:00
nodes = serializers.ListField(required=False, child=serializers.CharField())
def create(self, validated_data):
assets = validated_data.__getitem__('assets')
node_ids = validated_data.pop('nodes')
if node_ids:
nodes = Node.objects.filter(id__in=node_ids)
assets.extend(
Node.get_nodes_all_assets(*nodes).exclude(id__in=[asset.id for asset in assets]))
return super().create(validated_data)
2022-11-11 11:20:17 +00:00
class Meta:
model = Job
2022-12-21 09:14:07 +00:00
read_only_fields = ["id", "date_last_run", "date_created", "date_updated", "average_time_cost"]
2022-11-18 10:46:48 +00:00
fields = read_only_fields + [
"name", "instant", "type", "module", "args", "playbook", "assets", "runas_policy", "runas", "creator",
"use_parameter_define",
2022-11-17 12:10:13 +00:00
"parameters_define",
"timeout",
"chdir",
"comment",
2022-11-18 10:46:48 +00:00
"summary",
2022-12-26 11:02:05 +00:00
"is_periodic", "interval", "crontab", "run_after_save", "nodes"
2022-11-11 11:20:17 +00:00
]
2022-12-15 09:25:21 +00:00
class JobExecutionSerializer(BulkOrgResourceModelSerializer):
creator = ReadableHiddenField(default=serializers.CurrentUserDefault())
job_type = serializers.ReadOnlyField(label=_("Job type"))
2022-12-20 08:36:22 +00:00
material = serializers.ReadOnlyField(label=_("Material"))
2022-11-11 11:20:17 +00:00
class Meta:
model = JobExecution
2022-12-20 08:36:22 +00:00
read_only_fields = ["id", "task_id", "timedelta", "time_cost", 'is_finished', 'date_start',
'date_finished',
'date_created',
2022-12-20 08:36:22 +00:00
'is_success', 'task_id', 'short_id', 'job_type', 'summary', 'material']
2022-11-11 11:20:17 +00:00
fields = read_only_fields + [
2022-12-16 10:39:59 +00:00
"job", "parameters", "creator"
2022-11-11 11:20:17 +00:00
]