Skip to content

Commit

Permalink
[#13] Hide prettify_body_response field in admin
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmursa-dev committed Jan 10, 2025
1 parent 0c427d6 commit b4d94e3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 39 deletions.
83 changes: 44 additions & 39 deletions log_outgoing_requests/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,40 +31,6 @@ class OutgoingRequestsLogAdmin(admin.ModelAdmin):
search_fields = ("url", "params", "hostname")
date_hierarchy = "timestamp"
show_full_result_count = False

fieldsets = (
(
_("Request"),
{
"fields": (
"method",
"url",
"timestamp",
"query_params",
"params",
"req_headers",
"req_content_type",
"req_body_encoding",
"request_body",
)
},
),
(
_("Response"),
{
"fields": (
"status_code",
"response_ms",
"res_headers",
"res_content_type",
"res_body_encoding",
"prettify_body_response",
"response_body",
)
},
),
(_("Extra"), {"fields": ("trace",)}),
)
readonly_fields = (
"url",
"timestamp",
Expand All @@ -83,6 +49,45 @@ class OutgoingRequestsLogAdmin(admin.ModelAdmin):
"trace",
)

def get_fieldsets(self, request, obj=None):
fieldsets = [
(
_("Request"),
{
"fields": (
"method",
"url",
"timestamp",
"query_params",
"params",
"req_headers",
"req_content_type",
"req_body_encoding",
"request_body",
)
},
),
(
_("Response"),
{
"fields": (
"status_code",
"response_ms",
"res_headers",
"res_content_type",
"res_body_encoding",
"response_body",
)
},
),
(_("Extra"), {"fields": ("trace",)}),
]

if obj and obj.supports_xml_or_json:
fieldsets[1][1]["fields"] += ("prettify_body_response",)

return fieldsets

class Media:
css = {
"all": ("log_outgoing_requests/css/admin.css",),
Expand All @@ -106,12 +111,12 @@ def response_body(self, obj) -> str:

def prettify_body_response(self, obj):
body_response = ""
if "xml" in obj.res_content_type or "json" in obj.res_content_type:
if obj.supports_xml_or_json:
body_response = mark_safe(
f"""
<a href="#" class="prettify-toggle-link">Prettify</a><br>
<textarea readonly class="prettify-output" style="display:none;" rows="15" cols="60" content-type='{obj.res_content_type}'>{obj.response_body_decoded}</textarea>
"""
'<a href="#" class="prettify-toggle-link">Prettify</a><br>\n'
'<textarea readonly class="prettify-output" style="display:none;"\n'
f" rows='15' cols='60' content-type='{obj.res_content_type}'>\n"
f"{obj.response_body_decoded}</textarea>"
)
return body_response

Expand Down
4 changes: 4 additions & 0 deletions log_outgoing_requests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ def __str__(self):
def url_parsed(self):
return urlparse(self.url)

@cached_property
def supports_xml_or_json(self):
return "xml" in self.res_content_type or "json" in self.res_content_type

@property
def query_params(self):
return self.url_parsed.query
Expand Down

0 comments on commit b4d94e3

Please sign in to comment.