2022-12-24 10:05:07 -08:00
|
|
|
import json
|
2022-12-18 10:00:21 -08:00
|
|
|
|
|
|
|
import httpx
|
|
|
|
from asgiref.sync import async_to_sync
|
|
|
|
from django import forms
|
|
|
|
from django.utils.decorators import method_decorator
|
2022-12-22 13:06:35 -08:00
|
|
|
from django.views.generic import FormView, TemplateView
|
2022-12-18 10:00:21 -08:00
|
|
|
|
|
|
|
from core.ld import canonicalise
|
|
|
|
from users.decorators import admin_required
|
|
|
|
from users.models import SystemActor
|
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(admin_required, name="dispatch")
|
|
|
|
class JsonViewer(FormView):
|
|
|
|
|
|
|
|
template_name = "activities/debug_json.html"
|
|
|
|
|
|
|
|
class form_class(forms.Form):
|
|
|
|
uri = forms.CharField(
|
|
|
|
help_text="The URI to fetch and show",
|
|
|
|
widget=forms.TextInput(attrs={"type": "search", "autofocus": "autofocus"}),
|
|
|
|
)
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
2022-12-20 05:21:36 -08:00
|
|
|
uri = form.cleaned_data["uri"]
|
|
|
|
if "://" not in uri:
|
|
|
|
uri = "https://" + uri
|
2022-12-24 10:05:07 -08:00
|
|
|
|
|
|
|
# Render results
|
|
|
|
context = self.get_context_data(form=form)
|
|
|
|
|
2022-12-18 10:00:21 -08:00
|
|
|
try:
|
|
|
|
response = async_to_sync(SystemActor().signed_request)(
|
|
|
|
method="get",
|
2022-12-20 05:21:36 -08:00
|
|
|
uri=uri,
|
2022-12-18 10:00:21 -08:00
|
|
|
)
|
2022-12-24 10:05:07 -08:00
|
|
|
except httpx.RequestError as ex:
|
|
|
|
result = f"Request Error: {str(ex)}"
|
2022-12-18 10:00:21 -08:00
|
|
|
else:
|
2022-12-24 10:05:07 -08:00
|
|
|
context.update(
|
|
|
|
{
|
|
|
|
"status_code": response.status_code,
|
|
|
|
"content_type": response.headers["content-type"],
|
|
|
|
"num_bytes_downloaded": response.num_bytes_downloaded,
|
|
|
|
"charset_encoding": response.charset_encoding,
|
|
|
|
"raw_result": response.text,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-12-18 10:00:21 -08:00
|
|
|
if response.status_code >= 400:
|
|
|
|
result = f"Error response: {response.status_code}\n{response.content}"
|
|
|
|
else:
|
2022-12-24 10:05:07 -08:00
|
|
|
try:
|
|
|
|
document = canonicalise(response.json(), include_security=True)
|
|
|
|
except json.JSONDecodeError as ex:
|
|
|
|
result = str(ex)
|
|
|
|
else:
|
|
|
|
result = json.dumps(document, indent=4, sort_keys=True)
|
|
|
|
# result = pprint.pformat(document)
|
2022-12-18 10:00:21 -08:00
|
|
|
context["result"] = result
|
|
|
|
return self.render_to_response(context)
|
2022-12-22 13:06:35 -08:00
|
|
|
|
|
|
|
|
|
|
|
class NotFound(TemplateView):
|
|
|
|
|
|
|
|
template_name = "404.html"
|
|
|
|
|
|
|
|
|
|
|
|
class ServerError(TemplateView):
|
|
|
|
|
|
|
|
template_name = "500.html"
|
2022-12-23 21:34:21 -08:00
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(admin_required, name="dispatch")
|
|
|
|
class OauthAuthorize(TemplateView):
|
|
|
|
|
|
|
|
template_name = "api/oauth_authorize.html"
|
|
|
|
|
|
|
|
def get_context_data(self):
|
|
|
|
return {
|
|
|
|
"application": {"name": "Fake Application", "client_id": "fake"},
|
|
|
|
"redirect_uri": "",
|
|
|
|
"scope": "read write push",
|
|
|
|
"identities": self.request.user.identities.all(),
|
|
|
|
"code": "12345abcde",
|
|
|
|
}
|