2017-12-10 21:30:46 -05:00
|
|
|
import html
|
2017-10-26 22:24:57 -04:00
|
|
|
import json
|
2017-10-29 02:07:36 -04:00
|
|
|
from accept_types import get_best_match
|
2017-10-26 22:24:57 -04:00
|
|
|
from django.conf import settings
|
2017-11-03 02:18:00 -04:00
|
|
|
from django.http import HttpResponse, JsonResponse
|
2017-11-02 01:16:04 -04:00
|
|
|
from django.http import HttpResponseForbidden, HttpResponseBadRequest
|
2017-12-10 21:30:46 -05:00
|
|
|
from django.utils.html import strip_tags
|
2017-10-26 22:24:57 -04:00
|
|
|
from os.path import join
|
2018-06-21 22:31:03 -04:00
|
|
|
from typing import Any, Dict, Optional
|
2018-06-19 01:47:10 -04:00
|
|
|
from urllib.parse import urlencode, urljoin, urlparse
|
2017-10-26 22:24:57 -04:00
|
|
|
|
2017-12-10 21:30:46 -05:00
|
|
|
from .templatetags.markdown import markdown
|
|
|
|
|
2018-06-21 22:31:03 -04:00
|
|
|
|
|
|
|
class PackageJson:
|
|
|
|
data: Optional[Dict[str, Any]]
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self.data = None
|
|
|
|
|
|
|
|
def load(self) -> Dict[str, Any]:
|
|
|
|
if self.data is None:
|
|
|
|
with open(join(settings.BASE_DIR, 'package.json')) as f:
|
|
|
|
self.data = json.load(f)
|
|
|
|
assert self.data is not None
|
|
|
|
return self.data
|
|
|
|
|
|
|
|
|
|
|
|
PACKAGE = PackageJson()
|
2017-10-26 22:24:57 -04:00
|
|
|
|
|
|
|
|
2018-06-19 01:47:10 -04:00
|
|
|
def friendly_url(url):
|
2018-07-01 01:26:55 -04:00
|
|
|
if '//' not in url:
|
|
|
|
url = '//' + url
|
2018-06-19 01:47:10 -04:00
|
|
|
(scheme, netloc, path, params, q, fragment) = urlparse(url)
|
2018-06-25 08:31:42 -04:00
|
|
|
if path == '/':
|
|
|
|
return netloc
|
2018-07-01 01:26:55 -04:00
|
|
|
return "{}\u200B{}".format(netloc, path)
|
2018-06-19 01:47:10 -04:00
|
|
|
|
|
|
|
|
2018-06-21 22:31:03 -04:00
|
|
|
def load_package_json() -> Dict[str, Any]:
|
|
|
|
return PACKAGE.load()
|
2017-10-26 22:24:57 -04:00
|
|
|
|
|
|
|
|
|
|
|
def origin(request):
|
2017-10-28 21:12:39 -04:00
|
|
|
return '{0}://{1}'.format(request.scheme, request.site.domain)
|
2017-10-26 22:24:57 -04:00
|
|
|
|
|
|
|
|
2018-05-07 08:28:48 -04:00
|
|
|
def absolute_url(request, url):
|
|
|
|
return urljoin(origin(request), url)
|
|
|
|
|
|
|
|
|
2017-10-26 22:24:57 -04:00
|
|
|
def uri(request):
|
|
|
|
return origin(request) + request.path
|
2017-10-29 02:07:36 -04:00
|
|
|
|
|
|
|
|
|
|
|
def form_encoded_response(content):
|
|
|
|
return HttpResponse(
|
|
|
|
urlencode(content),
|
|
|
|
content_type='application/x-www-form-urlencoded'
|
|
|
|
)
|
2017-10-29 23:58:50 -04:00
|
|
|
|
|
|
|
|
2017-11-03 02:18:00 -04:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2017-11-02 01:16:04 -04:00
|
|
|
def bad_req(message):
|
|
|
|
return HttpResponseBadRequest(message, content_type='text/plain')
|
|
|
|
|
|
|
|
|
|
|
|
def forbid(message):
|
|
|
|
return HttpResponseForbidden(message, content_type='text/plain')
|
2017-12-10 21:30:46 -05:00
|
|
|
|
|
|
|
|
|
|
|
def to_plain(md):
|
|
|
|
return html.unescape(strip_tags(markdown(md)))
|