forked from 00dani/lemoncurry
Make shortlinks discoverable by displaying them on each entry, with extraneous bits trimmed out
This commit is contained in:
parent
65ff5f947a
commit
52106f1d3f
4 changed files with 37 additions and 3 deletions
10
lemoncurry/templatetags/friendly_url.py
Normal file
10
lemoncurry/templatetags/friendly_url.py
Normal 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, '', '', ''))
|
10
lemoncurry/templatetags/shortlink.py
Normal file
10
lemoncurry/templatetags/shortlink.py
Normal 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)
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue