Skip to content

Commit

Permalink
fix(logs): also truncate user agent
Browse files Browse the repository at this point in the history
  • Loading branch information
alanzhu0 committed Jul 26, 2023
1 parent b5bfbb7 commit 19a2a30
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions intranet/apps/logs/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ def queryset(self, request, queryset):
return queryset


class TruncatedUserAgentFilter(admin.SimpleListFilter):
title = "user agent"
parameter_name = "user_agent"

def lookups(self, request, model_admin):
paths = model_admin.model.objects.order_by("user_agent").values_list("user_agent", flat=True).distinct()
truncated_paths = {path if len(path) < 40 else path[:40] + "..." for path in paths}
truncated_paths = sorted(truncated_paths)
return zip(truncated_paths, gettext_lazy(truncated_paths))

def queryset(self, request, queryset):
if self.value():
if self.value().endswith("..."):
return queryset.filter(user_agent__startswith=self.value()[:-3])
return queryset.filter(user_agent=self.value())
return queryset


class RequestAdmin(admin.ModelAdmin):
def truncated_path(self):
return self.path[:80] + "..." if len(self.path) > 80 else self.path # pylint: disable=no-member
Expand All @@ -49,10 +67,10 @@ def truncated_path(self):
"flag",
"timestamp",
"method",
TruncatedPathFilter,
"user",
"ip",
"user_agent",
TruncatedPathFilter,
TruncatedUserAgentFilter,
)
search_fields = (
"user__username",
Expand Down

0 comments on commit 19a2a30

Please sign in to comment.