Repository: maykinmedia/django-log-outgoing-requests (language: python)

Issue:
Cut off the URL admin list field after 200 chars.
To prevent the other columns moving out of view:

![image](https://github.com/maykinmedia/django-log-outgoing-requests/assets/96970/70909c82-80ae-4030-b123-4fbef328f10d)

Ideally, you can leave out the domain (since its already in the first column) and the URL path would be nicest if it looks like: `/some/very/very/very ... /very/long/path` (so elipses in the middle)




Discussion / hints:

Fix patch:
diff --git a/log_outgoing_requests/admin.py b/log_outgoing_requests/admin.py
index a9f3b48..e9921b2 100644
--- a/log_outgoing_requests/admin.py
+++ b/log_outgoing_requests/admin.py
@@ -1,3 +1,5 @@
+from urllib.parse import urlparse
+
 from django import forms
 from django.contrib import admin
 from django.utils.translation import gettext as _
@@ -12,7 +14,7 @@
 class OutgoingRequestsLogAdmin(admin.ModelAdmin):
     list_display = (
         "hostname",
-        "url",
+        "truncated_url",
         "params",
         "status_code",
         "method",
@@ -93,6 +95,20 @@ def request_body(self, obj) -> str:
     def response_body(self, obj) -> str:
         return obj.response_body_decoded or "-"
 
+    def truncated_url(self, obj):
+        parsed_url = urlparse(obj.url)
+        path = parsed_url.path
+        max_length = 200
+        path_length = len(path)
+
+        if path_length <= max_length:
+            return path
+
+        half_length = (max_length - 3) // 2
+        left_half = path[:half_length]
+        right_half = path[-half_length:]
+        return left_half + " \u2026 " + right_half
+
 
 class ConfigAdminForm(forms.ModelForm):
     class Meta:


Test patch:
diff --git a/tests/test_admin.py b/tests/test_admin.py
index 283b4d9..82ee05c 100644
--- a/tests/test_admin.py
+++ b/tests/test_admin.py
@@ -94,3 +94,40 @@ def test_admin_override_max_content_length(requests_mock, request_mock_kwargs):
     request_log = OutgoingRequestsLog.objects.last()
 
     assert request_log.res_body == b""
+
+
+@pytest.mark.django_db
+def test_list_url_is_trucated_over_200_chars(admin_client):
+    OutgoingRequestsLog.objects.create(
+        id=1,
+        url="https://example.com/a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t1u2v3w4x5y6z1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s1t2u3v4w5x6y7z/some-path-3894ndjidjd93djjd3eu9jjddu9eu93j3e39ei9idjd3ddksdj9393/some-path/some-path-as-well/skdlkdlskdksdkd9828393jdd",
+        timestamp=timezone.now(),
+    )
+    url = reverse("admin:log_outgoing_requests_outgoingrequestslog_changelist")
+
+    response = admin_client.get(url)
+    html = response.content.decode("utf-8")
+    doc = PyQuery(html)
+    truncated_url = doc.find(".field-truncated_url").text()
+
+    assert (
+        truncated_url
+        == "/a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t1u2v3w4x5y6z1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s1t2u3v4w \u2026 d93djjd3eu9jjddu9eu93j3e39ei9idjd3ddksdj9393/some-path/some-path-as-well/skdlkdlskdksdkd9828393jdd"
+    )
+
+
+@pytest.mark.django_db
+def test_list_url_is_not_trucated_under_200_chars(admin_client):
+    OutgoingRequestsLog.objects.create(
+        id=1,
+        url="https://example.com/a1b2c3d4e/some-path",
+        timestamp=timezone.now(),
+    )
+    url = reverse("admin:log_outgoing_requests_outgoingrequestslog_changelist")
+
+    response = admin_client.get(url)
+    html = response.content.decode("utf-8")
+    doc = PyQuery(html)
+    truncated_url = doc.find(".field-truncated_url").text()
+
+    assert truncated_url == "/a1b2c3d4e/some-path"