From fa225093f4b6cfbd5d8cbb0a57bcdc57c4063dc3 Mon Sep 17 00:00:00 2001 From: vasileios Date: Fri, 28 Jul 2023 17:13:33 +0200 Subject: [PATCH] [#13] Add prettyfier based on content-type [#13] Fix admin.js [#13] Fixed admin button [#13] Remove unnecessary views and urls [#13] Remove message in quickstart.rst --- log_outgoing_requests/admin.py | 17 ++++++ .../static/log_outgoing_requests/js/admin.js | 53 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 log_outgoing_requests/static/log_outgoing_requests/js/admin.js diff --git a/log_outgoing_requests/admin.py b/log_outgoing_requests/admin.py index ffb33a7..b03efb8 100644 --- a/log_outgoing_requests/admin.py +++ b/log_outgoing_requests/admin.py @@ -2,6 +2,7 @@ from django import forms from django.contrib import admin +from django.utils.html import mark_safe from django.utils.translation import gettext as _ from solo.admin import SingletonModelAdmin @@ -57,6 +58,7 @@ class OutgoingRequestsLogAdmin(admin.ModelAdmin): "res_headers", "res_content_type", "res_body_encoding", + "prettify_body_response", "response_body", ) }, @@ -68,6 +70,7 @@ class OutgoingRequestsLogAdmin(admin.ModelAdmin): "timestamp", "method", "query_params", + "prettify_body_response", "params", "req_headers", "req_content_type", @@ -84,6 +87,7 @@ class Media: css = { "all": ("log_outgoing_requests/css/admin.css",), } + js = ("log_outgoing_requests/js/admin.js",) def has_add_permission(self, request): return False @@ -100,6 +104,19 @@ def request_body(self, obj) -> str: def response_body(self, obj) -> str: return obj.response_body_decoded or "-" + def prettify_body_response(self, obj): + body_response = "" + if "xml" in obj.res_content_type or "json" in obj.res_content_type: + body_response = mark_safe( + f""" + Prettify
+ + """ + ) + return body_response + + prettify_body_response.allow_tags = True + def truncated_url(self, obj): parsed_url = urlparse(obj.url) path = parsed_url.path diff --git a/log_outgoing_requests/static/log_outgoing_requests/js/admin.js b/log_outgoing_requests/static/log_outgoing_requests/js/admin.js new file mode 100644 index 0000000..0a73ec2 --- /dev/null +++ b/log_outgoing_requests/static/log_outgoing_requests/js/admin.js @@ -0,0 +1,53 @@ +document.addEventListener("DOMContentLoaded", function () { + const link = document.querySelector(".prettify-toggle-link"); + if (link) { + link.addEventListener("click", function (event) { + event.preventDefault(); + const textArea = document.querySelector(".prettify-output"); + const contentType = textArea.getAttribute("content-type"); + const responseBody = textArea.value; + if (textArea.style.display === "none") { + let outputValue = ''; + if (contentType.includes('json')) { + try { + outputValue = JSON.stringify(JSON.parse(responseBody), null, 3); + } catch (error) { + outputValue = "
Invalid JSON format"; + } + } else if (contentType.includes('xml')) { + try { + outputValue = prettifyXml(responseBody); + } catch (xmlError) { + outputValue = "
Invalid XML format"; + } + } + textArea.value = outputValue; + textArea.style.display = "inline"; + } else { + textArea.style.display = "none"; + } + }); + } +}); + +var prettifyXml = function(sourceXml) { + var xmlDoc = new DOMParser().parseFromString(sourceXml, 'application/xml'); + var xsltDoc = new DOMParser().parseFromString([ + '', + ' ', + ' ', + ' ', + ' ', + ' ', + ' ', + ' ', + '', + ].join('\n'), 'application/xml'); + + var xsltProcessor = new XSLTProcessor(); + xsltProcessor.importStylesheet(xsltDoc); + var resultDoc = xsltProcessor.transformToDocument(xmlDoc); + var resultXml = new XMLSerializer().serializeToString(resultDoc); + return resultXml; +}; +