2011-07-08 06:33:39 +04:00
|
|
|
from django.conf.urls.defaults import patterns, url
|
|
|
|
from django.forms import ModelForm
|
|
|
|
from django.contrib.auth.models import Group, User
|
|
|
|
from djangorestframework.resources import ModelResource
|
|
|
|
from djangorestframework.views import ListOrCreateModelView, InstanceModelView
|
2011-07-08 20:37:20 +04:00
|
|
|
from djangorestframework.tests.models import CustomUser
|
2011-12-15 00:10:06 +04:00
|
|
|
from djangorestframework.tests.testcases import TestModelsTestCase
|
2011-07-08 06:33:39 +04:00
|
|
|
|
2012-02-23 13:21:01 +04:00
|
|
|
|
2011-07-08 06:33:39 +04:00
|
|
|
class GroupResource(ModelResource):
|
|
|
|
model = Group
|
|
|
|
|
2012-02-23 13:21:01 +04:00
|
|
|
|
2011-07-08 06:33:39 +04:00
|
|
|
class UserForm(ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
exclude = ('last_login', 'date_joined')
|
|
|
|
|
2012-02-23 13:21:01 +04:00
|
|
|
|
2011-07-08 06:33:39 +04:00
|
|
|
class UserResource(ModelResource):
|
|
|
|
model = User
|
|
|
|
form = UserForm
|
2011-12-29 17:31:12 +04:00
|
|
|
|
2012-02-23 13:21:01 +04:00
|
|
|
|
2011-07-08 20:37:20 +04:00
|
|
|
class CustomUserResource(ModelResource):
|
2011-12-29 17:31:12 +04:00
|
|
|
model = CustomUser
|
2011-07-08 06:33:39 +04:00
|
|
|
|
|
|
|
urlpatterns = patterns('',
|
|
|
|
url(r'^users/$', ListOrCreateModelView.as_view(resource=UserResource), name='users'),
|
|
|
|
url(r'^users/(?P<id>[0-9]+)/$', InstanceModelView.as_view(resource=UserResource)),
|
2011-07-08 20:37:20 +04:00
|
|
|
url(r'^customusers/$', ListOrCreateModelView.as_view(resource=CustomUserResource), name='customusers'),
|
|
|
|
url(r'^customusers/(?P<id>[0-9]+)/$', InstanceModelView.as_view(resource=CustomUserResource)),
|
2011-07-08 06:33:39 +04:00
|
|
|
url(r'^groups/$', ListOrCreateModelView.as_view(resource=GroupResource), name='groups'),
|
|
|
|
url(r'^groups/(?P<id>[0-9]+)/$', InstanceModelView.as_view(resource=GroupResource)),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2011-12-15 00:10:06 +04:00
|
|
|
class ModelViewTests(TestModelsTestCase):
|
2011-07-08 06:33:39 +04:00
|
|
|
"""Test the model views djangorestframework provides"""
|
2011-12-29 17:31:12 +04:00
|
|
|
urls = 'djangorestframework.tests.modelviews'
|
2011-07-08 06:33:39 +04:00
|
|
|
|
|
|
|
def test_creation(self):
|
|
|
|
"""Ensure that a model object can be created"""
|
|
|
|
self.assertEqual(0, Group.objects.count())
|
|
|
|
|
|
|
|
response = self.client.post('/groups/', {'name': 'foo'})
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 201)
|
|
|
|
self.assertEqual(1, Group.objects.count())
|
|
|
|
self.assertEqual('foo', Group.objects.all()[0].name)
|
|
|
|
|
|
|
|
def test_creation_with_m2m_relation(self):
|
|
|
|
"""Ensure that a model object with a m2m relation can be created"""
|
|
|
|
group = Group(name='foo')
|
|
|
|
group.save()
|
|
|
|
self.assertEqual(0, User.objects.count())
|
|
|
|
|
|
|
|
response = self.client.post('/users/', {'username': 'bar', 'password': 'baz', 'groups': [group.id]})
|
2011-12-29 17:31:12 +04:00
|
|
|
|
2011-07-08 06:33:39 +04:00
|
|
|
self.assertEqual(response.status_code, 201)
|
|
|
|
self.assertEqual(1, User.objects.count())
|
2011-12-29 17:31:12 +04:00
|
|
|
|
2011-07-08 06:33:39 +04:00
|
|
|
user = User.objects.all()[0]
|
|
|
|
self.assertEqual('bar', user.username)
|
|
|
|
self.assertEqual('baz', user.password)
|
|
|
|
self.assertEqual(1, user.groups.count())
|
2011-12-29 17:31:12 +04:00
|
|
|
|
2011-07-08 06:33:39 +04:00
|
|
|
group = user.groups.all()[0]
|
|
|
|
self.assertEqual('foo', group.name)
|
2011-12-29 17:31:12 +04:00
|
|
|
|
2011-07-08 20:37:20 +04:00
|
|
|
def test_creation_with_m2m_relation_through(self):
|
|
|
|
"""
|
|
|
|
Ensure that a model object with a m2m relation can be created where that
|
|
|
|
relation uses a through table
|
|
|
|
"""
|
|
|
|
group = Group(name='foo')
|
|
|
|
group.save()
|
|
|
|
self.assertEqual(0, User.objects.count())
|
|
|
|
|
|
|
|
response = self.client.post('/customusers/', {'username': 'bar', 'groups': [group.id]})
|
2011-12-29 17:31:12 +04:00
|
|
|
|
2011-07-08 20:37:20 +04:00
|
|
|
self.assertEqual(response.status_code, 201)
|
|
|
|
self.assertEqual(1, CustomUser.objects.count())
|
2011-12-29 17:31:12 +04:00
|
|
|
|
2011-07-08 20:37:20 +04:00
|
|
|
user = CustomUser.objects.all()[0]
|
|
|
|
self.assertEqual('bar', user.username)
|
|
|
|
self.assertEqual(1, user.groups.count())
|
2011-12-29 17:31:12 +04:00
|
|
|
|
2011-07-08 20:37:20 +04:00
|
|
|
group = user.groups.all()[0]
|
2011-12-29 17:31:12 +04:00
|
|
|
self.assertEqual('foo', group.name)
|