Skip to content

Fix authtoken.TokenProxy error when not installed in Django apps #7584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions rest_framework/authtoken/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db import models
from django.utils.translation import gettext_lazy as _

Expand Down Expand Up @@ -40,14 +41,31 @@ def __str__(self):
return self.key


class TokenProxy(Token):
"""
Proxy mapping pk to user pk for use in admin.
"""
@property
def pk(self):
return self.user.pk
if 'rest_framework.authtoken' in settings.INSTALLED_APPS:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The token model could also be moved into this block and use the same pattern as I did for TokenProxy

class TokenProxy(Token):
"""
Proxy mapping pk to user pk for use in admin.
"""
@property
def pk(self):
return self.user.pk

class Meta:
proxy = True
verbose_name = "token"
class Meta:
proxy = True
verbose_name = "token"
else:
def improperly_configured(*args, **kwargs):
raise ImproperlyConfigured(
'"rest_framework.authtoken" must be in your '
'settings.INSTALLED_APPS to use the Token or TokenProxy model.')

# throw improperly_configured when accessing TokenProxy.objects
class Descriptor:
def __get__(self, obj, type=None):
improperly_configured()

class TokenProxy:
def __init__(self, *args, **kwargs):
improperly_configured()

objects = Descriptor()
30 changes: 29 additions & 1 deletion tests/test_authtoken.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import importlib
from io import StringIO

import pytest
from django.contrib.admin import site
from django.contrib.auth.models import User
from django.core.exceptions import ImproperlyConfigured
from django.core.management import CommandError, call_command
from django.test import TestCase
from django.db import models
from django.test import TestCase, modify_settings

from rest_framework.authtoken.admin import TokenAdmin
from rest_framework.authtoken.management.commands.drf_create_token import \
Expand All @@ -21,6 +24,31 @@ def setUp(self):
self.user = User.objects.create_user(username='test_user')
self.token = Token.objects.create(key='test token', user=self.user)

def test_authtoken_can_be_imported_when_not_installed(self):
try:
import rest_framework.authtoken.models
authtoken_models = rest_framework.authtoken.models
assert issubclass(authtoken_models.Token, models.Model)
assert issubclass(authtoken_models.TokenProxy, models.Model)
assert not authtoken_models.Token._meta.abstract
assert authtoken_models.TokenProxy._meta.proxy

with modify_settings(INSTALLED_APPS={
'remove': 'rest_framework.authtoken'}):
importlib.reload(rest_framework.authtoken.models)
authtoken_models = rest_framework.authtoken.models
assert issubclass(authtoken_models.Token, models.Model)
assert authtoken_models.Token._meta.abstract
with pytest.raises(ImproperlyConfigured):
authtoken_models.TokenProxy()
with pytest.raises(ImproperlyConfigured):
authtoken_models.TokenProxy.objects

finally:
# Set the proxy and abstract properties back to the version,
# where authtoken is among INSTALLED_APPS.
importlib.reload(rest_framework.authtoken.models)

def test_model_admin_displayed_fields(self):
mock_request = object()
token_admin = TokenAdmin(self.token, self.site)
Expand Down