From 9efa7b84dfbb0e4654f440f6b8cdc48b79789343 Mon Sep 17 00:00:00 2001 From: Jeff Bradberry Date: Thu, 10 Oct 2019 16:08:17 -0400 Subject: [PATCH] Depend on a serializer context variable `no_truncate` to decide whether to turn off the ANSI control sequence-aware truncation, instead of needing inappropriate awareness of the details of the view that invoked the serializer. This will also allow us to have views that can more flexibly turn off the truncation under other circumstances. --- awx/api/serializers.py | 10 +++++----- awx/api/views/__init__.py | 10 ++++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/awx/api/serializers.py b/awx/api/serializers.py index a5bdad1c39..71525ccf0c 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -3852,12 +3852,12 @@ class JobEventSerializer(BaseSerializer): def to_representation(self, obj): data = super(JobEventSerializer, self).to_representation(obj) - # Show full stdout for event detail view, truncate only for list view. - if hasattr(self.context.get('view', None), 'retrieve'): - return data # Show full stdout for playbook_on_* events. if obj and obj.event.startswith('playbook_on'): return data + # If the view logic says to not trunctate (request was to the detail view or a param was used) + if self.context.get('no_truncate', False): + return data max_bytes = settings.EVENT_STDOUT_MAX_BYTES_DISPLAY if 'stdout' in data: data['stdout'] = truncate_stdout(data['stdout'], max_bytes) @@ -3957,8 +3957,8 @@ class AdHocCommandEventSerializer(BaseSerializer): def to_representation(self, obj): data = super(AdHocCommandEventSerializer, self).to_representation(obj) - # Show full stdout for event detail view, truncate only for list view. - if hasattr(self.context.get('view', None), 'retrieve'): + # If the view logic says to not trunctate (request was to the detail view or a param was used) + if self.context.get('no_truncate', False): return data max_bytes = settings.EVENT_STDOUT_MAX_BYTES_DISPLAY if 'stdout' in data: diff --git a/awx/api/views/__init__.py b/awx/api/views/__init__.py index 383c7aeee9..646ff2c746 100644 --- a/awx/api/views/__init__.py +++ b/awx/api/views/__init__.py @@ -3774,6 +3774,11 @@ class JobEventDetail(RetrieveAPIView): model = models.JobEvent serializer_class = serializers.JobEventSerializer + def get_serializer_context(self): + context = super().get_serializer_context() + context.update(no_truncate=True) + return context + class JobEventChildrenList(SubListAPIView): @@ -4008,6 +4013,11 @@ class AdHocCommandEventDetail(RetrieveAPIView): model = models.AdHocCommandEvent serializer_class = serializers.AdHocCommandEventSerializer + def get_serializer_context(self): + context = super().get_serializer_context() + context.update(no_truncate=True) + return context + class BaseAdHocCommandEventsList(SubListAPIView):