2012-09-03 18:57:43 +04:00
|
|
|
"""
|
|
|
|
Settings for REST framework are all namespaced in the API_SETTINGS setting.
|
|
|
|
For example your project's `settings.py` file might look like this:
|
|
|
|
|
|
|
|
API_SETTINGS = {
|
|
|
|
'DEFAULT_RENDERERS': (
|
|
|
|
'djangorestframework.renderers.JSONRenderer',
|
|
|
|
'djangorestframework.renderers.YAMLRenderer',
|
|
|
|
)
|
|
|
|
'DEFAULT_PARSERS': (
|
|
|
|
'djangorestframework.parsers.JSONParser',
|
|
|
|
'djangorestframework.parsers.YAMLParser',
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2012-09-07 12:36:52 +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.
|
2012-09-03 18:57:43 +04:00
|
|
|
"""
|
|
|
|
from django.conf import settings
|
2012-09-06 18:42:54 +04:00
|
|
|
from django.utils import importlib
|
2012-09-03 18:57:43 +04:00
|
|
|
|
|
|
|
|
|
|
|
DEFAULTS = {
|
|
|
|
'DEFAULT_RENDERERS': (
|
2012-09-06 18:42:54 +04:00
|
|
|
'djangorestframework.renderers.JSONRenderer',
|
|
|
|
'djangorestframework.renderers.JSONPRenderer',
|
2012-09-07 13:17:47 +04:00
|
|
|
'djangorestframework.renderers.DocumentingHTMLRenderer',
|
2012-09-06 18:42:54 +04:00
|
|
|
'djangorestframework.renderers.DocumentingPlainTextRenderer',
|
|
|
|
),
|
|
|
|
'DEFAULT_PARSERS': (
|
|
|
|
'djangorestframework.parsers.JSONParser',
|
|
|
|
'djangorestframework.parsers.FormParser'
|
|
|
|
),
|
2012-09-06 18:57:16 +04:00
|
|
|
'DEFAULT_AUTHENTICATION': (
|
|
|
|
'djangorestframework.authentication.SessionAuthentication',
|
|
|
|
'djangorestframework.authentication.UserBasicAuthentication'
|
|
|
|
),
|
|
|
|
'DEFAULT_PERMISSIONS': (),
|
|
|
|
'DEFAULT_THROTTLES': (),
|
2012-09-15 01:42:29 +04:00
|
|
|
'DEFAULT_CONTENT_NEGOTIATION': 'djangorestframework.contentnegotiation.DefaultContentNegotiation',
|
2012-09-07 12:36:52 +04:00
|
|
|
|
2012-09-06 18:42:54 +04:00
|
|
|
'UNAUTHENTICATED_USER': 'django.contrib.auth.models.AnonymousUser',
|
2012-09-06 19:28:11 +04:00
|
|
|
'UNAUTHENTICATED_TOKEN': None,
|
2012-09-07 12:36:52 +04:00
|
|
|
|
2012-09-06 19:28:11 +04:00
|
|
|
'FORM_METHOD_OVERRIDE': '_method',
|
|
|
|
'FORM_CONTENT_OVERRIDE': '_content',
|
|
|
|
'FORM_CONTENTTYPE_OVERRIDE': '_content_type',
|
2012-09-15 01:42:29 +04:00
|
|
|
'URL_ACCEPT_OVERRIDE': 'accept',
|
|
|
|
'IGNORE_MSIE_ACCEPT_HEADER': True,
|
2012-09-07 13:17:47 +04:00
|
|
|
|
|
|
|
'FORMAT_SUFFIX_KWARG': 'format'
|
2012-09-03 18:57:43 +04:00
|
|
|
}
|
|
|
|
|
2012-09-06 18:42:54 +04:00
|
|
|
|
|
|
|
# List of settings that may be in string import notation.
|
|
|
|
IMPORT_STRINGS = (
|
|
|
|
'DEFAULT_RENDERERS',
|
2012-09-06 18:57:16 +04:00
|
|
|
'DEFAULT_PARSERS',
|
|
|
|
'DEFAULT_AUTHENTICATION',
|
|
|
|
'DEFAULT_PERMISSIONS',
|
|
|
|
'DEFAULT_THROTTLES',
|
2012-09-15 01:42:29 +04:00
|
|
|
'DEFAULT_CONTENT_NEGOTIATION',
|
2012-09-06 18:42:54 +04:00
|
|
|
'UNAUTHENTICATED_USER',
|
2012-09-15 01:42:29 +04:00
|
|
|
'UNAUTHENTICATED_TOKEN',
|
2012-09-06 18:42:54 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def perform_import(val, setting):
|
|
|
|
"""
|
|
|
|
If the given setting is a string import notation,
|
|
|
|
then perform the necessary import or imports.
|
|
|
|
"""
|
2012-09-15 01:42:29 +04:00
|
|
|
if val is None or not setting in IMPORT_STRINGS:
|
2012-09-06 18:42:54 +04:00
|
|
|
return val
|
|
|
|
|
2012-09-06 18:57:16 +04:00
|
|
|
if isinstance(val, basestring):
|
|
|
|
return import_from_string(val, setting)
|
|
|
|
elif isinstance(val, (list, tuple)):
|
|
|
|
return [import_from_string(item, setting) for item in val]
|
|
|
|
return val
|
2012-09-06 18:42:54 +04:00
|
|
|
|
|
|
|
|
2012-09-06 18:57:16 +04:00
|
|
|
def import_from_string(val, setting):
|
2012-09-06 18:42:54 +04:00
|
|
|
"""
|
|
|
|
Attempt to import a class from a string representation.
|
|
|
|
"""
|
2012-09-06 18:57:16 +04:00
|
|
|
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)
|
2012-09-15 01:42:29 +04:00
|
|
|
except:
|
2012-09-06 18:57:16 +04:00
|
|
|
msg = "Could not import '%s' for API setting '%s'" % (val, setting)
|
|
|
|
raise ImportError(msg)
|
2012-09-03 18:57:43 +04:00
|
|
|
|
|
|
|
|
|
|
|
class APISettings(object):
|
2012-09-06 18:42:54 +04:00
|
|
|
"""
|
|
|
|
A settings object, that allows API settings to be accessed as properties.
|
|
|
|
For example:
|
|
|
|
|
|
|
|
from djangorestframework.settings import api_settings
|
|
|
|
print api_settings.DEFAULT_RENDERERS
|
|
|
|
|
2012-09-07 12:36:52 +04:00
|
|
|
Any setting with string import paths will be automatically resolved
|
|
|
|
and return the class, rather than the string literal.
|
2012-09-06 18:42:54 +04:00
|
|
|
"""
|
2012-09-03 18:57:43 +04:00
|
|
|
def __getattr__(self, attr):
|
2012-09-06 18:42:54 +04:00
|
|
|
if attr not in DEFAULTS.keys():
|
|
|
|
raise AttributeError("Invalid API setting: '%s'" % attr)
|
|
|
|
|
2012-09-03 18:57:43 +04:00
|
|
|
try:
|
2012-09-06 18:42:54 +04:00
|
|
|
# Check if present in user settings
|
|
|
|
val = perform_import(settings.API_SETTINGS[attr], attr)
|
2012-09-03 18:57:43 +04:00
|
|
|
except (AttributeError, KeyError):
|
2012-09-06 18:42:54 +04:00
|
|
|
# Fall back to defaults
|
|
|
|
val = perform_import(DEFAULTS[attr], attr)
|
|
|
|
|
|
|
|
# Cache the result
|
|
|
|
setattr(self, attr, val)
|
|
|
|
return val
|
2012-09-03 18:57:43 +04:00
|
|
|
|
|
|
|
api_settings = APISettings()
|