Add more settings to settings.py

This commit is contained in:
Tom Christie 2012-09-06 15:57:16 +01:00
parent 74c50b9535
commit c707034649
2 changed files with 26 additions and 22 deletions

View File

@ -29,6 +29,12 @@ DEFAULTS = {
'djangorestframework.parsers.JSONParser', 'djangorestframework.parsers.JSONParser',
'djangorestframework.parsers.FormParser' 'djangorestframework.parsers.FormParser'
), ),
'DEFAULT_AUTHENTICATION': (
'djangorestframework.authentication.SessionAuthentication',
'djangorestframework.authentication.UserBasicAuthentication'
),
'DEFAULT_PERMISSIONS': (),
'DEFAULT_THROTTLES': (),
'UNAUTHENTICATED_USER': 'django.contrib.auth.models.AnonymousUser', 'UNAUTHENTICATED_USER': 'django.contrib.auth.models.AnonymousUser',
'UNAUTHENTICATED_TOKEN': None 'UNAUTHENTICATED_TOKEN': None
} }
@ -40,6 +46,10 @@ if yaml:
# List of settings that may be in string import notation. # List of settings that may be in string import notation.
IMPORT_STRINGS = ( IMPORT_STRINGS = (
'DEFAULT_RENDERERS', 'DEFAULT_RENDERERS',
'DEFAULT_PARSERS',
'DEFAULT_AUTHENTICATION',
'DEFAULT_PERMISSIONS',
'DEFAULT_THROTTLES',
'UNAUTHENTICATED_USER', 'UNAUTHENTICATED_USER',
'UNAUTHENTICATED_TOKEN' 'UNAUTHENTICATED_TOKEN'
) )
@ -53,26 +63,26 @@ def perform_import(val, setting):
if val is None or setting not in IMPORT_STRINGS: if val is None or setting not in IMPORT_STRINGS:
return val return val
try: if isinstance(val, basestring):
if isinstance(val, basestring): return import_from_string(val, setting)
return import_from_string(val) elif isinstance(val, (list, tuple)):
elif isinstance(val, (list, tuple)): return [import_from_string(item, setting) for item in val]
return [import_from_string(item) for item in val] return val
return val
except:
msg = "Could not import '%s' for API setting '%s'" % (val, setting)
raise ImportError(msg)
def import_from_string(val): def import_from_string(val, setting):
""" """
Attempt to import a class from a string representation. Attempt to import a class from a string representation.
""" """
# Nod to tastypie's use of importlib. try:
parts = val.split('.') # Nod to tastypie's use of importlib.
module_path, class_name = '.'.join(parts[:-1]), parts[-1] parts = val.split('.')
module = importlib.import_module(module_path) module_path, class_name = '.'.join(parts[:-1]), parts[-1]
return getattr(module, class_name) module = importlib.import_module(module_path)
return getattr(module, class_name)
except:
msg = "Could not import '%s' for API setting '%s'" % (val, setting)
raise ImportError(msg)
class APISettings(object): class APISettings(object):

View File

@ -40,19 +40,13 @@ Default:
A list or tuple of authentication classes, that determines the default set of authenticators used when accessing the `request.user` or `request.auth` properties. A list or tuple of authentication classes, that determines the default set of authenticators used when accessing the `request.user` or `request.auth` properties.
Default if `DEBUG` is `True`: Default:
( (
'djangorestframework.authentication.SessionAuthentication', 'djangorestframework.authentication.SessionAuthentication',
'djangorestframework.authentication.UserBasicAuthentication' 'djangorestframework.authentication.UserBasicAuthentication'
) )
Default if `DEBUG` is `False`:
(
'djangorestframework.authentication.SessionAuthentication',
)
## DEFAULT_PERMISSIONS ## DEFAULT_PERMISSIONS
A list or tuple of permission classes, that determines the default set of permissions checked at the start of a view. A list or tuple of permission classes, that determines the default set of permissions checked at the start of a view.