Browse Source

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

Danielle McLean 6 months ago
parent
commit
065619772e
Signed by: Danielle McLean <dani@00dani.me> GPG Key ID: 8EB789DDF3ABD240

+ 3
- 1
entries/pagination.py View File

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

+ 0
- 4
entries/views/lists.py View File

@@ -9,8 +9,6 @@ from ..pagination import paginate
9 9
 def by_kind(request, kind, page=None):
10 10
     entries = Entry.objects.filter(kind=kind.id)
11 11
     entries = paginate(queryset=entries, reverse=kind.index_page, page=page)
12
-    if hasattr(entries, 'content'):
13
-        return entries
14 12
 
15 13
     return {
16 14
         'entries': entries,
@@ -31,8 +29,6 @@ def by_cat(request, slug, page=None):
31 29
     cat = get_object_or_404(Cat, slug=slug)
32 30
     entries = cat.entries.all()
33 31
     entries = paginate(queryset=entries, reverse=url, page=page)
34
-    if hasattr(entries, 'content'):
35
-        return entries
36 32
 
37 33
     return {
38 34
         'entries': entries,

+ 0
- 4
home/views.py View File

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

+ 8
- 5
lemonauth/tokens.py View File

@@ -1,3 +1,4 @@
1
+from lemoncurry.middleware import ResponseException
1 2
 from micropub.views import error
2 3
 from .models import IndieAuthCode, Token
3 4
 
@@ -6,23 +7,25 @@ def auth(request):
6 7
     if 'HTTP_AUTHORIZATION' in request.META:
7 8
         auth = request.META.get('HTTP_AUTHORIZATION').split(' ')
8 9
         if auth[0] != 'Bearer':
9
-            return error.bad_req('auth type {0} not supported'.format(auth[0]))
10
+            raise ResponseException(error.bad_req(
11
+                'auth type {0} not supported'.format(auth[0])
12
+            ))
10 13
         if len(auth) != 2:
11
-            return error.bad_req(
14
+            raise ResponseException(error.bad_req(
12 15
                 'invalid Bearer auth format, must be Bearer <token>'
13
-            )
16
+            ))
14 17
         token = auth[1]
15 18
     elif 'access_token' in request.POST:
16 19
         token = request.POST.get('access_token')
17 20
     elif 'access_token' in request.GET:
18 21
         token = request.GET.get('access_token')
19 22
     else:
20
-        return error.unauthorized()
23
+        raise ResponseException(error.unauthorized())
21 24
 
22 25
     try:
23 26
         token = Token.objects.get(pk=token)
24 27
     except Token.DoesNotExist:
25
-        return error.forbidden()
28
+        raise ResponseException(error.forbidden())
26 29
 
27 30
     return token
28 31
 

+ 0
- 2
lemonauth/views/token.py View File

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

+ 1
- 4
micropub/views/__init__.py View File

@@ -18,10 +18,7 @@ actions = {
18 18
 @csrf_exempt
19 19
 @require_http_methods(['GET', 'HEAD', 'POST'])
20 20
 def micropub(request):
21
-    token = tokens.auth(request)
22
-    if hasattr(token, 'content'):
23
-        return token
24
-    request.token = token
21
+    request.token = tokens.auth(request)
25 22
     if request.method in ('GET', 'HEAD'):
26 23
         return query(request)
27 24
 

+ 0
- 2
micropub/views/media.py View File

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

+ 5
- 4
micropub/views/query.py View File

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

Loading…
Cancel
Save