2012-09-20 16:06:27 +04:00
|
|
|
"""
|
|
|
|
Settings for REST framework are all namespaced in the REST_FRAMEWORK setting.
|
|
|
|
For example your project's `settings.py` file might look like this:
|
|
|
|
|
|
|
|
REST_FRAMEWORK = {
|
2019-07-15 13:54:46 +03:00
|
|
|
'DEFAULT_RENDERER_CLASSES': [
|
2012-09-20 16:06:27 +04:00
|
|
|
'rest_framework.renderers.JSONRenderer',
|
2014-11-29 21:43:05 +03:00
|
|
|
'rest_framework.renderers.TemplateHTMLRenderer',
|
2019-07-15 13:54:46 +03:00
|
|
|
],
|
|
|
|
'DEFAULT_PARSER_CLASSES': [
|
2012-09-20 16:06:27 +04:00
|
|
|
'rest_framework.parsers.JSONParser',
|
2017-03-29 08:57:14 +03:00
|
|
|
'rest_framework.parsers.FormParser',
|
2019-07-15 13:54:46 +03:00
|
|
|
'rest_framework.parsers.MultiPartParser',
|
|
|
|
],
|
2012-09-20 16:06:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
This module provides the `api_setting` object, that is used to access
|
|
|
|
REST framework settings, checking for user settings first, then falling
|
|
|
|
back to the defaults.
|
|
|
|
"""
|
2015-06-18 16:38:29 +03:00
|
|
|
from django.conf import settings
|
2022-10-14 18:30:26 +03:00
|
|
|
# Import from `django.core.signals` instead of the official location
|
|
|
|
# `django.test.signals` to avoid importing the test module unnecessarily.
|
|
|
|
from django.core.signals import setting_changed
|
2019-05-02 01:32:28 +03:00
|
|
|
from django.utils.module_loading import import_string
|
2015-06-18 16:38:29 +03:00
|
|
|
|
2022-06-24 11:59:20 +03:00
|
|
|
from rest_framework import ISO_8601, STD_DURATION
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
DEFAULTS = {
|
2013-05-07 16:00:44 +04:00
|
|
|
# Base API policies
|
2019-07-15 13:54:46 +03:00
|
|
|
'DEFAULT_RENDERER_CLASSES': [
|
2012-09-20 16:06:27 +04:00
|
|
|
'rest_framework.renderers.JSONRenderer',
|
2012-10-09 18:58:48 +04:00
|
|
|
'rest_framework.renderers.BrowsableAPIRenderer',
|
2019-07-15 13:54:46 +03:00
|
|
|
],
|
|
|
|
'DEFAULT_PARSER_CLASSES': [
|
2012-09-20 16:06:27 +04:00
|
|
|
'rest_framework.parsers.JSONParser',
|
2012-09-26 16:09:20 +04:00
|
|
|
'rest_framework.parsers.FormParser',
|
2012-09-26 15:40:11 +04:00
|
|
|
'rest_framework.parsers.MultiPartParser'
|
2019-07-15 13:54:46 +03:00
|
|
|
],
|
|
|
|
'DEFAULT_AUTHENTICATION_CLASSES': [
|
2012-09-20 16:06:27 +04:00
|
|
|
'rest_framework.authentication.SessionAuthentication',
|
2012-10-15 16:27:50 +04:00
|
|
|
'rest_framework.authentication.BasicAuthentication'
|
2019-07-15 13:54:46 +03:00
|
|
|
],
|
|
|
|
'DEFAULT_PERMISSION_CLASSES': [
|
2012-10-27 23:04:33 +04:00
|
|
|
'rest_framework.permissions.AllowAny',
|
2019-07-15 13:54:46 +03:00
|
|
|
],
|
|
|
|
'DEFAULT_THROTTLE_CLASSES': [],
|
2014-08-19 16:28:07 +04:00
|
|
|
'DEFAULT_CONTENT_NEGOTIATION_CLASS': 'rest_framework.negotiation.DefaultContentNegotiation',
|
2014-09-24 17:09:49 +04:00
|
|
|
'DEFAULT_METADATA_CLASS': 'rest_framework.metadata.SimpleMetadata',
|
2014-12-16 18:34:19 +03:00
|
|
|
'DEFAULT_VERSIONING_CLASS': None,
|
2013-05-07 16:00:44 +04:00
|
|
|
|
2014-12-05 02:29:28 +03:00
|
|
|
# Generic view behavior
|
2017-09-25 16:36:31 +03:00
|
|
|
'DEFAULT_PAGINATION_CLASS': None,
|
2019-07-15 13:54:46 +03:00
|
|
|
'DEFAULT_FILTER_BACKENDS': [],
|
2012-10-18 02:09:11 +04:00
|
|
|
|
2017-12-14 13:24:21 +03:00
|
|
|
# Schema
|
2019-05-13 17:07:03 +03:00
|
|
|
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.openapi.AutoSchema',
|
2017-12-14 13:24:21 +03:00
|
|
|
|
2013-05-07 16:00:44 +04:00
|
|
|
# Throttling
|
2012-09-27 00:47:19 +04:00
|
|
|
'DEFAULT_THROTTLE_RATES': {
|
|
|
|
'user': None,
|
|
|
|
'anon': None,
|
|
|
|
},
|
2013-12-07 02:21:52 +04:00
|
|
|
'NUM_PROXIES': None,
|
2012-11-17 02:45:57 +04:00
|
|
|
|
|
|
|
# Pagination
|
2015-03-04 18:51:00 +03:00
|
|
|
'PAGE_SIZE': None,
|
2012-11-17 02:45:57 +04:00
|
|
|
|
2014-03-07 01:39:44 +04:00
|
|
|
# Filtering
|
|
|
|
'SEARCH_PARAM': 'search',
|
|
|
|
'ORDERING_PARAM': 'ordering',
|
|
|
|
|
2014-12-17 15:41:46 +03:00
|
|
|
# Versioning
|
|
|
|
'DEFAULT_VERSION': None,
|
|
|
|
'ALLOWED_VERSIONS': None,
|
|
|
|
'VERSION_PARAM': 'version',
|
|
|
|
|
2012-11-17 02:45:57 +04:00
|
|
|
# Authentication
|
2012-09-20 16:06:27 +04:00
|
|
|
'UNAUTHENTICATED_USER': 'django.contrib.auth.models.AnonymousUser',
|
|
|
|
'UNAUTHENTICATED_TOKEN': None,
|
|
|
|
|
2013-08-27 15:32:13 +04:00
|
|
|
# View configuration
|
|
|
|
'VIEW_NAME_FUNCTION': 'rest_framework.views.get_view_name',
|
|
|
|
'VIEW_DESCRIPTION_FUNCTION': 'rest_framework.views.get_view_description',
|
|
|
|
|
2013-09-06 20:40:34 +04:00
|
|
|
# Exception handling
|
|
|
|
'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler',
|
2014-09-12 13:21:35 +04:00
|
|
|
'NON_FIELD_ERRORS_KEY': 'non_field_errors',
|
2013-09-06 20:40:34 +04:00
|
|
|
|
2013-07-01 16:59:05 +04:00
|
|
|
# Testing
|
2019-07-15 13:54:46 +03:00
|
|
|
'TEST_REQUEST_RENDERER_CLASSES': [
|
2013-07-01 16:59:05 +04:00
|
|
|
'rest_framework.renderers.MultiPartRenderer',
|
|
|
|
'rest_framework.renderers.JSONRenderer'
|
2019-07-15 13:54:46 +03:00
|
|
|
],
|
2013-07-01 16:59:05 +04:00
|
|
|
'TEST_REQUEST_DEFAULT_FORMAT': 'multipart',
|
|
|
|
|
2015-09-28 17:33:55 +03:00
|
|
|
# Hyperlink settings
|
2012-09-20 16:06:27 +04:00
|
|
|
'URL_FORMAT_OVERRIDE': 'format',
|
2012-11-15 14:15:05 +04:00
|
|
|
'FORMAT_SUFFIX_KWARG': 'format',
|
2014-01-14 12:38:49 +04:00
|
|
|
'URL_FIELD_NAME': 'url',
|
2013-03-01 16:16:00 +04:00
|
|
|
|
|
|
|
# Input and output formats
|
2014-09-12 15:10:22 +04:00
|
|
|
'DATE_FORMAT': ISO_8601,
|
2019-07-15 13:54:46 +03:00
|
|
|
'DATE_INPUT_FORMATS': [ISO_8601],
|
2013-03-01 16:16:00 +04:00
|
|
|
|
2014-09-12 15:10:22 +04:00
|
|
|
'DATETIME_FORMAT': ISO_8601,
|
2019-07-15 13:54:46 +03:00
|
|
|
'DATETIME_INPUT_FORMATS': [ISO_8601],
|
2013-03-01 16:16:00 +04:00
|
|
|
|
2014-09-12 15:10:22 +04:00
|
|
|
'TIME_FORMAT': ISO_8601,
|
2019-07-15 13:54:46 +03:00
|
|
|
'TIME_INPUT_FORMATS': [ISO_8601],
|
2013-05-07 16:00:44 +04:00
|
|
|
|
2022-06-24 11:59:20 +03:00
|
|
|
'DURATION_FORMAT': STD_DURATION,
|
|
|
|
|
2014-09-12 14:38:22 +04:00
|
|
|
# Encoding
|
|
|
|
'UNICODE_JSON': True,
|
2014-09-12 15:10:22 +04:00
|
|
|
'COMPACT_JSON': True,
|
2017-07-10 22:23:12 +03:00
|
|
|
'STRICT_JSON': True,
|
2014-09-26 20:06:20 +04:00
|
|
|
'COERCE_DECIMAL_TO_STRING': True,
|
2015-03-04 18:51:00 +03:00
|
|
|
'UPLOADED_FILES_USE_URL': True,
|
2016-10-10 15:03:46 +03:00
|
|
|
|
2024-01-25 01:17:01 +03:00
|
|
|
# Browsable API
|
2016-10-10 15:03:46 +03:00
|
|
|
'HTML_SELECT_CUTOFF': 1000,
|
|
|
|
'HTML_SELECT_CUTOFF_TEXT': "More than {count} items...",
|
|
|
|
|
|
|
|
# Schemas
|
|
|
|
'SCHEMA_COERCE_PATH_PK': True,
|
|
|
|
'SCHEMA_COERCE_METHOD_NAMES': {
|
|
|
|
'retrieve': 'read',
|
|
|
|
'destroy': 'delete'
|
|
|
|
},
|
2012-09-20 16:06:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# List of settings that may be in string import notation.
|
2019-07-15 13:54:46 +03:00
|
|
|
IMPORT_STRINGS = [
|
2012-10-18 02:09:11 +04:00
|
|
|
'DEFAULT_RENDERER_CLASSES',
|
|
|
|
'DEFAULT_PARSER_CLASSES',
|
|
|
|
'DEFAULT_AUTHENTICATION_CLASSES',
|
|
|
|
'DEFAULT_PERMISSION_CLASSES',
|
|
|
|
'DEFAULT_THROTTLE_CLASSES',
|
|
|
|
'DEFAULT_CONTENT_NEGOTIATION_CLASS',
|
2014-09-24 17:09:49 +04:00
|
|
|
'DEFAULT_METADATA_CLASS',
|
2014-12-16 18:34:19 +03:00
|
|
|
'DEFAULT_VERSIONING_CLASS',
|
2015-01-09 18:30:36 +03:00
|
|
|
'DEFAULT_PAGINATION_CLASS',
|
2013-05-07 16:00:44 +04:00
|
|
|
'DEFAULT_FILTER_BACKENDS',
|
2017-12-14 13:24:21 +03:00
|
|
|
'DEFAULT_SCHEMA_CLASS',
|
2013-09-06 20:40:34 +04:00
|
|
|
'EXCEPTION_HANDLER',
|
2013-07-01 16:59:05 +04:00
|
|
|
'TEST_REQUEST_RENDERER_CLASSES',
|
2012-09-20 16:06:27 +04:00
|
|
|
'UNAUTHENTICATED_USER',
|
|
|
|
'UNAUTHENTICATED_TOKEN',
|
2013-08-16 21:23:04 +04:00
|
|
|
'VIEW_NAME_FUNCTION',
|
|
|
|
'VIEW_DESCRIPTION_FUNCTION'
|
2019-07-15 13:54:46 +03:00
|
|
|
]
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
2015-12-08 08:19:24 +03:00
|
|
|
# List of settings that have been removed
|
2019-07-15 13:54:46 +03:00
|
|
|
REMOVED_SETTINGS = [
|
|
|
|
'PAGINATE_BY', 'PAGINATE_BY_PARAM', 'MAX_PAGINATE_BY',
|
|
|
|
]
|
2015-12-08 08:19:24 +03:00
|
|
|
|
|
|
|
|
2012-10-10 15:54:40 +04:00
|
|
|
def perform_import(val, setting_name):
|
2012-09-20 16:06:27 +04:00
|
|
|
"""
|
|
|
|
If the given setting is a string import notation,
|
|
|
|
then perform the necessary import or imports.
|
|
|
|
"""
|
2014-12-16 18:34:19 +03:00
|
|
|
if val is None:
|
|
|
|
return None
|
2019-04-30 18:53:44 +03:00
|
|
|
elif isinstance(val, str):
|
2012-10-10 15:54:40 +04:00
|
|
|
return import_from_string(val, setting_name)
|
2012-09-20 16:06:27 +04:00
|
|
|
elif isinstance(val, (list, tuple)):
|
2012-10-10 15:54:40 +04:00
|
|
|
return [import_from_string(item, setting_name) for item in val]
|
2012-09-20 16:06:27 +04:00
|
|
|
return val
|
|
|
|
|
|
|
|
|
2012-10-10 15:54:40 +04:00
|
|
|
def import_from_string(val, setting_name):
|
2012-09-20 16:06:27 +04:00
|
|
|
"""
|
|
|
|
Attempt to import a class from a string representation.
|
|
|
|
"""
|
|
|
|
try:
|
2019-05-02 01:32:28 +03:00
|
|
|
return import_string(val)
|
|
|
|
except ImportError as e:
|
2013-01-07 16:52:20 +04:00
|
|
|
msg = "Could not import '%s' for API setting '%s'. %s: %s." % (val, setting_name, e.__class__.__name__, e)
|
2012-09-20 16:06:27 +04:00
|
|
|
raise ImportError(msg)
|
|
|
|
|
|
|
|
|
2019-04-30 18:53:44 +03:00
|
|
|
class APISettings:
|
2012-09-20 16:06:27 +04:00
|
|
|
"""
|
2020-01-15 13:52:29 +03:00
|
|
|
A settings object that allows REST Framework settings to be accessed as
|
|
|
|
properties. For example:
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
from rest_framework.settings import api_settings
|
2015-01-08 18:38:27 +03:00
|
|
|
print(api_settings.DEFAULT_RENDERER_CLASSES)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
Any setting with string import paths will be automatically resolved
|
|
|
|
and return the class, rather than the string literal.
|
2020-01-15 13:52:29 +03:00
|
|
|
|
|
|
|
Note:
|
|
|
|
This is an internal class that is only compatible with settings namespaced
|
|
|
|
under the REST_FRAMEWORK name. It is not intended to be used by 3rd-party
|
|
|
|
apps, and test helpers like `override_settings` may not work as expected.
|
2012-09-20 16:06:27 +04:00
|
|
|
"""
|
2012-10-10 15:54:40 +04:00
|
|
|
def __init__(self, user_settings=None, defaults=None, import_strings=None):
|
2015-09-03 18:24:13 +03:00
|
|
|
if user_settings:
|
2015-12-08 06:45:44 +03:00
|
|
|
self._user_settings = self.__check_user_settings(user_settings)
|
2014-12-15 16:18:39 +03:00
|
|
|
self.defaults = defaults or DEFAULTS
|
|
|
|
self.import_strings = import_strings or IMPORT_STRINGS
|
2017-12-14 12:33:48 +03:00
|
|
|
self._cached_attrs = set()
|
2012-10-10 15:54:40 +04:00
|
|
|
|
2015-09-03 18:24:13 +03:00
|
|
|
@property
|
|
|
|
def user_settings(self):
|
|
|
|
if not hasattr(self, '_user_settings'):
|
|
|
|
self._user_settings = getattr(settings, 'REST_FRAMEWORK', {})
|
|
|
|
return self._user_settings
|
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
def __getattr__(self, attr):
|
2016-01-07 00:51:11 +03:00
|
|
|
if attr not in self.defaults:
|
2012-09-20 16:06:27 +04:00
|
|
|
raise AttributeError("Invalid API setting: '%s'" % attr)
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Check if present in user settings
|
2012-10-10 15:54:40 +04:00
|
|
|
val = self.user_settings[attr]
|
|
|
|
except KeyError:
|
2012-09-20 16:06:27 +04:00
|
|
|
# Fall back to defaults
|
2012-10-10 15:54:40 +04:00
|
|
|
val = self.defaults[attr]
|
|
|
|
|
|
|
|
# Coerce import strings into classes
|
2015-07-02 12:28:46 +03:00
|
|
|
if attr in self.import_strings:
|
2012-10-10 15:54:40 +04:00
|
|
|
val = perform_import(val, attr)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
# Cache the result
|
2017-12-14 12:33:48 +03:00
|
|
|
self._cached_attrs.add(attr)
|
2012-09-20 16:06:27 +04:00
|
|
|
setattr(self, attr, val)
|
|
|
|
return val
|
|
|
|
|
2015-12-08 06:45:44 +03:00
|
|
|
def __check_user_settings(self, user_settings):
|
2018-10-02 09:28:58 +03:00
|
|
|
SETTINGS_DOC = "https://www.django-rest-framework.org/api-guide/settings/"
|
2015-12-08 08:19:24 +03:00
|
|
|
for setting in REMOVED_SETTINGS:
|
2015-12-08 06:45:44 +03:00
|
|
|
if setting in user_settings:
|
2016-07-13 18:30:41 +03:00
|
|
|
raise RuntimeError("The '%s' setting has been removed. Please refer to '%s' for available settings." % (setting, SETTINGS_DOC))
|
2015-12-08 06:53:33 +03:00
|
|
|
return user_settings
|
2015-12-08 06:45:44 +03:00
|
|
|
|
2017-12-14 12:33:48 +03:00
|
|
|
def reload(self):
|
|
|
|
for attr in self._cached_attrs:
|
|
|
|
delattr(self, attr)
|
|
|
|
self._cached_attrs.clear()
|
|
|
|
if hasattr(self, '_user_settings'):
|
|
|
|
delattr(self, '_user_settings')
|
|
|
|
|
2012-11-09 01:46:53 +04:00
|
|
|
|
2015-09-03 18:24:13 +03:00
|
|
|
api_settings = APISettings(None, DEFAULTS, IMPORT_STRINGS)
|
2015-01-27 16:51:30 +03:00
|
|
|
|
|
|
|
|
|
|
|
def reload_api_settings(*args, **kwargs):
|
2017-12-14 12:33:48 +03:00
|
|
|
setting = kwargs['setting']
|
2015-01-27 16:51:30 +03:00
|
|
|
if setting == 'REST_FRAMEWORK':
|
2017-12-14 12:33:48 +03:00
|
|
|
api_settings.reload()
|
2015-01-27 16:51:30 +03:00
|
|
|
|
|
|
|
|
|
|
|
setting_changed.connect(reload_api_settings)
|