Use ResponseException for various places rather than needing to check the return value for responseness

This commit is contained in:
Danielle McLean 2018-07-03 09:51:51 +10:00
parent 7d17a92793
commit 065619772e
Signed by: 00dani
GPG key ID: 8EB789DDF3ABD240
8 changed files with 17 additions and 26 deletions

View file

@ -1,6 +1,8 @@
from django.core.paginator import Paginator
from django.shortcuts import redirect
from lemoncurry.middleware import ResponseException
def paginate(queryset, reverse, page):
class Page:
@ -18,7 +20,7 @@ def paginate(queryset, reverse, page):
# If the first page was requested, redirect to the clean version of the URL
# with no page suffix.
if page == 1:
return redirect(Page(1).url)
raise ResponseException(redirect(Page(1).url))
paginator = Paginator(queryset, 10)
entries = paginator.page(page or 1)

View file

@ -9,8 +9,6 @@ from ..pagination import paginate
def by_kind(request, kind, page=None):
entries = Entry.objects.filter(kind=kind.id)
entries = paginate(queryset=entries, reverse=kind.index_page, page=page)
if hasattr(entries, 'content'):
return entries
return {
'entries': entries,
@ -31,8 +29,6 @@ def by_cat(request, slug, page=None):
cat = get_object_or_404(Cat, slug=slug)
entries = cat.entries.all()
entries = paginate(queryset=entries, reverse=url, page=page)
if hasattr(entries, 'content'):
return entries
return {
'entries': entries,

View file

@ -24,10 +24,6 @@ def index(request, page=None):
entries = user.entries.filter(kind__in=kinds.on_home)
entries = pagination.paginate(queryset=entries, reverse=url, page=page)
# If we got a valid HTTP response, just return it without rendering.
if hasattr(entries, 'content'):
return entries
return {
'user': user,
'entries': entries,

View file

@ -1,3 +1,4 @@
from lemoncurry.middleware import ResponseException
from micropub.views import error
from .models import IndieAuthCode, Token
@ -6,23 +7,25 @@ def auth(request):
if 'HTTP_AUTHORIZATION' in request.META:
auth = request.META.get('HTTP_AUTHORIZATION').split(' ')
if auth[0] != 'Bearer':
return error.bad_req('auth type {0} not supported'.format(auth[0]))
raise ResponseException(error.bad_req(
'auth type {0} not supported'.format(auth[0])
))
if len(auth) != 2:
return error.bad_req(
raise ResponseException(error.bad_req(
'invalid Bearer auth format, must be Bearer <token>'
)
))
token = auth[1]
elif 'access_token' in request.POST:
token = request.POST.get('access_token')
elif 'access_token' in request.GET:
token = request.GET.get('access_token')
else:
return error.unauthorized()
raise ResponseException(error.unauthorized())
try:
token = Token.objects.get(pk=token)
except Token.DoesNotExist:
return error.forbidden()
raise ResponseException(error.forbidden())
return token

View file

@ -11,8 +11,6 @@ from lemoncurry import utils
class TokenView(View):
def get(self, req):
token = tokens.auth(req)
if hasattr(token, 'content'):
return token
res = {
'me': token.me,
'client_id': token.client_id,

View file

@ -18,10 +18,7 @@ actions = {
@csrf_exempt
@require_http_methods(['GET', 'HEAD', 'POST'])
def micropub(request):
token = tokens.auth(request)
if hasattr(token, 'content'):
return token
request.token = token
request.token = tokens.auth(request)
if request.method in ('GET', 'HEAD'):
return query(request)

View file

@ -21,8 +21,6 @@ ACCEPTED_MEDIA_TYPES = (
@require_POST
def media(request):
token = tokens.auth(request)
if hasattr(token, 'content'):
return token
if 'file' not in request.FILES:
return error.bad_req(
"a file named 'file' must be provided to the media endpoint"

View file

@ -1,6 +1,7 @@
from django.http import JsonResponse
from django.urls import reverse
from lemoncurry import requests
from lemoncurry.middleware import ResponseException
from lemoncurry.utils import absolute_url
from . import error
@ -14,10 +15,12 @@ def config(request):
def source(request):
get = request.GET
if 'url' not in get:
return error.bad_req('must specify url parameter for source query')
raise ResponseException(error.bad_req(
'must specify url parameter for source query'
))
mf2 = requests.mf2(get['url']).to_dict(filter_by_type='h-entry')
if not mf2:
return error.bad_req('no h-entry at the requested url')
raise ResponseException(error.bad_req('no h-entry at the requested url'))
entry = mf2[0]
keys = get.getlist('properties', []) + get.getlist('properties[]', [])
if not keys:
@ -45,6 +48,4 @@ def query(request):
if q not in queries:
return error.bad_req('unsupported query {0}'.format(q))
res = queries[q](request)
if hasattr(res, 'content'):
return res
return JsonResponse(res)