Fix implementation

This commit is contained in:
Tom Christie 2013-01-15 17:50:51 +00:00
parent 8f3931e02d
commit 52847a215d
2 changed files with 23 additions and 47 deletions

View File

@ -25,9 +25,6 @@ class CreateModelMixin(object):
return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def pre_save(self, obj):
pass
class ListModelMixin(object): class ListModelMixin(object):
""" """

View File

@ -1,31 +1,27 @@
##### RESOURCES AND ROUTERS ARE NOT YET IMPLEMENTED - PLACEHOLDER ONLY ##### ##### RESOURCES AND ROUTERS ARE NOT YET IMPLEMENTED - PLACEHOLDER ONLY #####
from functools import update_wrapper from functools import update_wrapper
import inspect
from django.utils.decorators import classonlymethod from django.utils.decorators import classonlymethod
from rest_framework import views, generics from rest_framework import views, generics, mixins
def wrapped(source, dest):
"""
Copy public, non-method attributes from source to dest, and return dest.
"""
for attr in [attr for attr in dir(source)
if not attr.startswith('_') and not inspect.ismethod(attr)]:
setattr(dest, attr, getattr(source, attr))
return dest
##### RESOURCES AND ROUTERS ARE NOT YET IMPLEMENTED - PLACEHOLDER ONLY ##### ##### RESOURCES AND ROUTERS ARE NOT YET IMPLEMENTED - PLACEHOLDER ONLY #####
class ResourceMixin(object): class ResourceMixin(object):
""" """
Clone Django's `View.as_view()` behaviour *except* using REST framework's This is the magic.
'method -> action' binding for resources.
Overrides `.as_view()` so that it takes an `actions` keyword that performs
the binding of HTTP methods to actions on the Resource.
For example, to create a concrete view binding the 'GET' and 'POST' methods
to the 'list' and 'create' actions...
my_resource = MyResource.as_view({'get': 'list', 'post': 'create'})
""" """
@classonlymethod @classonlymethod
def as_view(cls, actions, **initkwargs): def as_view(cls, actions=None, **initkwargs):
""" """
Main entry point for a request-response process. Main entry point for a request-response process.
""" """
@ -61,36 +57,19 @@ class ResourceMixin(object):
return view return view
##### RESOURCES AND ROUTERS ARE NOT YET IMPLEMENTED - PLACEHOLDER ONLY #####
class Resource(ResourceMixin, views.APIView): class Resource(ResourceMixin, views.APIView):
pass pass
##### RESOURCES AND ROUTERS ARE NOT YET IMPLEMENTED - PLACEHOLDER ONLY ##### # Note the inheritence of both MultipleObjectAPIView *and* SingleObjectAPIView
# is a bit weird given the diamond inheritence, but it will work for now.
class ModelResource(ResourceMixin, views.APIView): # There's some implementation clean up that can happen later.
# TODO: Actually delegation won't work class ModelResource(mixins.CreateModelMixin,
root_class = generics.ListCreateAPIView mixins.RetrieveModelMixin,
detail_class = generics.RetrieveUpdateDestroyAPIView mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
def root_view(self): mixins.ListModelMixin,
return wrapped(self, self.root_class()) ResourceMixin,
generics.MultipleObjectAPIView,
def detail_view(self): generics.SingleObjectAPIView):
return wrapped(self, self.detail_class()) pass
def list(self, request, *args, **kwargs):
return self.root_view().list(request, args, kwargs)
def create(self, request, *args, **kwargs):
return self.root_view().create(request, args, kwargs)
def retrieve(self, request, *args, **kwargs):
return self.detail_view().retrieve(request, args, kwargs)
def update(self, request, *args, **kwargs):
return self.detail_view().update(request, args, kwargs)
def destroy(self, request, *args, **kwargs):
return self.detail_view().destroy(request, args, kwargs)