Skip to content

Commit

Permalink
Add syntax highlighted JSON field output in webhook admin (#327)
Browse files Browse the repository at this point in the history
* Add syntax highlighted JSON field output in webhook admin

* Update base image

---------

Co-authored-by: meanderfox <[email protected]>
  • Loading branch information
rechner and meanderfox committed Aug 27, 2024
1 parent d9f61ef commit b761574
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .isort.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[settings]
known_third_party = configobj,django,django_u2f,freezegun,idempotency_key,import_export,influxdb,jwt,nested_inline,paho,pytz,qrcode,square
known_third_party = configobj,django,django_u2f,freezegun,idempotency_key,import_export,influxdb,jwt,nested_inline,paho,pygments,pytz,qrcode,square
multi_line_output = 3
#forced_separate=django.contrib,django.utils
include_trailing_comma = True
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ghcr.io/furthemore/apis:apis-base-35aadf5
FROM ghcr.io/furthemore/apis:apis-base-cb35d5b

LABEL org.opencontainers.image.source="https://github.com/furthemore/APIS"

Expand Down
32 changes: 31 additions & 1 deletion registration/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
from django.shortcuts import render
from django.urls import reverse
from django.utils.html import format_html, urlencode
from django.utils.safestring import mark_safe
from import_export import fields, resources
from import_export.admin import ImportExportModelAdmin
from nested_inline.admin import NestedModelAdmin, NestedTabularInline
from pygments import highlight
from pygments.formatters.html import HtmlFormatter
from pygments.lexers.data import JsonLexer
from qrcode.image.svg import SvgPathFillImage

import registration.emails
Expand Down Expand Up @@ -1564,11 +1568,37 @@ def format_value(self, value):
return super(PrettyJSONWidget, self).format_value(value)


def json_highlight_format_value(value):
doc = json.dumps(value, indent=2, sort_keys=True)
# Get the Pygments formatter
formatter = HtmlFormatter(style="default")

# Highlight the data
response = highlight(doc, JsonLexer(), formatter)

# Get the stylesheet
style = "<style>" + formatter.get_style_defs() + "</style><br>"

# Safe the output
return mark_safe(style + response)


class PaymentWebhookAdmin(admin.ModelAdmin):
list_display = ("event_id", "event_type", "timestamp", "integration", "processed")
list_filter = ("event_type", "processed")
search_fields = ["event_id"]
formfield_overrides = {JSONField: {"widget": PrettyJSONWidget}}
readonly_fields = ("processed", "body_highlighted", "headers_highlighted")
exclude = ("body", "headers")

def body_highlighted(self, instance):
return json_highlight_format_value(instance.body)

body_highlighted.short_description = "Body"

def headers_highlighted(self, instance):
return json_highlight_format_value(instance.headers)

body_highlighted.short_description = "Headers"


admin.site.register(PaymentWebhookNotification, PaymentWebhookAdmin)
3 changes: 3 additions & 0 deletions registration/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,9 @@ class PaymentWebhookNotification(models.Model):
body = models.JSONField("Webhook body")
headers = models.JSONField("Webhook headers")

def __str__(self):
return f"{self.integration} {self.event_type} {self.event_id}"


class OrderItem(models.Model):
order = models.ForeignKey(Order, null=True, on_delete=models.CASCADE)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ sentry-sdk
squareup==22.0.0.20220921
qrcode~=7.4.2
pytz~=2024.1
Pygments~=2.18.0

0 comments on commit b761574

Please sign in to comment.