Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
itminedu
synnefo
Commits
a8883097
Commit
a8883097
authored
Jun 18, 2013
by
Kostas Papadimitriou
Browse files
django-lib: api_method decorator improvements
allow API views to define supported serializations.
parent
c148b295
Changes
1
Hide whitespace changes
Inline
Side-by-side
snf-django-lib/snf_django/lib/api/__init__.py
View file @
a8883097
...
...
@@ -62,18 +62,30 @@ def get_token(request):
def
api_method
(
http_method
=
None
,
token_required
=
True
,
user_required
=
True
,
logger
=
None
,
format_allowed
=
True
,
astakos_url
=
None
,
defaul
t_ser
ia
lization
=
"json"
):
serializations
=
None
,
stric
t_serlization
=
False
):
"""Decorator function for views that implement an API method."""
if
not
logger
:
logger
=
log
serializations
=
serializations
or
[
'json'
,
'xml'
]
def
decorator
(
func
):
@
wraps
(
func
)
def
wrapper
(
request
,
*
args
,
**
kwargs
):
try
:
# Get the requested serialization format
request
.
serialization
=
get_serialization
(
request
,
format_allowed
,
default_serialization
)
serialization
=
get_serialization
(
request
,
format_allowed
,
'json'
)
# If guessed serialization is not supported, fallback to
# the default serialization or return an API error in case
# strict serialization flag is set.
if
not
serialization
in
serializations
:
if
strict_serlization
:
raise
faults
.
BadRequest
((
"%s serialization not "
"supported"
)
%
serialization
)
serialization
=
serializations
[
0
]
request
.
serialization
=
serialization
# Check HTTP method
if
http_method
and
request
.
method
!=
http_method
:
...
...
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