From 787cdbcadf9a690c5367ac2ba361c4135a9d4a73 Mon Sep 17 00:00:00 2001 From: xinwen Date: Thu, 11 Jun 2020 20:30:59 +0800 Subject: [PATCH] [Fix] ops.models.adhoc (#4096) --- apps/ops/models/adhoc.py | 20 -------------------- apps/ops/serializers/adhoc.py | 10 ++-------- 2 files changed, 2 insertions(+), 28 deletions(-) diff --git a/apps/ops/models/adhoc.py b/apps/ops/models/adhoc.py index e8923df1b..013fdc628 100644 --- a/apps/ops/models/adhoc.py +++ b/apps/ops/models/adhoc.py @@ -118,26 +118,6 @@ 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 and obj.summary.get(key) - - if body: - asset, body = body.popitem() - if body: - action, body = body.popitem() - return asset, action, (body or '') and body.get('msg', '') - return asset, '', '' - return '', '', '' - def __str__(self): return self.name + '@' + str(self.org_id) diff --git a/apps/ops/serializers/adhoc.py b/apps/ops/serializers/adhoc.py index 0d284cb48..d4e67371a 100644 --- a/apps/ops/serializers/adhoc.py +++ b/apps/ops/serializers/adhoc.py @@ -60,14 +60,8 @@ 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:]) + last_success = serializers.ListField(source='latest_execution.success_hosts') + last_failure = serializers.DictField(source='latest_execution.failed_hosts') class Meta(TaskSerializer.Meta): fields = TaskSerializer.Meta.fields + ['last_success', 'last_failure']