Skip to content

Commit

Permalink
[#13] Add prettyfier based on content-type
Browse files Browse the repository at this point in the history
[#13] Fix admin.js

[#13] Fixed admin button

[#13] Remove unnecessary views and urls

[#13] Remove message in quickstart.rst

[#13] Black
  • Loading branch information
vaszig authored and danielmursa-dev committed Dec 12, 2024
1 parent 988e2c9 commit aca1430
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
17 changes: 17 additions & 0 deletions log_outgoing_requests/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -57,6 +58,7 @@ class OutgoingRequestsLogAdmin(admin.ModelAdmin):
"res_headers",
"res_content_type",
"res_body_encoding",
"prettify_body_response",
"response_body",
)
},
Expand All @@ -68,6 +70,7 @@ class OutgoingRequestsLogAdmin(admin.ModelAdmin):
"timestamp",
"method",
"query_params",
"prettify_body_response",
"params",
"req_headers",
"req_content_type",
Expand All @@ -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
Expand All @@ -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"""
<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>
"""
)
return body_response

prettify_body_response.allow_tags = True

def truncated_url(self, obj):
parsed_url = urlparse(obj.url)
path = parsed_url.path
Expand Down
53 changes: 53 additions & 0 deletions log_outgoing_requests/static/log_outgoing_requests/js/admin.js
Original file line number Diff line number Diff line change
@@ -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 = "<br><span style='color: red;'>Invalid JSON format</span>";
}
} else if (contentType.includes('xml')) {
try {
outputValue = prettifyXml(responseBody);
} catch (xmlError) {
outputValue = "<br><span style='color: red;'>Invalid XML format</span>";
}
}
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([
'<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">',
' <xsl:strip-space elements="*"/>',
' <xsl:template match="node()|@*">',
' <xsl:copy>',
' <xsl:apply-templates select="node()|@*"/>',
' </xsl:copy>',
' </xsl:template>',
' <xsl:output method="xml" indent="yes"/>',
'</xsl:stylesheet>',
].join('\n'), 'application/xml');

var xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsltDoc);
var resultDoc = xsltProcessor.transformToDocument(xmlDoc);
var resultXml = new XMLSerializer().serializeToString(resultDoc);
return resultXml;
};

0 comments on commit aca1430

Please sign in to comment.