feat: 优化api

pull/9225/head^2
Aaron3S 2022-12-21 17:14:07 +08:00
parent 5198ac1cc0
commit 0e534f3251
4 changed files with 17 additions and 8 deletions

View File

@ -15,6 +15,8 @@ class AdHocViewSet(OrgBulkModelViewSet):
permission_classes = () permission_classes = ()
model = AdHoc model = AdHoc
def allow_bulk_destroy(self, qs, filtered):
return True
def get_queryset(self): def get_queryset(self):
queryset = super().get_queryset() queryset = super().get_queryset()
return queryset.filter(creator=self.request.user) return queryset.filter(creator=self.request.user)

View File

@ -23,6 +23,9 @@ class JobViewSet(OrgBulkModelViewSet):
permission_classes = () permission_classes = ()
model = Job model = Job
def allow_bulk_destroy(self, qs, filtered):
return True
def get_queryset(self): def get_queryset(self):
queryset = super().get_queryset() queryset = super().get_queryset()
queryset = queryset.filter(creator=self.request.user) queryset = queryset.filter(creator=self.request.user)
@ -31,20 +34,21 @@ class JobViewSet(OrgBulkModelViewSet):
return queryset return queryset
def perform_create(self, serializer): def perform_create(self, serializer):
run_after_save = serializer.validated_data.pop('run_after_save', False)
instance = serializer.save() instance = serializer.save()
run_after_save = serializer.validated_data.get('run_after_save', False)
if instance.instant or run_after_save: if instance.instant or run_after_save:
self.run_job(instance, serializer) self.run_job(instance, serializer)
def perform_update(self, serializer): def perform_update(self, serializer):
run_after_save = serializer.validated_data.pop('run_after_save', False)
instance = serializer.save() instance = serializer.save()
run_after_save = serializer.validated_data.get('run_after_save', False)
if run_after_save: if run_after_save:
self.run_job(instance, serializer) self.run_job(instance, serializer)
@staticmethod def run_job(self, job, serializer):
def run_job(job, serializer):
execution = job.create_execution() execution = job.create_execution()
execution.creator = self.request.user
execution.save()
task = run_ops_job_execution.delay(execution.id) task = run_ops_job_execution.delay(execution.id)
set_task_to_serializer_data(serializer, task) set_task_to_serializer_data(serializer, task)
@ -58,6 +62,7 @@ class JobExecutionViewSet(OrgBulkModelViewSet):
def perform_create(self, serializer): def perform_create(self, serializer):
instance = serializer.save() instance = serializer.save()
instance.job_version = instance.job.version instance.job_version = instance.job.version
instance.creator = self.request.user
instance.save() instance.save()
task = run_ops_job_execution.delay(instance.id) task = run_ops_job_execution.delay(instance.id)
set_task_to_serializer_data(serializer, task) set_task_to_serializer_data(serializer, task)

View File

@ -21,6 +21,9 @@ class PlaybookViewSet(OrgBulkModelViewSet):
permission_classes = () permission_classes = ()
model = Playbook model = Playbook
def allow_bulk_destroy(self, qs, filtered):
return True
def get_queryset(self): def get_queryset(self):
queryset = super().get_queryset() queryset = super().get_queryset()
queryset = queryset.filter(creator=self.request.user) queryset = queryset.filter(creator=self.request.user)

View File

@ -9,12 +9,11 @@ from orgs.mixins.serializers import BulkOrgResourceModelSerializer
class JobSerializer(BulkOrgResourceModelSerializer, PeriodTaskSerializerMixin): class JobSerializer(BulkOrgResourceModelSerializer, PeriodTaskSerializerMixin):
creator = ReadableHiddenField(default=serializers.CurrentUserDefault()) creator = ReadableHiddenField(default=serializers.CurrentUserDefault())
run_after_save = serializers.BooleanField(label=_("Run after save"), read_only=True, default=False, required=False) run_after_save = serializers.BooleanField(label=_("Run after save"), default=False, required=False)
class Meta: class Meta:
model = Job model = Job
read_only_fields = ["id", "date_last_run", "date_created", "date_updated", "average_time_cost", read_only_fields = ["id", "date_last_run", "date_created", "date_updated", "average_time_cost"]
"run_after_save"]
fields = read_only_fields + [ fields = read_only_fields + [
"name", "instant", "type", "module", "args", "playbook", "assets", "runas_policy", "runas", "creator", "name", "instant", "type", "module", "args", "playbook", "assets", "runas_policy", "runas", "creator",
"use_parameter_define", "use_parameter_define",
@ -23,7 +22,7 @@ class JobSerializer(BulkOrgResourceModelSerializer, PeriodTaskSerializerMixin):
"chdir", "chdir",
"comment", "comment",
"summary", "summary",
"is_periodic", "interval", "crontab" "is_periodic", "interval", "crontab", "run_after_save"
] ]