Skip to content

Commit 0aef0fd

Browse files
committed
Update authentication helper so it works correctly with Django versions
where "request.user.is_authentication" variable is a property and not a method (aka newer Django versions.)
1 parent 3372375 commit 0aef0fd

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

codespeed/auth.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import types
23
from functools import wraps
34
from django.contrib.auth import authenticate, login
45
from django.http import HttpResponse, HttpResponseForbidden
@@ -9,6 +10,20 @@
910
logger = logging.getLogger(__name__)
1011

1112

13+
def is_authenticated(request):
14+
# NOTE: We do type check so we also support newer versions of Djando when
15+
# is_authenticated and some other methods have been properties
16+
if isinstance(request.user.is_authenticated, (types.FunctionType,
17+
types.MethodType)):
18+
return request.user.is_authenticated()
19+
elif isinstance(request.user.is_authenticated, bool):
20+
return request.user.is_authenticated
21+
else:
22+
logger.info('Got unexpected type for request.user.is_authenticated '
23+
'variable')
24+
return False
25+
26+
1227
def basic_auth_required(realm='default'):
1328
def _helper(func):
1429
@wraps(func)
@@ -18,7 +33,7 @@ def _decorator(request, *args, **kwargs):
1833
if settings.ALLOW_ANONYMOUS_POST:
1934
logger.debug('allowing anonymous post')
2035
allowed = True
21-
elif hasattr(request, 'user') and request.user.is_authenticated():
36+
elif hasattr(request, 'user') and is_authenticated(request=request):
2237
allowed = True
2338
elif 'HTTP_AUTHORIZATION' in request.META:
2439
logger.debug('checking for http authorization header')

0 commit comments

Comments
 (0)