File tree Expand file tree Collapse file tree 4 files changed +38
-7
lines changed
src/rest_framework_api_key Expand file tree Collapse file tree 4 files changed +38
-7
lines changed Original file line number Diff line number Diff line change 44from django .db import models
55from django .http .request import HttpRequest
66
7- from .models import AbstractAPIKey , APIKey
7+ from .models import AbstractAPIKey
88
99
1010class APIKeyModelAdmin (admin .ModelAdmin ):
@@ -55,6 +55,4 @@ def save_model(
5555 obj .save ()
5656
5757
58- admin .site .register (APIKey , APIKeyModelAdmin )
59-
6058APIKeyAdmin = APIKeyModelAdmin # Compatibility with <1.3
Original file line number Diff line number Diff line change 11import 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
46from django .db import models
57from 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+
145155class 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+ )
Original file line number Diff line number Diff line change 44from django .http import HttpRequest
55from rest_framework import permissions
66
7- from .models import AbstractAPIKey , APIKey
7+ from .models import AbstractAPIKey , get_api_key_model
88
99
1010class KeyParser :
@@ -59,4 +59,4 @@ def has_object_permission(
5959
6060
6161class HasAPIKey (BaseHasAPIKey ):
62- model = APIKey
62+ model = get_api_key_model ()
Original file line number Diff line number Diff line change 8282# Static files (CSS, JavaScript, Images)
8383
8484STATIC_URL = "/static/"
85+
86+
87+ # API keys
88+
89+ API_KEY_MODEL = "heroes.HeroAPIKey"
You can’t perform that action at this time.
0 commit comments