Minor mixin refactoring

This commit is contained in:
Tom Christie 2013-05-17 22:09:23 +01:00
parent b6fb377c2b
commit 34776da924

View File

@ -43,7 +43,6 @@ def _get_validation_exclusions(obj, pk=None, slug_field=None, lookup_field=None)
class CreateModelMixin(object): class CreateModelMixin(object):
""" """
Create a model instance. Create a model instance.
Should be mixed in with any `GenericAPIView`.
""" """
def create(self, request, *args, **kwargs): def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.DATA, files=request.FILES) serializer = self.get_serializer(data=request.DATA, files=request.FILES)
@ -68,7 +67,6 @@ class CreateModelMixin(object):
class ListModelMixin(object): class ListModelMixin(object):
""" """
List a queryset. List a queryset.
Should be mixed in with `MultipleObjectAPIView`.
""" """
empty_error = "Empty list and '%(class_name)s.allow_empty' is False." empty_error = "Empty list and '%(class_name)s.allow_empty' is False."
@ -101,7 +99,6 @@ class ListModelMixin(object):
class RetrieveModelMixin(object): class RetrieveModelMixin(object):
""" """
Retrieve a model instance. Retrieve a model instance.
Should be mixed in with `SingleObjectAPIView`.
""" """
def retrieve(self, request, *args, **kwargs): def retrieve(self, request, *args, **kwargs):
self.object = self.get_object() self.object = self.get_object()
@ -112,17 +109,22 @@ class RetrieveModelMixin(object):
class UpdateModelMixin(object): class UpdateModelMixin(object):
""" """
Update a model instance. Update a model instance.
Should be mixed in with `SingleObjectAPIView`.
""" """
def update(self, request, *args, **kwargs): def get_object_or_none(self):
partial = kwargs.pop('partial', False)
self.object = None
try: try:
self.object = self.get_object() return self.get_object()
except Http404: except Http404:
# If this is a PUT-as-create operation, we need to ensure that # If this is a PUT-as-create operation, we need to ensure that
# we have relevant permissions, as if this was a POST request. # we have relevant permissions, as if this was a POST request.
self.check_permissions(clone_request(request, 'POST')) # This will either raise a PermissionDenied exception,
# or simply return None
self.check_permissions(clone_request(self.request, 'POST'))
def update(self, request, *args, **kwargs):
partial = kwargs.pop('partial', False)
self.object = self.get_object_or_none()
if self.object is None:
created = True created = True
save_kwargs = {'force_insert': True} save_kwargs = {'force_insert': True}
success_status_code = status.HTTP_201_CREATED success_status_code = status.HTTP_201_CREATED
@ -175,7 +177,6 @@ class UpdateModelMixin(object):
class DestroyModelMixin(object): class DestroyModelMixin(object):
""" """
Destroy a model instance. Destroy a model instance.
Should be mixed in with `SingleObjectAPIView`.
""" """
def destroy(self, request, *args, **kwargs): def destroy(self, request, *args, **kwargs):
obj = self.get_object() obj = self.get_object()