mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-28 04:24:00 +03:00
De-couple request/response layer CRUD, from underlying resource CRUD.
This commit is contained in:
parent
f84fd47825
commit
a32eb50bae
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user