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
add6c88a
Commit
add6c88a
authored
Jan 21, 2012
by
Tom Christie
Browse files
Options
Browse Files
Download
Plain Diff
Merge
https://github.com/mjumbewu/django-rest-framework
parents
c9442315
417eacb2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
6 deletions
+46
-6
djangorestframework/mixins.py
+11
-4
djangorestframework/tests/mixins.py
+35
-2
No files found.
djangorestframework/mixins.py
View file @
add6c88a
...
...
@@ -557,6 +557,13 @@ class ModelMixin(object):
return
all_kw_args
def
get_object
(
self
,
*
args
,
**
kwargs
):
"""
Get the instance object for read/update/delete requests.
"""
model
=
self
.
resource
.
model
return
model
.
objects
.
get
(
self
.
build_query
(
*
args
,
**
kwargs
))
class
ReadModelMixin
(
ModelMixin
):
"""
...
...
@@ -566,7 +573,7 @@ class ReadModelMixin(ModelMixin):
model
=
self
.
resource
.
model
try
:
self
.
model_instance
=
model
.
objects
.
get
(
self
.
build_query
(
*
args
,
**
kwargs
)
)
self
.
model_instance
=
self
.
get_object
(
*
args
,
**
kwargs
)
except
model
.
DoesNotExist
:
raise
ErrorResponse
(
status
.
HTTP_404_NOT_FOUND
)
...
...
@@ -629,7 +636,7 @@ class UpdateModelMixin(ModelMixin):
# TODO: update on the url of a non-existing resource url doesn't work correctly at the moment - will end up with a new url
try
:
self
.
model_instance
=
model
.
objects
.
get
(
self
.
build_query
(
*
args
,
**
kwargs
)
)
self
.
model_instance
=
self
.
get_object
(
*
args
,
**
kwargs
)
for
(
key
,
val
)
in
self
.
CONTENT
.
items
():
setattr
(
self
.
model_instance
,
key
,
val
)
...
...
@@ -647,7 +654,7 @@ class DeleteModelMixin(ModelMixin):
model
=
self
.
resource
.
model
try
:
instance
=
model
.
objects
.
get
(
self
.
build_query
(
*
args
,
**
kwargs
)
)
instance
=
self
.
get_object
(
*
args
,
**
kwargs
)
except
model
.
DoesNotExist
:
raise
ErrorResponse
(
status
.
HTTP_404_NOT_FOUND
,
None
,
{})
...
...
@@ -689,7 +696,7 @@ class ListModelMixin(ModelMixin):
def
get_queryset
(
self
):
model
=
self
.
resource
.
model
return
model
.
objects
.
all
()
if
self
.
queryset
is
None
else
self
.
queryset
return
model
.
objects
.
all
()
if
self
.
queryset
is
None
else
self
.
queryset
########## Pagination Mixins ##########
...
...
djangorestframework/tests/mixins.py
View file @
add6c88a
...
...
@@ -4,14 +4,47 @@ from django.utils import simplejson as json
from
djangorestframework
import
status
from
djangorestframework.compat
import
RequestFactory
from
django.contrib.auth.models
import
Group
,
User
from
djangorestframework.mixins
import
CreateModelMixin
,
PaginatorMixin
from
djangorestframework.mixins
import
CreateModelMixin
,
PaginatorMixin
,
ReadModelMixin
from
djangorestframework.resources
import
ModelResource
from
djangorestframework.response
import
Response
from
djangorestframework.response
import
Response
,
ErrorResponse
from
djangorestframework.tests.models
import
CustomUser
from
djangorestframework.tests.testcases
import
TestModelsTestCase
from
djangorestframework.views
import
View
class
TestModelRead
(
TestModelsTestCase
):
"""Tests on ReadModelMixin"""
def
setUp
(
self
):
super
(
TestModelRead
,
self
)
.
setUp
()
self
.
req
=
RequestFactory
()
def
test_read
(
self
):
Group
.
objects
.
create
(
name
=
'other group'
)
group
=
Group
.
objects
.
create
(
name
=
'my group'
)
class
GroupResource
(
ModelResource
):
model
=
Group
request
=
self
.
req
.
get
(
'/groups'
)
mixin
=
ReadModelMixin
()
mixin
.
resource
=
GroupResource
response
=
mixin
.
get
(
request
,
group
.
id
)
self
.
assertEquals
(
group
.
name
,
response
.
name
)
def
test_read_404
(
self
):
class
GroupResource
(
ModelResource
):
model
=
Group
request
=
self
.
req
.
get
(
'/groups'
)
mixin
=
ReadModelMixin
()
mixin
.
resource
=
GroupResource
with
self
.
assertRaises
(
ErrorResponse
):
response
=
mixin
.
get
(
request
,
12345
)
class
TestModelCreation
(
TestModelsTestCase
):
"""Tests on CreateModelMixin"""
...
...
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