From 065619772e8faf19220f97104f877722a4df7e52 Mon Sep 17 00:00:00 2001 From: Danielle McLean Date: Tue, 3 Jul 2018 09:51:51 +1000 Subject: [PATCH] Use ResponseException for various places rather than needing to check the return value for responseness --- entries/pagination.py | 4 +++- entries/views/lists.py | 4 ---- home/views.py | 4 ---- lemonauth/tokens.py | 13 ++++++++----- lemonauth/views/token.py | 2 -- micropub/views/__init__.py | 5 +---- micropub/views/media.py | 2 -- micropub/views/query.py | 9 +++++---- 8 files changed, 17 insertions(+), 26 deletions(-) diff --git a/entries/pagination.py b/entries/pagination.py index ab3763e..aa7c174 100644 --- a/entries/pagination.py +++ b/entries/pagination.py @@ -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) diff --git a/entries/views/lists.py b/entries/views/lists.py index 35afd08..c177670 100644 --- a/entries/views/lists.py +++ b/entries/views/lists.py @@ -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, diff --git a/home/views.py b/home/views.py index bb414e4..d5ddea0 100644 --- a/home/views.py +++ b/home/views.py @@ -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, diff --git a/lemonauth/tokens.py b/lemonauth/tokens.py index 8749e93..2ea16ff 100644 --- a/lemonauth/tokens.py +++ b/lemonauth/tokens.py @@ -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 = 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 diff --git a/lemonauth/views/token.py b/lemonauth/views/token.py index 72543f0..251c016 100644 --- a/lemonauth/views/token.py +++ b/lemonauth/views/token.py @@ -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, diff --git a/micropub/views/__init__.py b/micropub/views/__init__.py index f487986..7998379 100644 --- a/micropub/views/__init__.py +++ b/micropub/views/__init__.py @@ -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) diff --git a/micropub/views/media.py b/micropub/views/media.py index 5fe3128..85a22a0 100644 --- a/micropub/views/media.py +++ b/micropub/views/media.py @@ -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" diff --git a/micropub/views/query.py b/micropub/views/query.py index 0bbda24..2dcd9ac 100644 --- a/micropub/views/query.py +++ b/micropub/views/query.py @@ -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)