Skip to content

Commit 5f20bf9

Browse files
Explore swappable API key model functionality
1 parent dd806cf commit 5f20bf9

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

src/rest_framework_api_key/admin.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from django.db import models
55
from django.http.request import HttpRequest
66

7-
from .models import AbstractAPIKey, APIKey
7+
from .models import AbstractAPIKey
88

99

1010
class APIKeyModelAdmin(admin.ModelAdmin):
@@ -55,6 +55,4 @@ def save_model(
5555
obj.save()
5656

5757

58-
admin.site.register(APIKey, APIKeyModelAdmin)
59-
6058
APIKeyAdmin = APIKeyModelAdmin # Compatibility with <1.3

src/rest_framework_api_key/models.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import typing
22

3-
from django.core.exceptions import ValidationError
3+
from django.apps import apps as django_apps
4+
from django.conf import settings
5+
from django.core.exceptions import ImproperlyConfigured, ValidationError
46
from django.db import models
57
from django.utils import timezone
68

@@ -142,5 +144,31 @@ def __str__(self) -> str:
142144
return str(self.name)
143145

144146

147+
def get_swappable_setting() -> str:
148+
if not hasattr(settings, "API_KEY_MODEL"):
149+
# Ensure a default value is set.
150+
settings.API_KEY_MODEL = "rest_framework_api_key.APIKey"
151+
152+
return "API_KEY_MODEL"
153+
154+
145155
class APIKey(AbstractAPIKey):
146-
pass
156+
class Meta(AbstractAPIKey.Meta):
157+
swappable = get_swappable_setting()
158+
159+
160+
def get_api_key_model() -> typing.Type[AbstractAPIKey]:
161+
"""
162+
Return the API key model that is active in this project.
163+
"""
164+
try:
165+
return django_apps.get_model(settings.API_KEY_MODEL, require_ready=False)
166+
except ValueError:
167+
raise ImproperlyConfigured(
168+
"API_KEY_MODEL must be of the form 'app_label.model_name'"
169+
)
170+
except LookupError:
171+
raise ImproperlyConfigured(
172+
"API_KEY_MODEL refers to model '%s' that has not been installed"
173+
% settings.API_KEY_MODEL
174+
)

src/rest_framework_api_key/permissions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from django.http import HttpRequest
55
from rest_framework import permissions
66

7-
from .models import AbstractAPIKey, APIKey
7+
from .models import AbstractAPIKey, get_api_key_model
88

99

1010
class KeyParser:
@@ -59,4 +59,4 @@ def has_object_permission(
5959

6060

6161
class HasAPIKey(BaseHasAPIKey):
62-
model = APIKey
62+
model = get_api_key_model()

test_project/project/settings.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,8 @@
8282
# Static files (CSS, JavaScript, Images)
8383

8484
STATIC_URL = "/static/"
85+
86+
87+
# API keys
88+
89+
API_KEY_MODEL = "heroes.HeroAPIKey"

0 commit comments

Comments
 (0)