Make shortlinks discoverable by displaying them on each entry, with extraneous bits trimmed out

This commit is contained in:
Danielle McLean 2017-10-30 14:58:50 +11:00
parent 65ff5f947a
commit 52106f1d3f
Signed by untrusted user: 00dani
GPG key ID: 5A5D2D1AFF12EEC5
4 changed files with 37 additions and 3 deletions

View file

@ -0,0 +1,10 @@
from django import template
from urllib.parse import urlunparse, urlparse
register = template.Library()
@register.filter
def friendly_url(url):
(scheme, netloc, path, params, q, fragment) = urlparse(url)
return urlunparse(('', netloc, path, '', '', ''))

View file

@ -0,0 +1,10 @@
from django import template
from django.template import Context
from lemoncurry import utils
register = template.Library()
@register.simple_tag
def shortlink(obj):
return utils.shortlink(obj)

View file

@ -3,8 +3,10 @@ from accept_types import get_best_match
from django.conf import settings
from django.http import HttpResponse
from os.path import join
from shorturls import default_converter as converter
from shorturls.templatetags.shorturl import ShortURL
from types import SimpleNamespace
from urllib.parse import urlencode
from urllib.parse import urlencode, urljoin
cache = SimpleNamespace(package_json=None)
@ -38,3 +40,11 @@ def form_encoded_response(content):
urlencode(content),
content_type='application/x-www-form-urlencoded'
)
def shortlink(obj):
prefix = ShortURL(None).get_prefix(obj)
tinyid = converter.from_decimal(obj.pk)
if hasattr(settings, 'SHORT_BASE_URL') and settings.SHORT_BASE_URL:
return urljoin(settings.SHORT_BASE_URL, prefix + tinyid)
return '/' + prefix + tinyid