From a32eb50bae8b16803eec108ec3a9c48ecf304d7a Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 14 Dec 2011 14:48:51 +0000 Subject: [PATCH] De-couple request/response layer CRUD, from underlying resource CRUD. --- djangorestframework/tests/mixins.py | 14 ++++---- djangorestframework/views.py | 50 +++++++---------------------- 2 files changed, 18 insertions(+), 46 deletions(-) diff --git a/djangorestframework/tests/mixins.py b/djangorestframework/tests/mixins.py index 0ccef5d3a..3b6512185 100644 --- a/djangorestframework/tests/mixins.py +++ b/djangorestframework/tests/mixins.py @@ -4,7 +4,7 @@ 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 PaginatorMixin +from djangorestframework.mixins import PaginatorMixin, ModelMixin from djangorestframework.resources import ModelResource from djangorestframework.response import Response from djangorestframework.tests.models import CustomUser @@ -29,7 +29,7 @@ class TestModelCreation(TestCase): mixin.resource = GroupResource mixin.CONTENT = form_data - response = mixin.post(request) + response = mixin.create(request) self.assertEquals(1, Group.objects.count()) self.assertEquals('foo', response.cleaned_content.name) @@ -55,7 +55,7 @@ class TestModelCreation(TestCase): mixin.resource = UserResource mixin.CONTENT = cleaned_data - response = mixin.post(request) + response = mixin.create(request) self.assertEquals(1, User.objects.count()) self.assertEquals(1, response.cleaned_content.groups.count()) self.assertEquals('foo', response.cleaned_content.groups.all()[0].name) @@ -78,7 +78,7 @@ class TestModelCreation(TestCase): mixin.resource = UserResource mixin.CONTENT = cleaned_data - response = mixin.post(request) + response = mixin.create(request) self.assertEquals(1, CustomUser.objects.count()) self.assertEquals(0, response.cleaned_content.groups.count()) @@ -89,11 +89,11 @@ class TestModelCreation(TestCase): request = self.req.post('/groups', data=form_data) cleaned_data = dict(form_data) cleaned_data['groups'] = [group] - mixin = CreateModelMixin() + mixin = ModelMixin() mixin.resource = UserResource mixin.CONTENT = cleaned_data - response = mixin.post(request) + response = mixin.create(request) self.assertEquals(2, CustomUser.objects.count()) self.assertEquals(1, response.cleaned_content.groups.count()) self.assertEquals('foo1', response.cleaned_content.groups.all()[0].name) @@ -109,7 +109,7 @@ class TestModelCreation(TestCase): mixin.resource = UserResource mixin.CONTENT = cleaned_data - response = mixin.post(request) + response = mixin.create(request) self.assertEquals(3, CustomUser.objects.count()) self.assertEquals(2, response.cleaned_content.groups.count()) self.assertEquals('foo1', response.cleaned_content.groups.all()[0].name) diff --git a/djangorestframework/views.py b/djangorestframework/views.py index d449636db..18911a52c 100644 --- a/djangorestframework/views.py +++ b/djangorestframework/views.py @@ -184,14 +184,8 @@ class ModelView(ModelMixin, View): """ resource = resources.ModelResource - def _filter_kwargs(self, kwargs): - kwargs = kwargs.copy() - if BaseRenderer._FORMAT_QUERY_PARAM in kwargs: - del kwargs[BaseRenderer._FORMAT_QUERY_PARAM] - return kwargs - -class InstanceModelView(ModelView): +class InstanceModelView(InstanceMixin, ModelView): """ A view which provides default operations for read/update/delete against a model instance. This view is also treated as the Canonical identifier @@ -199,49 +193,27 @@ class InstanceModelView(ModelView): """ _suffix = 'Instance' - def get(self, request, *args, **kwargs): - instance = self.read(request, *args, **self._filter_kwargs(kwargs)) - - if not instance: - raise ErrorResponse(status.HTTP_404_NOT_FOUND) - - return instance - - def put(self, request, *args, **kwargs): - return self.update(request, *args, **self._filter_kwargs(kwargs)) - - def delete(self, request, *args, **kwargs): - instance = self.destroy(request, *args, **self._filter_kwargs(kwargs)) - - if not instance: - raise ErrorResponse(status.HTTP_404_NOT_FOUND, None, {}) - - return None + get = ModelMixin.read + put = ModelMixin.update + delete = ModelMixin.destroy class ListModelView(ModelView): """ - A view which provides default operations for list, against a model in the database. + A view which provides default operations for list, against a model in the + database. """ _suffix = 'List' - def get(self, request, *args, **kwargs): - return self.list(request, *args, **self._filter_kwargs(kwargs)) + get = ModelMixin.list class ListOrCreateModelView(ModelView): """ - A view which provides default operations for list and create, against a model in the database. + A view which provides default operations for list and create, against a + model in the database. """ _suffix = 'List' - def get(self, request, *args, **kwargs): - return self.list(request, *args, **self._filter_kwargs(kwargs)) - - def post(self, request, *args, **kwargs): - instance = self.create(request, *args, **self._filter_kwargs(kwargs)) - - headers = {} - if hasattr(instance, 'get_absolute_url'): - headers['Location'] = self.resource(self).url(instance) - return Response(status.HTTP_201_CREATED, instance, headers) + get = ModelMixin.list + post = ModelMixin.create