2018-01-28 18:28:46 -05:00
|
|
|
from django.http import JsonResponse
|
2018-05-07 08:28:48 -04:00
|
|
|
from django.urls import reverse
|
2018-07-02 20:18:24 -04:00
|
|
|
|
|
|
|
from entries.from_url import from_url
|
2018-01-28 18:28:46 -05:00
|
|
|
from lemoncurry import requests
|
2018-05-07 08:28:48 -04:00
|
|
|
from lemoncurry.utils import absolute_url
|
2018-07-02 20:03:35 -04:00
|
|
|
from .. import error
|
2018-01-28 18:28:46 -05:00
|
|
|
|
|
|
|
|
|
|
|
def config(request):
|
|
|
|
config = syndicate_to(request)
|
2023-08-10 02:52:37 -04:00
|
|
|
config["media-endpoint"] = absolute_url(request, reverse("micropub:media"))
|
2018-01-28 18:28:46 -05:00
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
|
|
def source(request):
|
2023-08-10 02:52:37 -04:00
|
|
|
if "url" not in request.GET:
|
|
|
|
raise error.bad_req("must specify url parameter for source query")
|
|
|
|
entry = from_url(request.GET["url"])
|
2018-07-02 20:18:24 -04:00
|
|
|
props = {}
|
|
|
|
|
2023-08-10 02:52:37 -04:00
|
|
|
keys = set(request.GET.getlist("properties") + request.GET.getlist("properties[]"))
|
|
|
|
if not keys or "content" in keys:
|
|
|
|
props["content"] = [entry.content]
|
|
|
|
if (not keys or "category" in keys) and entry.cats.exists():
|
|
|
|
props["category"] = [cat.name for cat in entry.cats.all()]
|
|
|
|
if (not keys or "name" in keys) and entry.name:
|
|
|
|
props["name"] = [entry.name]
|
|
|
|
if (not keys or "syndication" in keys) and entry.syndications.exists():
|
|
|
|
props["syndication"] = [synd.url for synd in entry.syndications.all()]
|
2018-07-02 20:18:24 -04:00
|
|
|
|
2023-08-10 02:52:37 -04:00
|
|
|
return {"type": ["h-entry"], "properties": props}
|
2018-01-28 18:28:46 -05:00
|
|
|
|
|
|
|
|
|
|
|
def syndicate_to(request):
|
2023-08-10 02:52:37 -04:00
|
|
|
return {"syndicate-to": []}
|
2018-01-28 18:28:46 -05:00
|
|
|
|
|
|
|
|
|
|
|
queries = {
|
2023-08-10 02:52:37 -04:00
|
|
|
"config": config,
|
|
|
|
"source": source,
|
|
|
|
"syndicate-to": syndicate_to,
|
2018-01-28 18:28:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def query(request):
|
2023-08-10 02:52:37 -04:00
|
|
|
if "q" not in request.GET:
|
|
|
|
raise error.bad_req("must specify q parameter")
|
|
|
|
q = request.GET["q"]
|
2018-01-28 18:28:46 -05:00
|
|
|
if q not in queries:
|
2023-08-10 02:52:37 -04:00
|
|
|
raise error.bad_req("unsupported query {0}".format(q))
|
2018-01-28 18:28:46 -05:00
|
|
|
res = queries[q](request)
|
|
|
|
return JsonResponse(res)
|