Skip to content

Commit

Permalink
[#44] Add response_content_length in admin listview
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmursa-dev committed Nov 7, 2024
1 parent f4953c8 commit 19182b4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions log_outgoing_requests/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -57,6 +58,7 @@ class OutgoingRequestsLogAdmin(admin.ModelAdmin):
"res_headers",
"res_content_type",
"res_body_encoding",
"response_content_length",
"response_body",
)
},
Expand All @@ -76,6 +78,7 @@ class OutgoingRequestsLogAdmin(admin.ModelAdmin):
"response_ms",
"res_headers",
"res_content_type",
"response_content_length",
"response_body",
"trace",
)
Expand All @@ -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
Expand Down
15 changes: 15 additions & 0 deletions log_outgoing_requests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
"""
Expand Down

0 comments on commit 19182b4

Please sign in to comment.