2023-02-13 11:22:52 +00:00
|
|
|
import uuid
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-11-11 11:20:17 +00:00
|
|
|
from rest_framework import serializers
|
2022-12-26 11:02:05 +00:00
|
|
|
|
2023-12-05 03:16:34 +00:00
|
|
|
from assets.models import Asset
|
2023-01-16 11:02:09 +00:00
|
|
|
from common.serializers.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-15 09:25:21 +00:00
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
2022-11-11 11:20:17 +00:00
|
|
|
|
|
|
|
|
2023-12-19 10:42:44 +00:00
|
|
|
class JobSerializer(BulkOrgResourceModelSerializer, PeriodTaskSerializerMixin):
|
2022-12-06 03:03:14 +00:00
|
|
|
creator = ReadableHiddenField(default=serializers.CurrentUserDefault())
|
2024-02-23 09:22:47 +00:00
|
|
|
run_after_save = serializers.BooleanField(label=_("Execute after saving"), default=False, required=False)
|
2022-12-26 11:02:05 +00:00
|
|
|
nodes = serializers.ListField(required=False, child=serializers.CharField())
|
2023-01-16 11:02:09 +00:00
|
|
|
date_last_run = serializers.DateTimeField(label=_('Date last run'), read_only=True)
|
2023-02-13 11:22:52 +00:00
|
|
|
name = serializers.CharField(label=_('Name'), max_length=128, allow_blank=True, required=False)
|
2023-12-05 03:16:34 +00:00
|
|
|
assets = serializers.PrimaryKeyRelatedField(label=_('Assets'), queryset=Asset.objects, many=True, required=False)
|
2023-02-13 11:22:52 +00:00
|
|
|
|
|
|
|
def to_internal_value(self, data):
|
|
|
|
instant = data.get('instant', False)
|
2023-12-05 11:09:17 +00:00
|
|
|
job_type = data.get('type', '')
|
|
|
|
_uid = str(uuid.uuid4()).split('-')[-1]
|
2023-02-13 11:22:52 +00:00
|
|
|
if instant:
|
|
|
|
data['name'] = f'job-{_uid}'
|
2023-12-05 11:09:17 +00:00
|
|
|
if job_type == 'upload_file':
|
|
|
|
data['name'] = f'upload_file-{_uid}'
|
2023-02-13 11:22:52 +00:00
|
|
|
return super().to_internal_value(data)
|
2023-01-16 11:02:09 +00:00
|
|
|
|
|
|
|
def get_request_user(self):
|
|
|
|
request = self.context.get('request')
|
|
|
|
user = request.user if request else None
|
|
|
|
return user
|
2022-12-26 11:02:05 +00:00
|
|
|
|
2022-11-11 11:20:17 +00:00
|
|
|
class Meta:
|
|
|
|
model = Job
|
2023-01-16 11:02:09 +00:00
|
|
|
read_only_fields = [
|
2023-12-05 03:16:34 +00:00
|
|
|
"id", "date_last_run", "date_created",
|
|
|
|
"date_updated", "average_time_cost"
|
2023-01-16 11:02:09 +00:00
|
|
|
]
|
2022-11-18 10:46:48 +00:00
|
|
|
fields = read_only_fields + [
|
2023-01-16 11:02:09 +00:00
|
|
|
"name", "instant", "type", "module",
|
2023-12-19 10:42:44 +00:00
|
|
|
"args", "playbook", "assets",
|
2023-01-16 11:02:09 +00:00
|
|
|
"runas_policy", "runas", "creator",
|
|
|
|
"use_parameter_define", "parameters_define",
|
|
|
|
"timeout", "chdir", "comment", "summary",
|
|
|
|
"is_periodic", "interval", "crontab", "nodes",
|
2023-12-05 11:09:17 +00:00
|
|
|
"run_after_save"
|
2022-11-11 11:20:17 +00:00
|
|
|
]
|
2024-02-01 08:02:31 +00:00
|
|
|
extra_kwargs = {
|
2024-06-18 10:47:06 +00:00
|
|
|
'average_time_cost': {'label': _('Duration')},
|
2024-02-01 08:02:31 +00:00
|
|
|
}
|
2022-11-11 11:20:17 +00:00
|
|
|
|
|
|
|
|
2023-12-05 11:09:17 +00:00
|
|
|
class FileSerializer(serializers.Serializer):
|
2023-12-15 09:10:51 +00:00
|
|
|
files = serializers.FileField(allow_empty_file=False, max_length=128)
|
2023-12-05 11:09:17 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ref_name = "JobFileSerializer"
|
|
|
|
|
|
|
|
|
2024-02-19 06:47:32 +00:00
|
|
|
class JobTaskStopSerializer(serializers.Serializer):
|
|
|
|
task_id = serializers.CharField(max_length=128)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ref_name = "JobTaskStopSerializer"
|
|
|
|
|
|
|
|
|
2022-12-15 09:25:21 +00:00
|
|
|
class JobExecutionSerializer(BulkOrgResourceModelSerializer):
|
2022-12-06 03:03:14 +00:00
|
|
|
creator = ReadableHiddenField(default=serializers.CurrentUserDefault())
|
2022-12-06 11:43:32 +00:00
|
|
|
job_type = serializers.ReadOnlyField(label=_("Job type"))
|
2023-01-16 11:02:09 +00:00
|
|
|
material = serializers.ReadOnlyField(label=_("Command"))
|
|
|
|
is_success = serializers.ReadOnlyField(label=_("Is success"))
|
|
|
|
is_finished = serializers.ReadOnlyField(label=_("Is finished"))
|
|
|
|
time_cost = serializers.ReadOnlyField(label=_("Time cost"))
|
2022-12-06 03:03:14 +00:00
|
|
|
|
2022-11-11 11:20:17 +00:00
|
|
|
class Meta:
|
|
|
|
model = JobExecution
|
2023-01-16 11:02:09 +00:00
|
|
|
read_only_fields = ["id", "task_id", "timedelta", "time_cost",
|
|
|
|
'is_finished', 'date_start', 'date_finished',
|
2023-02-09 09:00:54 +00:00
|
|
|
'date_created', 'is_success', 'job_type',
|
2023-01-16 11:02:09 +00:00
|
|
|
'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
|
|
|
]
|
2024-03-19 02:34:35 +00:00
|
|
|
|
|
|
|
def validate_job(self, job_obj):
|
|
|
|
if job_obj.creator != self.context['request'].user:
|
|
|
|
raise serializers.ValidationError(_("You do not have permission for the current job."))
|
|
|
|
return job_obj
|