a Django-based indieweb.org site https://00dani.me/

query.py 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from django.http import JsonResponse
  2. from django.urls import reverse
  3. from entries.from_url import from_url
  4. from lemoncurry import requests
  5. from lemoncurry.utils import absolute_url
  6. from .. import error
  7. def config(request):
  8. config = syndicate_to(request)
  9. config['media-endpoint'] = absolute_url(request, reverse('micropub:media'))
  10. return config
  11. def source(request):
  12. if 'url' not in request.GET:
  13. raise error.bad_req('must specify url parameter for source query')
  14. entry = from_url(request.GET['url'])
  15. props = {}
  16. keys = set(request.GET.getlist('properties') + request.GET.getlist('properties[]'))
  17. if not keys or 'content' in keys:
  18. props['content'] = [entry.content]
  19. if (not keys or 'category' in keys) and entry.cats.exists():
  20. props['category'] = [cat.name for cat in entry.cats.all()]
  21. if (not keys or 'name' in keys) and entry.name:
  22. props['name'] = [entry.name]
  23. if (not keys or 'syndication' in keys) and entry.syndications.exists():
  24. props['syndication'] = [synd.url for synd in entry.syndications.all()]
  25. return {'type': ['h-entry'], 'properties': props}
  26. def syndicate_to(request):
  27. return {'syndicate-to': []}
  28. queries = {
  29. 'config': config,
  30. 'source': source,
  31. 'syndicate-to': syndicate_to,
  32. }
  33. def query(request):
  34. if 'q' not in request.GET:
  35. raise error.bad_req('must specify q parameter')
  36. q = request.GET['q']
  37. if q not in queries:
  38. raise error.bad_req('unsupported query {0}'.format(q))
  39. res = queries[q](request)
  40. return JsonResponse(res)