Skip to content

Commit

Permalink
Move model vars into init.
Browse files Browse the repository at this point in the history
  • Loading branch information
olymk2 committed Nov 14, 2019
1 parent 1aa5d68 commit b51f1ea
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
9 changes: 6 additions & 3 deletions rest_framework_simplejwt/authentication.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from django.contrib.auth import get_user_model
from django.utils.translation import ugettext_lazy as _
from rest_framework import HTTP_HEADER_ENCODING, authentication

from .exceptions import AuthenticationFailed, InvalidToken, TokenError
from .models import TokenUser
from .settings import api_settings
from .state import User

AUTH_HEADER_TYPES = api_settings.AUTH_HEADER_TYPES

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

def __init__(self):
self.user_model = get_user_model()

def authenticate(self, request):
header = self.get_header(request)
if header is None:
Expand Down Expand Up @@ -108,8 +111,8 @@ def get_user(self, validated_token):
raise InvalidToken(_('Token contained no recognizable user identification'))

try:
user = User.objects.get(**{api_settings.USER_ID_FIELD: user_id})
except User.DoesNotExist:
user = self.user_model.objects.get(**{api_settings.USER_ID_FIELD: user_id})
except self.User.DoesNotExist:
raise AuthenticationFailed(_('User not found'), code='user_not_found')

if not user.is_active:
Expand Down
7 changes: 4 additions & 3 deletions rest_framework_simplejwt/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django.contrib.auth import models as auth_models
from django.db.models.manager import EmptyManager
from django.utils.functional import cached_property

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

_groups = EmptyManager(auth_models.Group)
_user_permissions = EmptyManager(auth_models.Permission)

def __init__(self, token):
self.token = token
from django.contrib.auth import models as auth_models
self._groups = EmptyManager(auth_models.Group)
self._user_permissions = EmptyManager(auth_models.Permission)


def __str__(self):
return 'TokenUser {}'.format(self.id)
Expand Down
6 changes: 4 additions & 2 deletions rest_framework_simplejwt/serializers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from django.contrib.auth import get_user_model
from django.contrib.auth import authenticate
from django.utils.translation import ugettext_lazy as _
from rest_framework import exceptions, serializers

from .settings import api_settings
from .state import User
from .tokens import RefreshToken, SlidingToken, UntypedToken


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


class TokenObtainSerializer(serializers.Serializer):
username_field = User.USERNAME_FIELD
user_model = get_user_model()

username_field = user_model.USERNAME_FIELD

default_error_messages = {
'no_active_account': _('No active account found with the given credentials')
Expand Down
1 change: 0 additions & 1 deletion rest_framework_simplejwt/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
from .backends import TokenBackend
from .settings import api_settings

User = get_user_model()
token_backend = TokenBackend(api_settings.ALGORITHM, api_settings.SIGNING_KEY,
api_settings.VERIFYING_KEY, api_settings.AUDIENCE, api_settings.ISSUER)

0 comments on commit b51f1ea

Please sign in to comment.