a Django-based indieweb.org site https://00dani.me/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.9 KiB

import html
import json
from accept_types import get_best_match
from django.conf import settings
from django.http import HttpResponse, JsonResponse
from django.http import HttpResponseForbidden, HttpResponseBadRequest
from django.utils.html import strip_tags
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, urljoin
from .templatetags.markdown import markdown
cache = SimpleNamespace(package_json=None)
def load_package_json():
if cache.package_json:
return cache.package_json
with open(join(settings.BASE_DIR, 'package.json')) as f:
cache.package_json = json.load(f)
return cache.package_json
def origin(request):
return '{0}://{1}'.format(request.scheme, request.site.domain)
def uri(request):
return origin(request) + request.path
def form_encoded_response(content):
return HttpResponse(
REPS = {
'application/x-www-form-urlencoded': form_encoded_response,
'application/json': JsonResponse,
def choose_type(request, content, reps=REPS):
accept = request.META.get('HTTP_ACCEPT', '*/*')
type = get_best_match(accept, reps.keys())
if type:
return reps[type](content)
return HttpResponse(status=406)
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
def bad_req(message):
return HttpResponseBadRequest(message, content_type='text/plain')
def forbid(message):
return HttpResponseForbidden(message, content_type='text/plain')
def to_plain(md):
return html.unescape(strip_tags(markdown(md)))