Debug JSON view, json fixes

This commit is contained in:
Andrew Godwin 2022-12-18 11:00:21 -07:00
parent 770aa1531a
commit 58e11ae3b6
4 changed files with 70 additions and 4 deletions

View File

@ -775,8 +775,8 @@ class Post(StatorModel):
# These have no IDs, so we have to wipe them each time # These have no IDs, so we have to wipe them each time
post.attachments.all().delete() post.attachments.all().delete()
for attachment in get_list(data, "attachment"): for attachment in get_list(data, "attachment"):
if "toot:focalPoint" in attachment: if "focalPoint" in attachment:
focal_x, focal_y = attachment["toot:focalPoint"]["@list"] focal_x, focal_y = attachment["focalPoint"]["@list"]
else: else:
focal_x, focal_y = None, None focal_x, focal_y = None, None
post.attachments.create( post.attachments.create(
@ -785,7 +785,7 @@ class Post(StatorModel):
name=attachment.get("name"), name=attachment.get("name"),
width=attachment.get("width"), width=attachment.get("width"),
height=attachment.get("height"), height=attachment.get("height"),
blurhash=attachment.get("toot:blurhash"), blurhash=attachment.get("blurhash"),
focal_x=focal_x, focal_x=focal_x,
focal_y=focal_y, focal_y=focal_y,
) )

45
activities/views/debug.py Normal file
View File

@ -0,0 +1,45 @@
import pprint
import httpx
from asgiref.sync import async_to_sync
from django import forms
from django.utils.decorators import method_decorator
from django.views.generic import FormView
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):
try:
response = async_to_sync(SystemActor().signed_request)(
method="get",
uri=form.cleaned_data["uri"],
)
except httpx.RequestError:
result = "Request Error"
else:
if response.status_code >= 400:
result = f"Error response: {response.status_code}\n{response.content}"
else:
document = canonicalise(response.json(), include_security=True)
result = pprint.pformat(document)
# Render results
context = self.get_context_data(form=form)
context["result"] = result
return self.render_to_response(context)
# https://pixelfed.social/p/wakest/502370273028530995

View File

@ -2,7 +2,7 @@ from django.conf import settings as djsettings
from django.contrib import admin as djadmin from django.contrib import admin as djadmin
from django.urls import path, re_path from django.urls import path, re_path
from activities.views import compose, explore, follows, posts, search, timelines from activities.views import compose, debug, explore, follows, posts, search, timelines
from api.views import api_router, oauth from api.views import api_router, oauth
from core import views as core from core import views as core
from mediaproxy import views as mediaproxy from mediaproxy import views as mediaproxy
@ -17,6 +17,7 @@ urlpatterns = [
path("local/", timelines.Local.as_view(), name="local"), path("local/", timelines.Local.as_view(), name="local"),
path("federated/", timelines.Federated.as_view(), name="federated"), path("federated/", timelines.Federated.as_view(), name="federated"),
path("search/", search.Search.as_view(), name="search"), path("search/", search.Search.as_view(), name="search"),
path("debug/json/", debug.JsonViewer.as_view(), name="debug_json"),
path("tags/<hashtag>/", timelines.Tag.as_view(), name="tag"), path("tags/<hashtag>/", timelines.Tag.as_view(), name="tag"),
path("explore/", explore.Explore.as_view(), name="explore"), path("explore/", explore.Explore.as_view(), name="explore"),
path("explore/tags/", explore.ExploreTag.as_view(), name="explore-tag"), path("explore/tags/", explore.ExploreTag.as_view(), name="explore-tag"),

View File

@ -0,0 +1,20 @@
{% extends "base.html" %}
{% block title %}Debug JSON{% endblock %}
{% block body_class %}no-sidebar{% endblock %}
{% block content %}
<form action="." method="POST">
{% csrf_token %}
<fieldset>
{% include "forms/_field.html" with field=form.uri %}
</fieldset>
<div class="buttons">
<button>Debug</button>
</div>
</form>
{% if result %}
<pre>{{ result }}</pre>
{% endif %}
{% endblock %}