Normalise formencoded Micropub requests to JSON form, and handle all requests as JSON, so now JSON request bodies work too
This commit is contained in:
parent
29a3f740a9
commit
d30f1bc334
1 changed files with 32 additions and 18 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
import json
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.utils.decorators import method_decorator
|
from django.utils.decorators import method_decorator
|
||||||
|
@ -12,8 +13,9 @@ from lemoncurry import utils
|
||||||
from lemonauth import tokens
|
from lemonauth import tokens
|
||||||
|
|
||||||
|
|
||||||
def form_to_mf2(post):
|
def form_to_mf2(request):
|
||||||
properties = {}
|
properties = {}
|
||||||
|
post = request.POST
|
||||||
for key in post.keys():
|
for key in post.keys():
|
||||||
if key.endswith('[]'):
|
if key.endswith('[]'):
|
||||||
key = key[:-2]
|
key = key[:-2]
|
||||||
|
@ -35,30 +37,42 @@ class MicropubView(View):
|
||||||
if hasattr(token, 'content'):
|
if hasattr(token, 'content'):
|
||||||
return token
|
return token
|
||||||
|
|
||||||
post = request.POST
|
normalise = {
|
||||||
if post.get('h') != 'entry':
|
'application/json': json.load,
|
||||||
return utils.bad_req('only h=entry supported')
|
'application/x-www-form-urlencoded': form_to_mf2,
|
||||||
|
}
|
||||||
|
if request.content_type not in normalise:
|
||||||
|
return HttpResponse(
|
||||||
|
'unsupported request type {0}'.format(request.content_type),
|
||||||
|
content_type='text/plain',
|
||||||
|
status=415,
|
||||||
|
)
|
||||||
|
body = normalise[request.content_type](request)
|
||||||
|
print(body)
|
||||||
|
if 'type' not in body:
|
||||||
|
return utils.bad_req('mf2 object type required')
|
||||||
|
if body['type'] != ['h-entry']:
|
||||||
|
return utils.bad_req('only h-entry supported')
|
||||||
|
|
||||||
entry = Entry(author=token.user)
|
entry = Entry(author=token.user)
|
||||||
|
props = body.get('properties', {})
|
||||||
kind = Note
|
kind = Note
|
||||||
if 'name' in post:
|
if 'name' in props:
|
||||||
entry.name = post['name']
|
entry.name = '\n'.join(props['name'])
|
||||||
kind = Article
|
kind = Article
|
||||||
if 'content' in post:
|
if 'content' in props:
|
||||||
entry.content = post['content']
|
entry.content = '\n'.join(props['content'])
|
||||||
if 'in-reply-to' in post:
|
if 'in-reply-to' in props:
|
||||||
entry.in_reply_to = post['in-reply-to']
|
entry.in_reply_to = props['in-reply-to']
|
||||||
kind = Reply
|
kind = Reply
|
||||||
if 'like-of' in post:
|
if 'like-of' in props:
|
||||||
entry.like_of = post['like-of']
|
entry.like_of = props['like-of']
|
||||||
kind = Like
|
kind = Like
|
||||||
if 'repost-of' in post:
|
if 'repost-of' in props:
|
||||||
entry.repost_of = post['repost-of']
|
entry.repost_of = props['repost-of']
|
||||||
kind = Repost
|
kind = Repost
|
||||||
|
|
||||||
cats = [
|
cats = [Cat.objects.from_name(c) for c in props.get('category', [])]
|
||||||
Cat.objects.from_name(c) for c in
|
|
||||||
post.getlist('category') + post.getlist('category[]')
|
|
||||||
]
|
|
||||||
|
|
||||||
entry.kind = kind.id
|
entry.kind = kind.id
|
||||||
entry.save()
|
entry.save()
|
||||||
|
|
Loading…
Reference in a new issue