From b8a8cd62cf33376cc643fbf49e1c8d0ec53d8734 Mon Sep 17 00:00:00 2001 From: Danielle McLean Date: Thu, 9 Nov 2017 13:55:44 +1100 Subject: [PATCH] Refactor micropub into a class-based view so that it can have a GET handler implemented as well (since that's needed to query for config info and stuff) --- micropub/views.py | 82 +++++++++++++++++++++++++---------------------- 1 file changed, 43 insertions(+), 39 deletions(-) diff --git a/micropub/views.py b/micropub/views.py index 743a22b..5ce51c9 100644 --- a/micropub/views.py +++ b/micropub/views.py @@ -1,8 +1,9 @@ from django.contrib.auth import get_user_model from django.http import HttpResponse from django.urls import reverse +from django.utils.decorators import method_decorator +from django.views import View from django.views.decorators.csrf import csrf_exempt -from django.views.decorators.http import require_POST from urllib.parse import urljoin from entries.jobs import ping_hub, send_mentions @@ -12,45 +13,48 @@ from lemoncurry import utils from lemonauth import tokens -@csrf_exempt -@require_POST -def micropub(request): - auth = request.META.get('HTTP_AUTHORIZATION', '').split(' ') - if auth[0] != 'Bearer': - return utils.bad_req('only Bearer auth supported') - try: - token = tokens.decode(auth[1]) - except Exception: - return utils.forbid('invalid token') - user = get_user_model().objects.get(pk=token['uid']) +@method_decorator(csrf_exempt, name='dispatch') +class MicropubView(View): + def post(self, request): + auth = request.META.get('HTTP_AUTHORIZATION', '').split(' ') + if auth[0] != 'Bearer': + return utils.bad_req('only Bearer auth supported') + try: + token = tokens.decode(auth[1]) + except Exception: + return utils.forbid('invalid token') + user = get_user_model().objects.get(pk=token['uid']) - post = request.POST - if post.get('h') != 'entry': - return utils.bad_req('only h=entry supported') - entry = Entry(author=user) - kind = Note - if 'name' in post: - entry.name = post['name'] - kind = Article - if 'content' in post: - entry.content = post['content'] + post = request.POST + if post.get('h') != 'entry': + return utils.bad_req('only h=entry supported') + entry = Entry(author=user) + kind = Note + if 'name' in post: + entry.name = post['name'] + kind = Article + if 'content' in post: + entry.content = post['content'] - entry.kind = kind.id - entry.save() + entry.kind = kind.id + entry.save() - base = utils.origin(request) - perma = urljoin(base, entry.url) - others = (urljoin(base, url) for url in ( - reverse('home:index'), - reverse('entries:atom'), - reverse('entries:rss'), - reverse('entries:' + kind.index), - reverse('entries:' + kind.atom), - reverse('entries:' + kind.rss), - )) - ping_hub.delay(perma, *others) - send_mentions.delay(perma) + base = utils.origin(request) + perma = urljoin(base, entry.url) + others = (urljoin(base, url) for url in ( + reverse('home:index'), + reverse('entries:atom'), + reverse('entries:rss'), + reverse('entries:' + kind.index), + reverse('entries:' + kind.atom), + reverse('entries:' + kind.rss), + )) + ping_hub.delay(perma, *others) + send_mentions.delay(perma) - res = HttpResponse(status=201) - res['Location'] = perma - return res + res = HttpResponse(status=201) + res['Location'] = perma + return res + + +micropub = MicropubView.as_view()