Format and color JSON in all views
This commit is contained in:
parent
9243a082b3
commit
bd16fefd08
4 changed files with 38 additions and 15 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
{% block title %}
|
{% block title %}
|
||||||
Games
|
Organizations
|
||||||
{% endblock title %}
|
{% endblock title %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h1 id="page-title">Organizations</h1>
|
<h1 id="page-title">Organizations</h1>
|
||||||
|
|
@ -13,6 +13,7 @@
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
{% else %}
|
{% else %}
|
||||||
<p>No games found.</p>
|
<p>No organizations found.</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
{{ orgs_data|safe }}
|
||||||
{% endblock content %}
|
{% endblock content %}
|
||||||
|
|
|
||||||
|
|
@ -35,5 +35,5 @@
|
||||||
</li>
|
</li>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
<pre><code id="org-data">{{ org_data }}</code></pre>
|
{{ org_data|safe }}
|
||||||
{% endblock content %}
|
{% endblock content %}
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ urlpatterns: list[URLPattern] = [
|
||||||
path("games/", views.GamesGridView.as_view(), name="game_list"),
|
path("games/", views.GamesGridView.as_view(), name="game_list"),
|
||||||
path("games/list/", views.GamesListView.as_view(), name="game_list_simple"),
|
path("games/list/", views.GamesListView.as_view(), name="game_list_simple"),
|
||||||
path("games/<str:twitch_id>/", views.GameDetailView.as_view(), name="game_detail"),
|
path("games/<str:twitch_id>/", views.GameDetailView.as_view(), name="game_detail"),
|
||||||
path("organizations/", views.OrgListView.as_view(), name="org_list"),
|
path("organizations/", views.org_list_view, name="org_list"),
|
||||||
path("organizations/<str:twitch_id>/", views.organization_detail_view, name="organization_detail"),
|
path("organizations/<str:twitch_id>/", views.organization_detail_view, name="organization_detail"),
|
||||||
path("channels/", views.ChannelListView.as_view(), name="channel_list"),
|
path("channels/", views.ChannelListView.as_view(), name="channel_list"),
|
||||||
path("channels/<str:twitch_id>/", views.ChannelDetailView.as_view(), name="channel_detail"),
|
path("channels/<str:twitch_id>/", views.ChannelDetailView.as_view(), name="channel_detail"),
|
||||||
|
|
|
||||||
|
|
@ -97,12 +97,36 @@ def search_view(request: HttpRequest) -> HttpResponse:
|
||||||
|
|
||||||
|
|
||||||
# MARK: /organizations/
|
# MARK: /organizations/
|
||||||
class OrgListView(ListView):
|
def org_list_view(request: HttpRequest) -> HttpResponse:
|
||||||
"""List view for organization."""
|
"""Function-based view for organization list.
|
||||||
|
|
||||||
model = Organization
|
Args:
|
||||||
template_name = "twitch/org_list.html"
|
request: The HTTP request.
|
||||||
context_object_name = "orgs"
|
|
||||||
|
Returns:
|
||||||
|
HttpResponse: The rendered organization list page.
|
||||||
|
"""
|
||||||
|
orgs: QuerySet[Organization] = Organization.objects.all().order_by("name")
|
||||||
|
|
||||||
|
# Serialize all organizations
|
||||||
|
serialized_orgs: str = serialize(
|
||||||
|
"json",
|
||||||
|
orgs,
|
||||||
|
fields=(
|
||||||
|
"twitch_id",
|
||||||
|
"name",
|
||||||
|
"added_at",
|
||||||
|
"updated_at",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
orgs_data: list[dict] = json.loads(serialized_orgs)
|
||||||
|
|
||||||
|
context: dict[str, Any] = {
|
||||||
|
"orgs": orgs,
|
||||||
|
"orgs_data": format_and_color_json(orgs_data),
|
||||||
|
}
|
||||||
|
|
||||||
|
return render(request, "twitch/org_list.html", context)
|
||||||
|
|
||||||
|
|
||||||
# MARK: /organizations/<twitch_id>/
|
# MARK: /organizations/<twitch_id>/
|
||||||
|
|
@ -156,12 +180,10 @@ def organization_detail_view(request: HttpRequest, twitch_id: str) -> HttpRespon
|
||||||
games_data: list[dict] = json.loads(serialized_games)
|
games_data: list[dict] = json.loads(serialized_games)
|
||||||
org_data[0]["fields"]["games"] = games_data
|
org_data[0]["fields"]["games"] = games_data
|
||||||
|
|
||||||
pretty_org_data: str = json.dumps(org_data[0], indent=4)
|
|
||||||
|
|
||||||
context: dict[str, Any] = {
|
context: dict[str, Any] = {
|
||||||
"organization": organization,
|
"organization": organization,
|
||||||
"games": games,
|
"games": games,
|
||||||
"org_data": pretty_org_data,
|
"org_data": format_and_color_json(org_data[0]),
|
||||||
}
|
}
|
||||||
|
|
||||||
return render(request, "twitch/organization_detail.html", context)
|
return render(request, "twitch/organization_detail.html", context)
|
||||||
|
|
@ -217,16 +239,16 @@ def drop_campaign_list_view(request: HttpRequest) -> HttpResponse:
|
||||||
return render(request, "twitch/campaign_list.html", context)
|
return render(request, "twitch/campaign_list.html", context)
|
||||||
|
|
||||||
|
|
||||||
def format_and_color_json(data: dict[str, Any] | str) -> str:
|
def format_and_color_json(data: dict[str, Any] | list[dict] | str) -> str:
|
||||||
"""Format and color a JSON string for HTML display.
|
"""Format and color a JSON string for HTML display.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
data: Either a dictionary or a JSON string to format.
|
data: Either a dictionary, list of dictionaries, or a JSON string to format.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
str: The formatted code with HTML styles.
|
str: The formatted code with HTML styles.
|
||||||
"""
|
"""
|
||||||
if isinstance(data, dict):
|
if isinstance(data, (dict, list)):
|
||||||
formatted_code: str = json.dumps(data, indent=4)
|
formatted_code: str = json.dumps(data, indent=4)
|
||||||
else:
|
else:
|
||||||
formatted_code = data
|
formatted_code = data
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue