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 = {
|
|
|
|
'DEFAULT_RENDERERS': (
|
|
|
|
'rest_framework.renderers.JSONRenderer',
|
|
|
|
'rest_framework.renderers.YAMLRenderer',
|
|
|
|
)
|
|
|
|
'DEFAULT_PARSERS': (
|
|
|
|
'rest_framework.parsers.JSONParser',
|
|
|
|
'rest_framework.parsers.YAMLParser',
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
from django.conf import settings
|
|
|
|
from django.utils import importlib
|
|
|
|
|
|
|
|
|
2012-10-10 15:54:40 +04:00
|
|
|
USER_SETTINGS = getattr(settings, 'REST_FRAMEWORK', None)
|
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
DEFAULTS = {
|
|
|
|
'DEFAULT_RENDERERS': (
|
|
|
|
'rest_framework.renderers.JSONRenderer',
|
2012-10-09 18:58:48 +04:00
|
|
|
'rest_framework.renderers.BrowsableAPIRenderer',
|
2012-09-20 16:06:27 +04:00
|
|
|
),
|
|
|
|
'DEFAULT_PARSERS': (
|
|
|
|
'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'
|
2012-09-20 16:06:27 +04:00
|
|
|
),
|
|
|
|
'DEFAULT_AUTHENTICATION': (
|
|
|
|
'rest_framework.authentication.SessionAuthentication',
|
2012-10-15 16:27:50 +04:00
|
|
|
'rest_framework.authentication.BasicAuthentication'
|
2012-09-20 16:06:27 +04:00
|
|
|
),
|
|
|
|
'DEFAULT_PERMISSIONS': (),
|
|
|
|
'DEFAULT_THROTTLES': (),
|
2012-09-20 20:44:34 +04:00
|
|
|
'DEFAULT_CONTENT_NEGOTIATION':
|
|
|
|
'rest_framework.negotiation.DefaultContentNegotiation',
|
2012-09-27 00:47:19 +04:00
|
|
|
'DEFAULT_THROTTLE_RATES': {
|
|
|
|
'user': None,
|
|
|
|
'anon': None,
|
|
|
|
},
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2012-09-30 20:31:28 +04:00
|
|
|
'MODEL_SERIALIZER': 'rest_framework.serializers.ModelSerializer',
|
|
|
|
'PAGINATION_SERIALIZER': 'rest_framework.pagination.PaginationSerializer',
|
2012-10-04 14:26:41 +04:00
|
|
|
'PAGINATE_BY': None,
|
2012-09-30 20:31:28 +04:00
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
'UNAUTHENTICATED_USER': 'django.contrib.auth.models.AnonymousUser',
|
|
|
|
'UNAUTHENTICATED_TOKEN': None,
|
|
|
|
|
|
|
|
'FORM_METHOD_OVERRIDE': '_method',
|
|
|
|
'FORM_CONTENT_OVERRIDE': '_content',
|
|
|
|
'FORM_CONTENTTYPE_OVERRIDE': '_content_type',
|
2012-10-02 18:24:42 +04:00
|
|
|
'URL_ACCEPT_OVERRIDE': 'accept',
|
2012-09-20 16:06:27 +04:00
|
|
|
'URL_FORMAT_OVERRIDE': 'format',
|
|
|
|
|
|
|
|
'FORMAT_SUFFIX_KWARG': 'format'
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# List of settings that may be in string import notation.
|
|
|
|
IMPORT_STRINGS = (
|
|
|
|
'DEFAULT_RENDERERS',
|
|
|
|
'DEFAULT_PARSERS',
|
|
|
|
'DEFAULT_AUTHENTICATION',
|
|
|
|
'DEFAULT_PERMISSIONS',
|
|
|
|
'DEFAULT_THROTTLES',
|
|
|
|
'DEFAULT_CONTENT_NEGOTIATION',
|
2012-09-30 20:31:28 +04:00
|
|
|
'MODEL_SERIALIZER',
|
|
|
|
'PAGINATION_SERIALIZER',
|
2012-09-20 16:06:27 +04:00
|
|
|
'UNAUTHENTICATED_USER',
|
|
|
|
'UNAUTHENTICATED_TOKEN',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
if isinstance(val, basestring):
|
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:
|
|
|
|
# Nod to tastypie's use of importlib.
|
|
|
|
parts = val.split('.')
|
|
|
|
module_path, class_name = '.'.join(parts[:-1]), parts[-1]
|
|
|
|
module = importlib.import_module(module_path)
|
|
|
|
return getattr(module, class_name)
|
|
|
|
except:
|
2012-10-10 15:54:40 +04:00
|
|
|
msg = "Could not import '%s' for API setting '%s'" % (val, setting_name)
|
2012-09-20 16:06:27 +04:00
|
|
|
raise ImportError(msg)
|
|
|
|
|
|
|
|
|
|
|
|
class APISettings(object):
|
|
|
|
"""
|
|
|
|
A settings object, that allows API settings to be accessed as properties.
|
|
|
|
For example:
|
|
|
|
|
|
|
|
from rest_framework.settings import api_settings
|
|
|
|
print api_settings.DEFAULT_RENDERERS
|
|
|
|
|
|
|
|
Any setting with string import paths will be automatically resolved
|
|
|
|
and return the class, rather than the string literal.
|
|
|
|
"""
|
2012-10-10 15:54:40 +04:00
|
|
|
def __init__(self, user_settings=None, defaults=None, import_strings=None):
|
|
|
|
self.user_settings = user_settings or {}
|
|
|
|
self.defaults = defaults or {}
|
|
|
|
self.import_strings = import_strings or ()
|
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
def __getattr__(self, attr):
|
2012-10-10 15:54:40 +04:00
|
|
|
if attr not in self.defaults.keys():
|
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
|
|
|
|
if val and attr in self.import_strings:
|
|
|
|
val = perform_import(val, attr)
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
# Cache the result
|
|
|
|
setattr(self, attr, val)
|
|
|
|
return val
|
|
|
|
|
2012-10-10 15:54:40 +04:00
|
|
|
api_settings = APISettings(USER_SETTINGS, DEFAULTS, IMPORT_STRINGS)
|