Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
5c7f3e23
Commit
5c7f3e23
authored
Oct 10, 2012
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tidy up APISettings object to not rely on module state
parent
db6b5a99
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
13 deletions
+21
-13
rest_framework/settings.py
+21
-13
No files found.
rest_framework/settings.py
View file @
5c7f3e23
...
@@ -21,6 +21,8 @@ from django.conf import settings
...
@@ -21,6 +21,8 @@ from django.conf import settings
from
django.utils
import
importlib
from
django.utils
import
importlib
USER_SETTINGS
=
getattr
(
settings
,
'REST_FRAMEWORK'
,
None
)
DEFAULTS
=
{
DEFAULTS
=
{
'DEFAULT_RENDERERS'
:
(
'DEFAULT_RENDERERS'
:
(
'rest_framework.renderers.JSONRenderer'
,
'rest_framework.renderers.JSONRenderer'
,
...
@@ -76,22 +78,19 @@ IMPORT_STRINGS = (
...
@@ -76,22 +78,19 @@ IMPORT_STRINGS = (
)
)
def
perform_import
(
val
,
setting
):
def
perform_import
(
val
,
setting
_name
):
"""
"""
If the given setting is a string import notation,
If the given setting is a string import notation,
then perform the necessary import or imports.
then perform the necessary import or imports.
"""
"""
if
val
is
None
or
not
setting
in
IMPORT_STRINGS
:
return
val
if
isinstance
(
val
,
basestring
):
if
isinstance
(
val
,
basestring
):
return
import_from_string
(
val
,
setting
)
return
import_from_string
(
val
,
setting
_name
)
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
,
setting
_name
)
for
item
in
val
]
return
val
return
val
def
import_from_string
(
val
,
setting
):
def
import_from_string
(
val
,
setting
_name
):
"""
"""
Attempt to import a class from a string representation.
Attempt to import a class from a string representation.
"""
"""
...
@@ -102,7 +101,7 @@ def import_from_string(val, setting):
...
@@ -102,7 +101,7 @@ def import_from_string(val, setting):
module
=
importlib
.
import_module
(
module_path
)
module
=
importlib
.
import_module
(
module_path
)
return
getattr
(
module
,
class_name
)
return
getattr
(
module
,
class_name
)
except
:
except
:
msg
=
"Could not import '
%
s' for API setting '
%
s'"
%
(
val
,
setting
)
msg
=
"Could not import '
%
s' for API setting '
%
s'"
%
(
val
,
setting
_name
)
raise
ImportError
(
msg
)
raise
ImportError
(
msg
)
...
@@ -117,19 +116,28 @@ class APISettings(object):
...
@@ -117,19 +116,28 @@ class APISettings(object):
Any setting with string import paths will be automatically resolved
Any setting with string import paths will be automatically resolved
and return the class, rather than the string literal.
and return the class, rather than the string literal.
"""
"""
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
()
def
__getattr__
(
self
,
attr
):
def
__getattr__
(
self
,
attr
):
if
attr
not
in
DEFAULTS
.
keys
():
if
attr
not
in
self
.
defaults
.
keys
():
raise
AttributeError
(
"Invalid API setting: '
%
s'"
%
attr
)
raise
AttributeError
(
"Invalid API setting: '
%
s'"
%
attr
)
try
:
try
:
# Check if present in user settings
# Check if present in user settings
val
=
perform_import
(
settings
.
REST_FRAMEWORK
[
attr
],
attr
)
val
=
self
.
user_settings
[
attr
]
except
(
AttributeError
,
KeyError
)
:
except
KeyError
:
# Fall back to defaults
# Fall back to defaults
val
=
perform_import
(
DEFAULTS
[
attr
],
attr
)
val
=
self
.
defaults
[
attr
]
# Coerce import strings into classes
if
val
and
attr
in
self
.
import_strings
:
val
=
perform_import
(
val
,
attr
)
# Cache the result
# Cache the result
setattr
(
self
,
attr
,
val
)
setattr
(
self
,
attr
,
val
)
return
val
return
val
api_settings
=
APISettings
()
api_settings
=
APISettings
(
USER_SETTINGS
,
DEFAULTS
,
IMPORT_STRINGS
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment