Rename generic views

This commit is contained in:
Tom Christie 2012-10-03 09:26:15 +01:00
parent d1b99f350a
commit c30e0795be
7 changed files with 44 additions and 42 deletions

View File

@ -136,12 +136,12 @@ Using the mixin classes we've rewritten the views to use slightly less code than
from rest_framework import generics
class CommentRoot(generics.RootAPIView):
class CommentRoot(generics.ListCreateAPIView):
model = Comment
serializer_class = CommentSerializer
class CommentInstance(generics.InstanceAPIView):
class CommentInstance(generics.RetrieveUpdateDestroyAPIView):
model = Comment
serializer_class = CommentSerializer

View File

@ -106,10 +106,10 @@ class ListAPIView(mixins.ListModelMixin,
return self.metadata(request, *args, **kwargs)
class RootAPIView(mixins.ListModelMixin,
mixins.CreateModelMixin,
mixins.MetadataMixin,
MultipleObjectBaseView):
class ListCreateAPIView(mixins.ListModelMixin,
mixins.CreateModelMixin,
mixins.MetadataMixin,
MultipleObjectBaseView):
"""
Concrete view for listing a queryset or creating a model instance.
"""
@ -123,9 +123,9 @@ class RootAPIView(mixins.ListModelMixin,
return self.metadata(request, *args, **kwargs)
class DetailAPIView(mixins.RetrieveModelMixin,
mixins.MetadataMixin,
SingleObjectBaseView):
class RetrieveAPIView(mixins.RetrieveModelMixin,
mixins.MetadataMixin,
SingleObjectBaseView):
"""
Concrete view for retrieving a model instance.
"""
@ -136,11 +136,11 @@ class DetailAPIView(mixins.RetrieveModelMixin,
return self.metadata(request, *args, **kwargs)
class InstanceAPIView(mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
mixins.MetadataMixin,
SingleObjectBaseView):
class RetrieveUpdateDestroyAPIView(mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
mixins.MetadataMixin,
SingleObjectBaseView):
"""
Concrete view for retrieving, updating or deleting a model instance.
"""

View File

@ -239,27 +239,29 @@ class DocumentingHTMLRenderer(BaseRenderer):
# We need to map our Fields to Django's Fields.
field_mapping = dict([
[serializers.FloatField.__name__, forms.FloatField],
[serializers.IntegerField.__name__, forms.IntegerField],
[serializers.DateTimeField.__name__, forms.DateTimeField],
[serializers.DateField.__name__, forms.DateField],
[serializers.EmailField.__name__, forms.EmailField],
[serializers.CharField.__name__, forms.CharField],
[serializers.BooleanField.__name__, forms.BooleanField]
[serializers.FloatField.__name__, forms.FloatField],
[serializers.IntegerField.__name__, forms.IntegerField],
[serializers.DateTimeField.__name__, forms.DateTimeField],
[serializers.DateField.__name__, forms.DateField],
[serializers.EmailField.__name__, forms.EmailField],
[serializers.CharField.__name__, forms.CharField],
[serializers.BooleanField.__name__, forms.BooleanField]
])
# Creating an on the fly form see: http://stackoverflow.com/questions/3915024/dynamically-creating-classes-python
fields = {}
object, data = None, None
obj, data = None, None
if getattr(view, 'object', None):
object = view.object
serializer = view.get_serializer(instance=object)
obj = view.object
serializer = view.get_serializer(instance=obj)
for k, v in serializer.fields.items():
if v.readonly:
continue
fields[k] = field_mapping[v.__class__.__name__]()
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
form_instance = OnTheFlyForm(data)
return form_instance

View File

@ -62,8 +62,8 @@ class Resource(ResourceMixin, views.APIView):
class ModelResource(ResourceMixin, views.APIView):
root_class = generics.RootAPIView
detail_class = generics.InstanceAPIView
root_class = generics.ListCreateAPIView
detail_class = generics.RetrieveUpdateDestroyAPIView
def root_view(self):
return wrapped(self, self.root_class())

View File

@ -322,7 +322,7 @@ class ModelSerializer(RelatedField, Serializer):
else:
field = self.get_field(model_field)
if field is not None:
if field:
field.initialize(parent=self, model_field=model_field)
ret[model_field.name] = field

View File

@ -8,7 +8,7 @@ from rest_framework.tests.models import BasicModel
factory = RequestFactory()
class RootView(generics.RootAPIView):
class RootView(generics.ListCreateAPIView):
"""
Example description for OPTIONS.
"""
@ -16,7 +16,7 @@ class RootView(generics.RootAPIView):
paginate_by = None
class InstanceView(generics.InstanceAPIView):
class InstanceView(generics.RetrieveUpdateDestroyAPIView):
"""
Example description for OPTIONS.
"""
@ -40,7 +40,7 @@ class TestRootView(TestCase):
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('/')
response = self.view(request).render()
@ -49,7 +49,7 @@ class TestRootView(TestCase):
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'}
request = factory.post('/', json.dumps(content),
@ -62,7 +62,7 @@ class TestRootView(TestCase):
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'}
request = factory.put('/', json.dumps(content),
@ -73,7 +73,7 @@ class TestRootView(TestCase):
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('/')
response = self.view(request).render()
@ -82,7 +82,7 @@ class TestRootView(TestCase):
def test_options_root_view(self):
"""
OPTIONS requests to RootAPIView should return metadata
OPTIONS requests to ListCreateAPIView should return metadata
"""
request = factory.options('/')
response = self.view(request).render()
@ -133,7 +133,7 @@ class TestInstanceView(TestCase):
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')
response = self.view(request, pk=1).render()
@ -142,7 +142,7 @@ class TestInstanceView(TestCase):
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'}
request = factory.post('/', json.dumps(content),
@ -153,7 +153,7 @@ class TestInstanceView(TestCase):
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'}
request = factory.put('/1', json.dumps(content),
@ -166,7 +166,7 @@ class TestInstanceView(TestCase):
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')
response = self.view(request, pk=1).render()
@ -177,7 +177,7 @@ class TestInstanceView(TestCase):
def test_options_instance_view(self):
"""
OPTIONS requests to InstanceAPIView should return metadata
OPTIONS requests to RetrieveUpdateDestroyAPIView should return metadata
"""
request = factory.options('/')
response = self.view(request).render()

View File

@ -7,7 +7,7 @@ from rest_framework.tests.models import BasicModel
factory = RequestFactory()
class RootView(generics.RootAPIView):
class RootView(generics.ListCreateAPIView):
"""
Example description for OPTIONS.
"""
@ -35,7 +35,7 @@ class IntegrationTestPagination(TestCase):
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('/')
response = self.view(request).render()