From 19182b43e2c4f5719b7785ef94bd82c5bd033d7f Mon Sep 17 00:00:00 2001 From: Daniel Mursa Date: Wed, 6 Nov 2024 15:21:41 +0100 Subject: [PATCH] [#44] Add response_content_length in admin listview --- log_outgoing_requests/admin.py | 6 ++++++ log_outgoing_requests/models.py | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/log_outgoing_requests/admin.py b/log_outgoing_requests/admin.py index ffb33a7..7367022 100644 --- a/log_outgoing_requests/admin.py +++ b/log_outgoing_requests/admin.py @@ -25,6 +25,7 @@ class OutgoingRequestsLogAdmin(admin.ModelAdmin): "method", "response_ms", "timestamp", + "response_content_length", ) list_filter = ("method", "timestamp", "status_code", "hostname") search_fields = ("url", "params", "hostname") @@ -57,6 +58,7 @@ class OutgoingRequestsLogAdmin(admin.ModelAdmin): "res_headers", "res_content_type", "res_body_encoding", + "response_content_length", "response_body", ) }, @@ -76,6 +78,7 @@ class OutgoingRequestsLogAdmin(admin.ModelAdmin): "response_ms", "res_headers", "res_content_type", + "response_content_length", "response_body", "trace", ) @@ -100,6 +103,9 @@ def request_body(self, obj) -> str: def response_body(self, obj) -> str: return obj.response_body_decoded or "-" + def response_content_length(self, obj): + return obj.response_content_length or "-" + def truncated_url(self, obj): parsed_url = urlparse(obj.url) path = parsed_url.path diff --git a/log_outgoing_requests/models.py b/log_outgoing_requests/models.py index db8f1be..b5d8c19 100644 --- a/log_outgoing_requests/models.py +++ b/log_outgoing_requests/models.py @@ -174,6 +174,21 @@ def response_body_decoded(self) -> str: response_body_decoded.short_description = _("Response body") # type: ignore + @cached_property + def response_content_length(self) -> str: + """ + Get Response content length + + Try `Content-Length` header first. If not present, try to + determine the size by reading `len(body)`. + """ + content_length = loads_headers(self.res_headers).get("Content-Length", "") + if not content_length and self.response_body_decoded: + content_length = str(len(self.response_body_decoded)) + return content_length + + response_content_length.short_description = _("Content length") # type: ignore + def get_default_max_content_length(): """