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
f6488cb0
Commit
f6488cb0
authored
Sep 14, 2012
by
Jamie Matthews
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move logic for attaching HTTP handlers into LazyViewCreator
parent
21b1116a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
32 deletions
+19
-32
djangorestframework/decorators.py
+19
-32
No files found.
djangorestframework/decorators.py
View file @
f6488cb0
...
...
@@ -16,11 +16,14 @@ class LazyViewCreator(object):
This is done so that the ordering of stacked decorators is irrelevant.
"""
def
__init__
(
self
):
def
__init__
(
self
,
wrapped_view
):
self
.
wrapped_view
=
wrapped_view
# Each item in this dictionary will be copied onto the final
# class-based view that gets created when this object is called
self
.
final_view_attrs
=
{
'http_method_names'
:
APIView
.
http_method_names
,
'renderer_classes'
:
APIView
.
renderer_classes
,
'parser_classes'
:
APIView
.
parser_classes
,
'authentication_classes'
:
APIView
.
authentication_classes
,
...
...
@@ -29,6 +32,9 @@ class LazyViewCreator(object):
}
self
.
_cached_view
=
None
def
handler
(
self
,
*
args
,
**
kwargs
):
return
self
.
wrapped_view
(
*
args
,
**
kwargs
)
@property
def
view
(
self
):
"""
...
...
@@ -44,6 +50,11 @@ class LazyViewCreator(object):
for
attr
,
value
in
self
.
final_view_attrs
.
items
():
setattr
(
WrappedAPIView
,
attr
,
value
)
# Attach the wrapped view function for each of the
# allowed HTTP methods
for
method
in
WrappedAPIView
.
http_method_names
:
setattr
(
WrappedAPIView
,
method
.
lower
(),
self
.
handler
)
self
.
_cached_view
=
WrappedAPIView
.
as_view
()
return
self
.
_cached_view
...
...
@@ -55,51 +66,27 @@ class LazyViewCreator(object):
return
self
.
view
(
*
args
,
**
kwargs
)
@staticmethod
def
maybe_create
(
func
):
def
maybe_create
(
func
_or_instance
):
"""
If the argument is already an instance of LazyViewCreator,
just return it. Otherwise, create a new one.
"""
if
isinstance
(
func
,
LazyViewCreator
):
return
func
return
LazyViewCreator
()
def
api_view
(
allowed_methods
):
"""
Decorator for function based views.
@api_view(['GET', 'POST'])
def my_view(request):
# request will be an instance of `Request`
# `Response` objects will have .request set automatically
# APIException instances will be handled
"""
def
decorator
(
func
):
wrapper
=
LazyViewCreator
.
maybe_create
(
func
)
@wraps
(
func
,
assigned
=
available_attrs
(
func
))
def
handler
(
self
,
*
args
,
**
kwargs
):
return
func
(
*
args
,
**
kwargs
)
for
method
in
allowed_methods
:
wrapper
.
final_view_attrs
[
method
.
lower
()]
=
handler
return
wrapper
return
decorator
if
isinstance
(
func_or_instance
,
LazyViewCreator
):
return
func_or_instance
return
LazyViewCreator
(
func_or_instance
)
def
_create_attribute_setting_decorator
(
attribute
):
def
_create_attribute_setting_decorator
(
attribute
,
filter
=
lambda
item
:
item
):
def
decorator
(
value
):
def
inner
(
func
):
wrapper
=
LazyViewCreator
.
maybe_create
(
func
)
wrapper
.
final_view_attrs
[
attribute
]
=
value
wrapper
.
final_view_attrs
[
attribute
]
=
filter
(
value
)
return
wrapper
return
inner
return
decorator
api_view
=
_create_attribute_setting_decorator
(
'http_method_names'
,
filter
=
lambda
methods
:
[
method
.
lower
()
for
method
in
methods
])
renderer_classes
=
_create_attribute_setting_decorator
(
'renderer_classes'
)
parser_classes
=
_create_attribute_setting_decorator
(
'parser_classes'
)
authentication_classes
=
_create_attribute_setting_decorator
(
'authentication_classes'
)
...
...
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