Skip to content

Commit b51f1ea

Browse files
committed
Move model vars into init.
1 parent 1aa5d68 commit b51f1ea

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

rest_framework_simplejwt/authentication.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
from django.contrib.auth import get_user_model
12
from django.utils.translation import ugettext_lazy as _
23
from rest_framework import HTTP_HEADER_ENCODING, authentication
34

45
from .exceptions import AuthenticationFailed, InvalidToken, TokenError
56
from .models import TokenUser
67
from .settings import api_settings
7-
from .state import User
88

99
AUTH_HEADER_TYPES = api_settings.AUTH_HEADER_TYPES
1010

@@ -24,6 +24,9 @@ class JWTAuthentication(authentication.BaseAuthentication):
2424
"""
2525
www_authenticate_realm = 'api'
2626

27+
def __init__(self):
28+
self.user_model = get_user_model()
29+
2730
def authenticate(self, request):
2831
header = self.get_header(request)
2932
if header is None:
@@ -108,8 +111,8 @@ def get_user(self, validated_token):
108111
raise InvalidToken(_('Token contained no recognizable user identification'))
109112

110113
try:
111-
user = User.objects.get(**{api_settings.USER_ID_FIELD: user_id})
112-
except User.DoesNotExist:
114+
user = self.user_model.objects.get(**{api_settings.USER_ID_FIELD: user_id})
115+
except self.User.DoesNotExist:
113116
raise AuthenticationFailed(_('User not found'), code='user_not_found')
114117

115118
if not user.is_active:

rest_framework_simplejwt/models.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from django.contrib.auth import models as auth_models
21
from django.db.models.manager import EmptyManager
32
from django.utils.functional import cached_property
43

@@ -19,11 +18,13 @@ class instead of a `User` model instance. Instances of this class act as
1918
# inactive user
2019
is_active = True
2120

22-
_groups = EmptyManager(auth_models.Group)
23-
_user_permissions = EmptyManager(auth_models.Permission)
2421

2522
def __init__(self, token):
2623
self.token = token
24+
from django.contrib.auth import models as auth_models
25+
self._groups = EmptyManager(auth_models.Group)
26+
self._user_permissions = EmptyManager(auth_models.Permission)
27+
2728

2829
def __str__(self):
2930
return 'TokenUser {}'.format(self.id)

rest_framework_simplejwt/serializers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
from django.contrib.auth import get_user_model
12
from django.contrib.auth import authenticate
23
from django.utils.translation import ugettext_lazy as _
34
from rest_framework import exceptions, serializers
45

56
from .settings import api_settings
6-
from .state import User
77
from .tokens import RefreshToken, SlidingToken, UntypedToken
88

99

@@ -18,7 +18,9 @@ def __init__(self, *args, **kwargs):
1818

1919

2020
class TokenObtainSerializer(serializers.Serializer):
21-
username_field = User.USERNAME_FIELD
21+
user_model = get_user_model()
22+
23+
username_field = user_model.USERNAME_FIELD
2224

2325
default_error_messages = {
2426
'no_active_account': _('No active account found with the given credentials')

rest_framework_simplejwt/state.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@
33
from .backends import TokenBackend
44
from .settings import api_settings
55

6-
User = get_user_model()
76
token_backend = TokenBackend(api_settings.ALGORITHM, api_settings.SIGNING_KEY,
87
api_settings.VERIFYING_KEY, api_settings.AUDIENCE, api_settings.ISSUER)

0 commit comments

Comments
 (0)