From 868cfd3cb9944373b3ecac74cc5d12e1014202a8 Mon Sep 17 00:00:00 2001 From: "R. Miles McCain" Date: Fri, 17 Apr 2020 13:40:29 -0400 Subject: [PATCH] Improve URL displays --- shynet/dashboard/templates/dashboard/pages/service.html | 2 +- shynet/dashboard/templatetags/helpers.py | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/shynet/dashboard/templates/dashboard/pages/service.html b/shynet/dashboard/templates/dashboard/pages/service.html index b23609a..ef46816 100644 --- a/shynet/dashboard/templates/dashboard/pages/service.html +++ b/shynet/dashboard/templates/dashboard/pages/service.html @@ -98,7 +98,7 @@ {% for referrer in stats.referrers %} - {{referrer.referrer|default:"Direct"|urlizetrunc:"40"}} + {{referrer.referrer|default:"Direct"|urldisplay}} {{referrer.count|intcomma}} {% endfor %} diff --git a/shynet/dashboard/templatetags/helpers.py b/shynet/dashboard/templatetags/helpers.py index 06503b4..8542c97 100644 --- a/shynet/dashboard/templatetags/helpers.py +++ b/shynet/dashboard/templatetags/helpers.py @@ -3,6 +3,7 @@ from urllib.parse import urlparse import flag from django import template from django.utils import timezone +from django.utils.html import escape from django.utils.safestring import SafeString register = template.Library() @@ -39,10 +40,10 @@ def startswith(text, starts): @register.filter def urldisplay(url): - try: - parsed = urlparse(url) + if url.startswith("http"): + display_url = url.replace("http://", "").replace("https://", "") return SafeString( - f"{parsed.path if len(parsed.path) < 32 else parsed.path[:32] + '…'}" + f"{escape(display_url if len(display_url) < 40 else display_url[:40] + '...')}" ) - except: + else: return url