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: 00dani
GPG key ID: 5A5D2D1AFF12EEC5
4 changed files with 37 additions and 3 deletions

View file

@ -1,4 +1,4 @@
{% load humanize markdown %}<article class="card h-entry"> {% load friendly_url humanize markdown shortlink %}<article class="card h-entry">
{% if entry.photo %}<img class="card-img-top u-photo" src="{{ entry.photo.url }}" />{% endif %} {% if entry.photo %}<img class="card-img-top u-photo" src="{{ entry.photo.url }}" />{% endif %}
<div class="card-body"> <div class="card-body">
{% if entry.name %}<h4 class="card-title p-name">{{ entry.name }}</h4>{% endif %} {% if entry.name %}<h4 class="card-title p-name">{{ entry.name }}</h4>{% endif %}
@ -9,12 +9,16 @@
<img class="u-photo" src="{{ entry.author.avatar.url }}" /> <img class="u-photo" src="{{ entry.author.avatar.url }}" />
{{ entry.author.first_name }} {{ entry.author.last_name }} {{ entry.author.first_name }} {{ entry.author.last_name }}
</a> </a>
<a class="u-url" href="{{ entry.url }}"> <a class="u-uid u-url" href="{{ entry.url }}">
<time class="dt-published" datetime="{{ entry.published.isoformat }}"> <time class="dt-published" datetime="{{ entry.published.isoformat }}">
<i class="fa fa-calendar"></i> <i class="fa fa-calendar"></i>
{{ entry.published | naturaltime }} {{ entry.published | naturaltime }}
</time> </time>
</a> </a>
{% shortlink entry as short %}<a class="u-url" href="{{ short }}">
<i class="fa fa-link"></i>
{{ short | friendly_url }}
</a>
{% if entry.updated != entry.published %} {% if entry.updated != entry.published %}
<time class="dt-updated" datetime="{{ entry.updated.isoformat }}"> <time class="dt-updated" datetime="{{ entry.updated.isoformat }}">
<i class="fa fa-pencil"></i> <i class="fa fa-pencil"></i>

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.conf import settings
from django.http import HttpResponse from django.http import HttpResponse
from os.path import join from os.path import join
from shorturls import default_converter as converter
from shorturls.templatetags.shorturl import ShortURL
from types import SimpleNamespace from types import SimpleNamespace
from urllib.parse import urlencode from urllib.parse import urlencode, urljoin
cache = SimpleNamespace(package_json=None) cache = SimpleNamespace(package_json=None)
@ -38,3 +40,11 @@ def form_encoded_response(content):
urlencode(content), urlencode(content),
content_type='application/x-www-form-urlencoded' 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