diff options
| author | Jamie Matthews | 2012-09-26 13:05:21 +0100 |
|---|---|---|
| committer | Jamie Matthews | 2012-09-26 13:05:21 +0100 |
| commit | 01770c53cd9045e6ea054f32b1e40b5d2ff7fe44 (patch) | |
| tree | 657cb66f92d78add3b2f587754387832043168e6 /djangorestframework/settings.py | |
| parent | f6488cb0589d3b11fb8d831e00d1389f3fff74b6 (diff) | |
| parent | 09a445b257532be69ffab69a3f62b84bfa90463d (diff) | |
| download | django-rest-framework-01770c53cd9045e6ea054f32b1e40b5d2ff7fe44.tar.bz2 | |
Merge branch 'restframework2' of git://github.com/tomchristie/django-rest-framework into improved-view-decorators
* 'restframework2' of git://github.com/tomchristie/django-rest-framework: (56 commits)
Bits of cleanup
Add request.QUERY_PARAMS
Add readonly 'id' field
Tweak browseable API
Don't display readonly fields
Fix some bits of serialization
Add csrf note
Fix incorrect bit of tutorial
Added tox.ini
Tweak media_type -> accepted_media_type. Need to document, but marginally less confusing
Tweak media_type -> accepted_media_type. Need to document, but marginally less confusing
Tweak media_type -> accepted_media_type. Need to document, but marginally less confusing
Clean up bits of templates etc
Hack out bunch of unneccesary private methods on View class
Clean up template tags
Remove dumbass __all__ variables
Remove old 'djangorestframework directories
Change package name: djangorestframework -> rest_framework
Dont strip final '/'
Use get_script_prefix to play nicely if not installed at the root.
...
Conflicts:
rest_framework/decorators.py
Diffstat (limited to 'djangorestframework/settings.py')
| -rw-r--r-- | djangorestframework/settings.py | 125 |
1 files changed, 0 insertions, 125 deletions
diff --git a/djangorestframework/settings.py b/djangorestframework/settings.py deleted file mode 100644 index e5181f4b..00000000 --- a/djangorestframework/settings.py +++ /dev/null @@ -1,125 +0,0 @@ -""" -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', - ) -} - -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 - - -DEFAULTS = { - 'DEFAULT_RENDERERS': ( - 'djangorestframework.renderers.JSONRenderer', - 'djangorestframework.renderers.JSONPRenderer', - 'djangorestframework.renderers.DocumentingHTMLRenderer', - 'djangorestframework.renderers.DocumentingPlainTextRenderer', - ), - 'DEFAULT_PARSERS': ( - 'djangorestframework.parsers.JSONParser', - 'djangorestframework.parsers.FormParser' - ), - 'DEFAULT_AUTHENTICATION': ( - 'djangorestframework.authentication.SessionAuthentication', - 'djangorestframework.authentication.UserBasicAuthentication' - ), - 'DEFAULT_PERMISSIONS': (), - 'DEFAULT_THROTTLES': (), - - 'UNAUTHENTICATED_USER': 'django.contrib.auth.models.AnonymousUser', - 'UNAUTHENTICATED_TOKEN': None, - - 'FORM_METHOD_OVERRIDE': '_method', - 'FORM_CONTENT_OVERRIDE': '_content', - 'FORM_CONTENTTYPE_OVERRIDE': '_content_type', - 'URL_ACCEPT_OVERRIDE': '_accept', - - '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', - 'UNAUTHENTICATED_USER', - 'UNAUTHENTICATED_TOKEN' -) - - -def perform_import(val, setting): - """ - If the given setting is a string import notation, - then perform the necessary import or imports. - """ - if val is None or setting not in IMPORT_STRINGS: - return val - - 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 - - -def import_from_string(val, setting): - """ - 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 Exception, e: - import traceback - tb = traceback.format_exc() - import pdb; pdb.set_trace() - msg = "Could not import '%s' for API setting '%s'" % (val, setting) - raise ImportError(msg) - - -class APISettings(object): - """ - 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 - - Any setting with string import paths will be automatically resolved - and return the class, rather than the string literal. - """ - def __getattr__(self, attr): - if attr not in DEFAULTS.keys(): - raise AttributeError("Invalid API setting: '%s'" % attr) - - try: - # Check if present in user settings - val = perform_import(settings.API_SETTINGS[attr], attr) - except (AttributeError, KeyError): - # Fall back to defaults - val = perform_import(DEFAULTS[attr], attr) - - # Cache the result - setattr(self, attr, val) - return val - -api_settings = APISettings() |
