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
eff40391
Commit
eff40391
authored
Jan 02, 2013
by
Tom Christie
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'patch-support' of
https://github.com/ahankinson/django-rest-framework
into patch
parents
d379997a
389ca3b3
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
79 additions
and
8 deletions
+79
-8
rest_framework/compat.py
+6
-0
rest_framework/generics.py
+10
-4
rest_framework/mixins.py
+2
-2
rest_framework/tests/decorators.py
+16
-0
rest_framework/tests/generics.py
+15
-1
rest_framework/tests/utils.py
+27
-0
rest_framework/tests/views.py
+3
-1
No files found.
rest_framework/compat.py
View file @
eff40391
...
...
@@ -96,6 +96,12 @@ else:
update_wrapper
(
view
,
cls
.
dispatch
,
assigned
=
())
return
view
# Taken from @markotibold's attempt at supporting PATCH.
# https://github.com/markotibold/django-rest-framework/tree/patch
http_method_names
=
set
(
View
.
http_method_names
)
http_method_names
.
add
(
'patch'
)
View
.
http_method_names
=
list
(
http_method_names
)
# PATCH method is not implemented by Django
# PUT, DELETE do not require CSRF until 1.4. They should. Make it better.
if
django
.
VERSION
>=
(
1
,
4
):
from
django.middleware.csrf
import
CsrfViewMiddleware
...
...
rest_framework/generics.py
View file @
eff40391
...
...
@@ -47,14 +47,14 @@ class GenericAPIView(views.APIView):
return
serializer_class
def
get_serializer
(
self
,
instance
=
None
,
data
=
None
,
files
=
None
):
def
get_serializer
(
self
,
instance
=
None
,
data
=
None
,
files
=
None
,
partial
=
False
):
"""
Return the serializer instance that should be used for validating and
deserializing input, and for serializing output.
"""
serializer_class
=
self
.
get_serializer_class
()
context
=
self
.
get_serializer_context
()
return
serializer_class
(
instance
,
data
=
data
,
files
=
files
,
context
=
context
)
return
serializer_class
(
instance
,
data
=
data
,
files
=
files
,
partial
=
partial
,
context
=
context
)
class
MultipleObjectAPIView
(
MultipleObjectMixin
,
GenericAPIView
):
...
...
@@ -169,7 +169,10 @@ class UpdateAPIView(mixins.UpdateModelMixin,
Concrete view for updating a model instance.
"""
def
put
(
self
,
request
,
*
args
,
**
kwargs
):
return
self
.
update
(
request
,
*
args
,
**
kwargs
)
return
self
.
update
(
request
,
partial
=
False
,
*
args
,
**
kwargs
)
def
patch
(
self
,
request
,
*
args
,
**
kwargs
):
return
self
.
update
(
request
,
partial
=
True
,
*
args
,
**
kwargs
)
class
ListCreateAPIView
(
mixins
.
ListModelMixin
,
...
...
@@ -209,7 +212,10 @@ class RetrieveUpdateDestroyAPIView(mixins.RetrieveModelMixin,
return
self
.
retrieve
(
request
,
*
args
,
**
kwargs
)
def
put
(
self
,
request
,
*
args
,
**
kwargs
):
return
self
.
update
(
request
,
*
args
,
**
kwargs
)
return
self
.
update
(
request
,
partial
=
False
,
*
args
,
**
kwargs
)
def
delete
(
self
,
request
,
*
args
,
**
kwargs
):
return
self
.
destroy
(
request
,
*
args
,
**
kwargs
)
def
patch
(
self
,
request
,
*
args
,
**
kwargs
):
return
self
.
update
(
request
,
partial
=
True
,
*
args
,
**
kwargs
)
rest_framework/mixins.py
View file @
eff40391
...
...
@@ -81,7 +81,7 @@ class UpdateModelMixin(object):
Update a model instance.
Should be mixed in with `SingleObjectBaseView`.
"""
def
update
(
self
,
request
,
*
args
,
**
kwargs
):
def
update
(
self
,
request
,
partial
=
False
,
*
args
,
**
kwargs
):
try
:
self
.
object
=
self
.
get_object
()
created
=
False
...
...
@@ -89,7 +89,7 @@ class UpdateModelMixin(object):
self
.
object
=
None
created
=
True
serializer
=
self
.
get_serializer
(
self
.
object
,
data
=
request
.
DATA
,
files
=
request
.
FILES
)
serializer
=
self
.
get_serializer
(
self
.
object
,
data
=
request
.
DATA
,
files
=
request
.
FILES
,
partial
=
partial
)
if
serializer
.
is_valid
():
self
.
pre_save
(
serializer
.
object
)
...
...
rest_framework/tests/decorators.py
View file @
eff40391
...
...
@@ -17,6 +17,8 @@ from rest_framework.decorators import (
permission_classes
,
)
from
rest_framework.tests.utils
import
RequestFactory
class
DecoratorTestCase
(
TestCase
):
...
...
@@ -63,6 +65,20 @@ class DecoratorTestCase(TestCase):
response
=
view
(
request
)
self
.
assertEqual
(
response
.
status_code
,
405
)
def
test_calling_patch_method
(
self
):
@api_view
([
'GET'
,
'PATCH'
])
def
view
(
request
):
return
Response
({})
request
=
self
.
factory
.
patch
(
'/'
)
response
=
view
(
request
)
self
.
assertEqual
(
response
.
status_code
,
200
)
request
=
self
.
factory
.
post
(
'/'
)
response
=
view
(
request
)
self
.
assertEqual
(
response
.
status_code
,
405
)
def
test_renderer_classes
(
self
):
@api_view
([
'GET'
])
...
...
rest_framework/tests/generics.py
View file @
eff40391
from
django.db
import
models
from
django.test
import
TestCase
from
django.test.client
import
RequestFactory
from
django.utils
import
simplejson
as
json
from
rest_framework
import
generics
,
serializers
,
status
from
rest_framework.tests.utils
import
RequestFactory
from
rest_framework.tests.models
import
BasicModel
,
Comment
,
SlugBasedModel
...
...
@@ -181,6 +181,20 @@ class TestInstanceView(TestCase):
updated
=
self
.
objects
.
get
(
id
=
1
)
self
.
assertEquals
(
updated
.
text
,
'foobar'
)
def
test_patch_instance_view
(
self
):
"""
PATCH requests to RetrieveUpdateDestroyAPIView should update an object.
"""
content
=
{
'text'
:
'foobar'
}
request
=
factory
.
patch
(
'/1'
,
json
.
dumps
(
content
),
content_type
=
'application/json'
)
response
=
self
.
view
(
request
,
pk
=
1
)
.
render
()
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertEquals
(
response
.
data
,
{
'id'
:
1
,
'text'
:
'foobar'
})
updated
=
self
.
objects
.
get
(
id
=
1
)
self
.
assertEquals
(
updated
.
text
,
'foobar'
)
def
test_delete_instance_view
(
self
):
"""
DELETE requests to RetrieveUpdateDestroyAPIView should delete an object.
...
...
rest_framework/tests/utils.py
0 → 100644
View file @
eff40391
from
django.test.client
import
RequestFactory
,
FakePayload
from
django.test.client
import
MULTIPART_CONTENT
from
urlparse
import
urlparse
class
RequestFactory
(
RequestFactory
):
def
__init__
(
self
,
**
defaults
):
super
(
RequestFactory
,
self
)
.
__init__
(
**
defaults
)
def
patch
(
self
,
path
,
data
=
{},
content_type
=
MULTIPART_CONTENT
,
**
extra
):
"Construct a PATCH request."
patch_data
=
self
.
_encode_data
(
data
,
content_type
)
parsed
=
urlparse
(
path
)
r
=
{
'CONTENT_LENGTH'
:
len
(
patch_data
),
'CONTENT_TYPE'
:
content_type
,
'PATH_INFO'
:
self
.
_get_path
(
parsed
),
'QUERY_STRING'
:
parsed
[
4
],
'REQUEST_METHOD'
:
'PATCH'
,
'wsgi.input'
:
FakePayload
(
patch_data
),
}
r
.
update
(
extra
)
return
self
.
request
(
**
r
)
rest_framework/tests/views.py
View file @
eff40391
...
...
@@ -18,7 +18,7 @@ class BasicView(APIView):
return
Response
({
'method'
:
'POST'
,
'data'
:
request
.
DATA
})
@api_view
([
'GET'
,
'POST'
,
'PUT'
])
@api_view
([
'GET'
,
'POST'
,
'PUT'
,
'PATCH'
])
def
basic_view
(
request
):
if
request
.
method
==
'GET'
:
return
{
'method'
:
'GET'
}
...
...
@@ -26,6 +26,8 @@ def basic_view(request):
return
{
'method'
:
'POST'
,
'data'
:
request
.
DATA
}
elif
request
.
method
==
'PUT'
:
return
{
'method'
:
'PUT'
,
'data'
:
request
.
DATA
}
elif
request
.
method
==
'PATCH'
:
return
{
'method'
:
'PATCH'
,
'data'
:
request
.
DATA
}
def
sanitise_json_error
(
error_dict
):
...
...
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