Improve URL displays

This commit is contained in:
R. Miles McCain 2020-04-17 13:40:29 -04:00
parent 58a7bf5efd
commit 868cfd3cb9
No known key found for this signature in database
GPG Key ID: 91CB47BDDF2671A5
2 changed files with 6 additions and 5 deletions

View File

@ -98,7 +98,7 @@
<tbody> <tbody>
{% for referrer in stats.referrers %} {% for referrer in stats.referrers %}
<tr> <tr>
<td>{{referrer.referrer|default:"Direct"|urlizetrunc:"40"}}</td> <td>{{referrer.referrer|default:"Direct"|urldisplay}}</td>
<td class="rf">{{referrer.count|intcomma}}</td> <td class="rf">{{referrer.count|intcomma}}</td>
</tr> </tr>
{% endfor %} {% endfor %}

View File

@ -3,6 +3,7 @@ from urllib.parse import urlparse
import flag import flag
from django import template from django import template
from django.utils import timezone from django.utils import timezone
from django.utils.html import escape
from django.utils.safestring import SafeString from django.utils.safestring import SafeString
register = template.Library() register = template.Library()
@ -39,10 +40,10 @@ def startswith(text, starts):
@register.filter @register.filter
def urldisplay(url): def urldisplay(url):
try: if url.startswith("http"):
parsed = urlparse(url) display_url = url.replace("http://", "").replace("https://", "")
return SafeString( return SafeString(
f"<a href='{url}' title='{url}' rel='nofollow'>{parsed.path if len(parsed.path) < 32 else parsed.path[:32] + '&hellip;'}</a>" f"<a href='{url}' title='{url}' rel='nofollow'>{escape(display_url if len(display_url) < 40 else display_url[:40] + '...')}</a>"
) )
except: else:
return url return url