Tom Christie 2012-01-21 17:58:06 +00:00
commit add6c88a26
2 changed files with 46 additions and 6 deletions

View File

@ -557,6 +557,13 @@ class ModelMixin(object):
return all_kw_args 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): class ReadModelMixin(ModelMixin):
""" """
@ -566,7 +573,7 @@ class ReadModelMixin(ModelMixin):
model = self.resource.model model = self.resource.model
try: try:
self.model_instance = model.objects.get(self.build_query(*args, **kwargs)) self.model_instance = self.get_object(*args, **kwargs)
except model.DoesNotExist: except model.DoesNotExist:
raise ErrorResponse(status.HTTP_404_NOT_FOUND) 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 # 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: 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(): for (key, val) in self.CONTENT.items():
setattr(self.model_instance, key, val) setattr(self.model_instance, key, val)
@ -647,7 +654,7 @@ class DeleteModelMixin(ModelMixin):
model = self.resource.model model = self.resource.model
try: try:
instance = model.objects.get(self.build_query(*args, **kwargs)) instance = self.get_object(*args, **kwargs)
except model.DoesNotExist: except model.DoesNotExist:
raise ErrorResponse(status.HTTP_404_NOT_FOUND, None, {}) raise ErrorResponse(status.HTTP_404_NOT_FOUND, None, {})

View File

@ -4,14 +4,47 @@ from django.utils import simplejson as json
from djangorestframework import status from djangorestframework import status
from djangorestframework.compat import RequestFactory from djangorestframework.compat import RequestFactory
from django.contrib.auth.models import Group, User 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.resources import ModelResource
from djangorestframework.response import Response from djangorestframework.response import Response, ErrorResponse
from djangorestframework.tests.models import CustomUser from djangorestframework.tests.models import CustomUser
from djangorestframework.tests.testcases import TestModelsTestCase from djangorestframework.tests.testcases import TestModelsTestCase
from djangorestframework.views import View 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): class TestModelCreation(TestModelsTestCase):
"""Tests on CreateModelMixin""" """Tests on CreateModelMixin"""