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 untrusted user: 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.core.paginator import Paginator
from django.shortcuts import redirect from django.shortcuts import redirect
from lemoncurry.middleware import ResponseException
def paginate(queryset, reverse, page): def paginate(queryset, reverse, page):
class 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 # If the first page was requested, redirect to the clean version of the URL
# with no page suffix. # with no page suffix.
if page == 1: if page == 1:
return redirect(Page(1).url) raise ResponseException(redirect(Page(1).url))
paginator = Paginator(queryset, 10) paginator = Paginator(queryset, 10)
entries = paginator.page(page or 1) entries = paginator.page(page or 1)

View file

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

View file

@ -24,10 +24,6 @@ def index(request, page=None):
entries = user.entries.filter(kind__in=kinds.on_home) entries = user.entries.filter(kind__in=kinds.on_home)
entries = pagination.paginate(queryset=entries, reverse=url, page=page) 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 { return {
'user': user, 'user': user,
'entries': entries, 'entries': entries,

View file

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

View file

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

View file

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

View file

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

View file

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