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
e003cc91
Commit
e003cc91
authored
Sep 28, 2012
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Get test-only models properly working
parent
84f77580
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
81 additions
and
33 deletions
+81
-33
rest_framework/generics.py
+10
-2
rest_framework/runtests/settings.py
+0
-1
rest_framework/serializers.py
+2
-2
rest_framework/tests/__init__.py
+4
-1
rest_framework/tests/generics.py
+30
-0
rest_framework/tests/models.py
+35
-27
No files found.
rest_framework/generics.py
View file @
e003cc91
...
...
@@ -2,7 +2,7 @@
Generic views that provide commmonly needed behaviour.
"""
from
rest_framework
import
views
,
mixins
from
rest_framework
import
views
,
mixins
,
serializers
from
django.views.generic.detail
import
SingleObjectMixin
from
django.views.generic.list
import
MultipleObjectMixin
...
...
@@ -18,11 +18,19 @@ class BaseView(views.APIView):
def
get_serializer
(
self
,
data
=
None
,
files
=
None
,
instance
=
None
):
# TODO: add support for files
# TODO: add support for seperate serializer/deserializer
serializer_class
=
self
.
serializer_class
if
serializer_class
is
None
:
class
DefaultSerializer
(
serializers
.
ModelSerializer
):
class
Meta
:
model
=
self
.
model
serializer_class
=
DefaultSerializer
context
=
{
'request'
:
self
.
request
,
'format'
:
self
.
kwargs
.
get
(
'format'
,
None
)
}
return
se
lf
.
se
rializer_class
(
data
,
instance
=
instance
,
context
=
context
)
return
serializer_class
(
data
,
instance
=
instance
,
context
=
context
)
class
MultipleObjectBaseView
(
MultipleObjectMixin
,
BaseView
):
...
...
rest_framework/runtests/settings.py
View file @
e003cc91
...
...
@@ -100,7 +100,6 @@ import django
if
django
.
VERSION
<
(
1
,
3
):
INSTALLED_APPS
+=
(
'staticfiles'
,)
# OAuth support is optional, so we only test oauth if it's installed.
try
:
import
oauth_provider
...
...
rest_framework/serializers.py
View file @
e003cc91
...
...
@@ -114,7 +114,7 @@ class BaseSerializer(Field):
Returns the complete set of fields for the object as a dict.
This will be the set of any explicitly declared fields,
plus the set of fields returned by
get_
default_fields().
plus the set of fields returned by default_fields().
"""
ret
=
SortedDict
()
...
...
@@ -234,7 +234,7 @@ class BaseSerializer(Field):
return
dict
([(
key
,
self
.
to_native
(
val
))
for
(
key
,
val
)
in
obj
.
items
()])
elif
hasattr
(
obj
,
'__iter__'
):
return
(
self
.
to_native
(
item
)
for
item
in
obj
)
return
[
self
.
to_native
(
item
)
for
item
in
obj
]
return
self
.
convert_object
(
obj
)
def
from_native
(
self
,
data
):
...
...
rest_framework/tests/__init__.py
View file @
e003cc91
"""Force import of all modules in this package in order to get the standard test runner to pick up the tests. Yowzers."""
"""
Force import of all modules in this package in order to get the standard test
runner to pick up the tests. Yowzers.
"""
import
os
modules
=
[
filename
.
rsplit
(
'.'
,
1
)[
0
]
...
...
rest_framework/tests/generics.py
0 → 100644
View file @
e003cc91
from
django.test
import
TestCase
from
django.test.client
import
RequestFactory
from
rest_framework
import
generics
,
status
from
rest_framework.tests.models
import
BasicModel
factory
=
RequestFactory
()
class
RootView
(
generics
.
RootAPIView
):
model
=
BasicModel
class
TestListView
(
TestCase
):
def
setUp
(
self
):
items
=
[
'foo'
,
'bar'
,
'baz'
]
for
item
in
items
:
BasicModel
(
text
=
item
)
.
save
()
self
.
objects
=
BasicModel
.
objects
self
.
data
=
[
{
'id'
:
obj
.
id
,
'text'
:
obj
.
text
}
for
obj
in
self
.
objects
.
all
()
]
def
test_get_root_view
(
self
):
view
=
RootView
.
as_view
()
request
=
factory
.
get
(
'/'
)
response
=
view
(
request
)
.
render
()
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_200_OK
)
self
.
assertEquals
(
response
.
data
,
self
.
data
)
rest_framework/tests/models.py
View file @
e003cc91
from
django.db
import
models
from
django.contrib.auth.models
import
Group
class
CustomUser
(
models
.
Model
):
"""
A custom user model, which uses a 'through' table for the foreign key
"""
username
=
models
.
CharField
(
max_length
=
255
,
unique
=
True
)
groups
=
models
.
ManyToManyField
(
to
=
Group
,
blank
=
True
,
null
=
True
,
through
=
'UserGroupMap'
)
@models.permalink
def
get_absolute_url
(
self
):
return
(
'custom_user'
,
(),
{
'pk'
:
self
.
id
})
class
UserGroupMap
(
models
.
Model
):
user
=
models
.
ForeignKey
(
to
=
CustomUser
)
group
=
models
.
ForeignKey
(
to
=
Group
)
@models.permalink
def
get_absolute_url
(
self
):
return
(
'user_group_map'
,
(),
{
'pk'
:
self
.
id
})
# from django.contrib.auth.models import Group
# class CustomUser(models.Model):
# """
# A custom user model, which uses a 'through' table for the foreign key
# """
# username = models.CharField(max_length=255, unique=True)
# groups = models.ManyToManyField(
# to=Group, blank=True, null=True, through='UserGroupMap'
# )
# @models.permalink
# def get_absolute_url(self):
# return ('custom_user', (), {
# 'pk': self.id
# })
# class UserGroupMap(models.Model):
# user = models.ForeignKey(to=CustomUser)
# group = models.ForeignKey(to=Group)
# @models.permalink
# def get_absolute_url(self):
# return ('user_group_map', (), {
# 'pk': self.id
# })
class
BasicModel
(
models
.
Model
):
text
=
models
.
CharField
(
max_length
=
100
)
class
Meta
:
app_label
=
'rest_framework'
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