lemoncurry/micropub/views/__init__.py

32 lines
824 B
Python
Raw Normal View History

import json
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_http_methods
from lemonauth import tokens
from .create import create
from .delete import delete
from .query import query
actions = {
2023-08-10 02:52:37 -04:00
"create": create,
"delete": delete,
}
@csrf_exempt
2023-08-10 02:52:37 -04:00
@require_http_methods(["GET", "HEAD", "POST"])
def micropub(request):
request.token = tokens.auth(request)
2023-08-10 02:52:37 -04:00
if request.method in ("GET", "HEAD"):
return query(request)
2023-08-10 02:52:37 -04:00
action = request.POST.get("action", "create")
if request.content_type == "application/json":
request.json = json.load(request)
2023-08-10 02:52:37 -04:00
action = request.json.get("action", "create")
if action not in actions:
2023-08-10 02:52:37 -04:00
raise error.bad_req("unknown action: {}".format(action))
return actions[action](request)