-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#13] Added endpoints for prettify and original body in the admin
- Loading branch information
Showing
5 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
log_outgoing_requests/static/log_outgoing_requests/js/admin.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const getCsrfTokenFromDom = () => { | ||
return document.querySelector("[name=csrfmiddlewaretoken]").value; | ||
}; | ||
|
||
document.addEventListener("DOMContentLoaded", function () { | ||
const prettifyAnchors = document.querySelectorAll( | ||
".prettify-body-response" | ||
); | ||
const originalAnchors = document.querySelectorAll( | ||
".original-body-response" | ||
); | ||
const url = window.location.href; | ||
|
||
prettifyAnchors.forEach(function (element) { | ||
const responseBodyText = document.querySelector(".body-response-text"); | ||
|
||
element.addEventListener("click", function (event) { | ||
event.preventDefault(); | ||
|
||
fetch(element.href, { | ||
method: "POST", | ||
credentials: "same-origin", | ||
headers: { | ||
"X-CSRFToken": getCsrfTokenFromDom(), | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ url: url, text: element.textContent }), | ||
}) | ||
.then((response) => response.json()) | ||
.then((data) => { | ||
responseBodyText.textContent = data.newValue; | ||
}) | ||
.catch((error) => { | ||
console.error("Error:", error); | ||
}); | ||
}); | ||
}); | ||
|
||
originalAnchors.forEach(function (element) { | ||
const responseBodyText = document.querySelector(".body-response-text"); | ||
|
||
element.addEventListener("click", function (event) { | ||
event.preventDefault(); | ||
|
||
fetch(element.href, { | ||
method: "POST", | ||
credentials: "same-origin", | ||
headers: { | ||
"X-CSRFToken": getCsrfTokenFromDom(), | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ url: url, text: element.textContent }), | ||
}) | ||
.then((response) => response.json()) | ||
.then((data) => { | ||
responseBodyText.textContent = data.newValue; | ||
}) | ||
.catch((error) => { | ||
console.error("Error:", error); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from django.urls import path | ||
|
||
from .views import OriginalResponseBodyView, PrettifyResponseBodyView | ||
|
||
urlpatterns = [ | ||
path( | ||
"prettify/", | ||
PrettifyResponseBodyView.as_view(), | ||
name="prettify_view", | ||
), | ||
path( | ||
"original/", | ||
OriginalResponseBodyView.as_view(), | ||
name="original_view", | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import json | ||
|
||
from django.http import JsonResponse | ||
from django.views import View | ||
|
||
|
||
class PrettifyResponseBodyView(View): | ||
def post(self, request): | ||
received_data = json.loads(request.body) | ||
return JsonResponse({"newValue": "Toggled to pretty"}) | ||
|
||
|
||
class OriginalResponseBodyView(View): | ||
def post(self, request): | ||
received_data = json.loads(request.body) | ||
return JsonResponse({"newValue": "Toggled to original"}) |