Skip to content

Commit

Permalink
Log query strings, closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Aug 7, 2024
1 parent 70f2de8 commit bca7c4a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
20 changes: 18 additions & 2 deletions django_http_debug/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
sequence_re = re.compile(r"((?:\\x[0-9a-f]{2})+)")
octet_re = re.compile(r"(\\x[0-9a-f]{2})")

QUERY_STRING_TRUNCATE = 16


@admin.register(DebugEndpoint)
class DebugEndpointAdmin(admin.ModelAdmin):
Expand All @@ -17,25 +19,39 @@ class DebugEndpointAdmin(admin.ModelAdmin):

@admin.register(RequestLog)
class RequestLogAdmin(admin.ModelAdmin):
list_display = ("timestamp", "endpoint", "method", "body_preview", "is_base64")
list_display = (
"timestamp",
"endpoint",
"method",
"query_string_truncated",
"body_preview",
"is_base64",
)
list_filter = ("endpoint", "method", "is_base64")
readonly_fields = (
"endpoint",
"query_string",
"method",
"headers",
"body",
"body_display",
"is_base64",
"timestamp",
)
search_fields = ("endpoint__path",)

def has_add_permission(self, request):
return False

def has_change_permission(self, request, obj=None):
return False

def query_string_truncated(self, obj):
return obj.query_string[:QUERY_STRING_TRUNCATE] + (
"…" if len(obj.query_string) > QUERY_STRING_TRUNCATE else ""
)

query_string_truncated.short_description = "Query string"

def body_preview(self, obj):
body = obj.get_body()
if isinstance(body, bytes):
Expand Down
20 changes: 20 additions & 0 deletions django_http_debug/migrations/0003_requestlog_query_string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 5.0.3 on 2024-08-07 20:05

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
(
"django_http_debug",
"0002_debugendpoint_content_type_debugendpoint_is_base64",
),
]

operations = [
migrations.AddField(
model_name="requestlog",
name="query_string",
field=models.CharField(blank=True, max_length=255),
),
]
1 change: 1 addition & 0 deletions django_http_debug/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def get_absolute_url(self):
class RequestLog(models.Model):
endpoint = models.ForeignKey(DebugEndpoint, on_delete=models.CASCADE)
method = models.CharField(max_length=10)
query_string = models.CharField(max_length=255, blank=True)
headers = models.JSONField()
body = models.TextField(blank=True)
is_base64 = models.BooleanField(default=False)
Expand Down
1 change: 1 addition & 0 deletions django_http_debug/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def debug_view(request, path):
log_entry = RequestLog(
endpoint=endpoint,
method=request.method,
query_string=request.META.get("QUERY_STRING", ""),
headers=dict(request.headers),
)
log_entry.set_body(request.body)
Expand Down

0 comments on commit bca7c4a

Please sign in to comment.