mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-26 03:23:59 +03:00
Rename generic views
This commit is contained in:
parent
d1b99f350a
commit
c30e0795be
|
@ -136,12 +136,12 @@ Using the mixin classes we've rewritten the views to use slightly less code than
|
||||||
from rest_framework import generics
|
from rest_framework import generics
|
||||||
|
|
||||||
|
|
||||||
class CommentRoot(generics.RootAPIView):
|
class CommentRoot(generics.ListCreateAPIView):
|
||||||
model = Comment
|
model = Comment
|
||||||
serializer_class = CommentSerializer
|
serializer_class = CommentSerializer
|
||||||
|
|
||||||
|
|
||||||
class CommentInstance(generics.InstanceAPIView):
|
class CommentInstance(generics.RetrieveUpdateDestroyAPIView):
|
||||||
model = Comment
|
model = Comment
|
||||||
serializer_class = CommentSerializer
|
serializer_class = CommentSerializer
|
||||||
|
|
||||||
|
|
|
@ -106,7 +106,7 @@ class ListAPIView(mixins.ListModelMixin,
|
||||||
return self.metadata(request, *args, **kwargs)
|
return self.metadata(request, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class RootAPIView(mixins.ListModelMixin,
|
class ListCreateAPIView(mixins.ListModelMixin,
|
||||||
mixins.CreateModelMixin,
|
mixins.CreateModelMixin,
|
||||||
mixins.MetadataMixin,
|
mixins.MetadataMixin,
|
||||||
MultipleObjectBaseView):
|
MultipleObjectBaseView):
|
||||||
|
@ -123,7 +123,7 @@ class RootAPIView(mixins.ListModelMixin,
|
||||||
return self.metadata(request, *args, **kwargs)
|
return self.metadata(request, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class DetailAPIView(mixins.RetrieveModelMixin,
|
class RetrieveAPIView(mixins.RetrieveModelMixin,
|
||||||
mixins.MetadataMixin,
|
mixins.MetadataMixin,
|
||||||
SingleObjectBaseView):
|
SingleObjectBaseView):
|
||||||
"""
|
"""
|
||||||
|
@ -136,7 +136,7 @@ class DetailAPIView(mixins.RetrieveModelMixin,
|
||||||
return self.metadata(request, *args, **kwargs)
|
return self.metadata(request, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class InstanceAPIView(mixins.RetrieveModelMixin,
|
class RetrieveUpdateDestroyAPIView(mixins.RetrieveModelMixin,
|
||||||
mixins.UpdateModelMixin,
|
mixins.UpdateModelMixin,
|
||||||
mixins.DestroyModelMixin,
|
mixins.DestroyModelMixin,
|
||||||
mixins.MetadataMixin,
|
mixins.MetadataMixin,
|
||||||
|
|
|
@ -250,16 +250,18 @@ class DocumentingHTMLRenderer(BaseRenderer):
|
||||||
|
|
||||||
# Creating an on the fly form see: http://stackoverflow.com/questions/3915024/dynamically-creating-classes-python
|
# Creating an on the fly form see: http://stackoverflow.com/questions/3915024/dynamically-creating-classes-python
|
||||||
fields = {}
|
fields = {}
|
||||||
object, data = None, None
|
obj, data = None, None
|
||||||
if getattr(view, 'object', None):
|
if getattr(view, 'object', None):
|
||||||
object = view.object
|
obj = view.object
|
||||||
serializer = view.get_serializer(instance=object)
|
|
||||||
|
serializer = view.get_serializer(instance=obj)
|
||||||
for k, v in serializer.fields.items():
|
for k, v in serializer.fields.items():
|
||||||
if v.readonly:
|
if v.readonly:
|
||||||
continue
|
continue
|
||||||
fields[k] = field_mapping[v.__class__.__name__]()
|
fields[k] = field_mapping[v.__class__.__name__]()
|
||||||
|
|
||||||
OnTheFlyForm = type("OnTheFlyForm", (forms.Form,), fields)
|
OnTheFlyForm = type("OnTheFlyForm", (forms.Form,), fields)
|
||||||
if object and not view.request.method == 'DELETE': # Don't fill in the form when the object is deleted
|
if obj and not view.request.method == 'DELETE': # Don't fill in the form when the object is deleted
|
||||||
data = serializer.data
|
data = serializer.data
|
||||||
form_instance = OnTheFlyForm(data)
|
form_instance = OnTheFlyForm(data)
|
||||||
return form_instance
|
return form_instance
|
||||||
|
|
|
@ -62,8 +62,8 @@ class Resource(ResourceMixin, views.APIView):
|
||||||
|
|
||||||
|
|
||||||
class ModelResource(ResourceMixin, views.APIView):
|
class ModelResource(ResourceMixin, views.APIView):
|
||||||
root_class = generics.RootAPIView
|
root_class = generics.ListCreateAPIView
|
||||||
detail_class = generics.InstanceAPIView
|
detail_class = generics.RetrieveUpdateDestroyAPIView
|
||||||
|
|
||||||
def root_view(self):
|
def root_view(self):
|
||||||
return wrapped(self, self.root_class())
|
return wrapped(self, self.root_class())
|
||||||
|
|
|
@ -322,7 +322,7 @@ class ModelSerializer(RelatedField, Serializer):
|
||||||
else:
|
else:
|
||||||
field = self.get_field(model_field)
|
field = self.get_field(model_field)
|
||||||
|
|
||||||
if field is not None:
|
if field:
|
||||||
field.initialize(parent=self, model_field=model_field)
|
field.initialize(parent=self, model_field=model_field)
|
||||||
ret[model_field.name] = field
|
ret[model_field.name] = field
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ from rest_framework.tests.models import BasicModel
|
||||||
factory = RequestFactory()
|
factory = RequestFactory()
|
||||||
|
|
||||||
|
|
||||||
class RootView(generics.RootAPIView):
|
class RootView(generics.ListCreateAPIView):
|
||||||
"""
|
"""
|
||||||
Example description for OPTIONS.
|
Example description for OPTIONS.
|
||||||
"""
|
"""
|
||||||
|
@ -16,7 +16,7 @@ class RootView(generics.RootAPIView):
|
||||||
paginate_by = None
|
paginate_by = None
|
||||||
|
|
||||||
|
|
||||||
class InstanceView(generics.InstanceAPIView):
|
class InstanceView(generics.RetrieveUpdateDestroyAPIView):
|
||||||
"""
|
"""
|
||||||
Example description for OPTIONS.
|
Example description for OPTIONS.
|
||||||
"""
|
"""
|
||||||
|
@ -40,7 +40,7 @@ class TestRootView(TestCase):
|
||||||
|
|
||||||
def test_get_root_view(self):
|
def test_get_root_view(self):
|
||||||
"""
|
"""
|
||||||
GET requests to RootAPIView should return list of objects.
|
GET requests to ListCreateAPIView should return list of objects.
|
||||||
"""
|
"""
|
||||||
request = factory.get('/')
|
request = factory.get('/')
|
||||||
response = self.view(request).render()
|
response = self.view(request).render()
|
||||||
|
@ -49,7 +49,7 @@ class TestRootView(TestCase):
|
||||||
|
|
||||||
def test_post_root_view(self):
|
def test_post_root_view(self):
|
||||||
"""
|
"""
|
||||||
POST requests to RootAPIView should create a new object.
|
POST requests to ListCreateAPIView should create a new object.
|
||||||
"""
|
"""
|
||||||
content = {'text': 'foobar'}
|
content = {'text': 'foobar'}
|
||||||
request = factory.post('/', json.dumps(content),
|
request = factory.post('/', json.dumps(content),
|
||||||
|
@ -62,7 +62,7 @@ class TestRootView(TestCase):
|
||||||
|
|
||||||
def test_put_root_view(self):
|
def test_put_root_view(self):
|
||||||
"""
|
"""
|
||||||
PUT requests to RootAPIView should not be allowed
|
PUT requests to ListCreateAPIView should not be allowed
|
||||||
"""
|
"""
|
||||||
content = {'text': 'foobar'}
|
content = {'text': 'foobar'}
|
||||||
request = factory.put('/', json.dumps(content),
|
request = factory.put('/', json.dumps(content),
|
||||||
|
@ -73,7 +73,7 @@ class TestRootView(TestCase):
|
||||||
|
|
||||||
def test_delete_root_view(self):
|
def test_delete_root_view(self):
|
||||||
"""
|
"""
|
||||||
DELETE requests to RootAPIView should not be allowed
|
DELETE requests to ListCreateAPIView should not be allowed
|
||||||
"""
|
"""
|
||||||
request = factory.delete('/')
|
request = factory.delete('/')
|
||||||
response = self.view(request).render()
|
response = self.view(request).render()
|
||||||
|
@ -82,7 +82,7 @@ class TestRootView(TestCase):
|
||||||
|
|
||||||
def test_options_root_view(self):
|
def test_options_root_view(self):
|
||||||
"""
|
"""
|
||||||
OPTIONS requests to RootAPIView should return metadata
|
OPTIONS requests to ListCreateAPIView should return metadata
|
||||||
"""
|
"""
|
||||||
request = factory.options('/')
|
request = factory.options('/')
|
||||||
response = self.view(request).render()
|
response = self.view(request).render()
|
||||||
|
@ -133,7 +133,7 @@ class TestInstanceView(TestCase):
|
||||||
|
|
||||||
def test_get_instance_view(self):
|
def test_get_instance_view(self):
|
||||||
"""
|
"""
|
||||||
GET requests to InstanceAPIView should return a single object.
|
GET requests to RetrieveUpdateDestroyAPIView should return a single object.
|
||||||
"""
|
"""
|
||||||
request = factory.get('/1')
|
request = factory.get('/1')
|
||||||
response = self.view(request, pk=1).render()
|
response = self.view(request, pk=1).render()
|
||||||
|
@ -142,7 +142,7 @@ class TestInstanceView(TestCase):
|
||||||
|
|
||||||
def test_post_instance_view(self):
|
def test_post_instance_view(self):
|
||||||
"""
|
"""
|
||||||
POST requests to InstanceAPIView should not be allowed
|
POST requests to RetrieveUpdateDestroyAPIView should not be allowed
|
||||||
"""
|
"""
|
||||||
content = {'text': 'foobar'}
|
content = {'text': 'foobar'}
|
||||||
request = factory.post('/', json.dumps(content),
|
request = factory.post('/', json.dumps(content),
|
||||||
|
@ -153,7 +153,7 @@ class TestInstanceView(TestCase):
|
||||||
|
|
||||||
def test_put_instance_view(self):
|
def test_put_instance_view(self):
|
||||||
"""
|
"""
|
||||||
PUT requests to InstanceAPIView should update an object.
|
PUT requests to RetrieveUpdateDestroyAPIView should update an object.
|
||||||
"""
|
"""
|
||||||
content = {'text': 'foobar'}
|
content = {'text': 'foobar'}
|
||||||
request = factory.put('/1', json.dumps(content),
|
request = factory.put('/1', json.dumps(content),
|
||||||
|
@ -166,7 +166,7 @@ class TestInstanceView(TestCase):
|
||||||
|
|
||||||
def test_delete_instance_view(self):
|
def test_delete_instance_view(self):
|
||||||
"""
|
"""
|
||||||
DELETE requests to InstanceAPIView should delete an object.
|
DELETE requests to RetrieveUpdateDestroyAPIView should delete an object.
|
||||||
"""
|
"""
|
||||||
request = factory.delete('/1')
|
request = factory.delete('/1')
|
||||||
response = self.view(request, pk=1).render()
|
response = self.view(request, pk=1).render()
|
||||||
|
@ -177,7 +177,7 @@ class TestInstanceView(TestCase):
|
||||||
|
|
||||||
def test_options_instance_view(self):
|
def test_options_instance_view(self):
|
||||||
"""
|
"""
|
||||||
OPTIONS requests to InstanceAPIView should return metadata
|
OPTIONS requests to RetrieveUpdateDestroyAPIView should return metadata
|
||||||
"""
|
"""
|
||||||
request = factory.options('/')
|
request = factory.options('/')
|
||||||
response = self.view(request).render()
|
response = self.view(request).render()
|
||||||
|
|
|
@ -7,7 +7,7 @@ from rest_framework.tests.models import BasicModel
|
||||||
factory = RequestFactory()
|
factory = RequestFactory()
|
||||||
|
|
||||||
|
|
||||||
class RootView(generics.RootAPIView):
|
class RootView(generics.ListCreateAPIView):
|
||||||
"""
|
"""
|
||||||
Example description for OPTIONS.
|
Example description for OPTIONS.
|
||||||
"""
|
"""
|
||||||
|
@ -35,7 +35,7 @@ class IntegrationTestPagination(TestCase):
|
||||||
|
|
||||||
def test_get_paginated_root_view(self):
|
def test_get_paginated_root_view(self):
|
||||||
"""
|
"""
|
||||||
GET requests to paginated RootAPIView should return paginated results.
|
GET requests to paginated ListCreateAPIView should return paginated results.
|
||||||
"""
|
"""
|
||||||
request = factory.get('/')
|
request = factory.get('/')
|
||||||
response = self.view(request).render()
|
response = self.view(request).render()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user