Minor tweaks to internals of generics and mixins

This commit is contained in:
Tom Christie 2012-11-16 21:27:34 +00:00
parent 3cfb23e4ac
commit 8d3581f4bd
2 changed files with 15 additions and 15 deletions

View File

@ -66,9 +66,6 @@ class MultipleObjectAPIView(MultipleObjectMixin, GenericAPIView):
backend = self.filter_backend() backend = self.filter_backend()
return backend.filter_queryset(self.request, queryset, self) return backend.filter_queryset(self.request, queryset, self)
def get_filtered_queryset(self):
return self.filter_queryset(self.get_queryset())
def get_pagination_serializer_class(self): def get_pagination_serializer_class(self):
""" """
Return the class to use for the pagination serializer. Return the class to use for the pagination serializer.

View File

@ -24,9 +24,9 @@ class CreateModelMixin(object):
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get_success_headers(self, data): def get_success_headers(self, data):
if 'url' in data: try:
return {'Location': data.get('url')} return {'Location': data['url']}
else: except (TypeError, KeyError):
return {} return {}
def pre_save(self, obj): def pre_save(self, obj):
@ -41,14 +41,16 @@ class ListModelMixin(object):
empty_error = u"Empty list and '%(class_name)s.allow_empty' is False." empty_error = u"Empty list and '%(class_name)s.allow_empty' is False."
def list(self, request, *args, **kwargs): def list(self, request, *args, **kwargs):
self.object_list = self.get_filtered_queryset() queryset = self.get_queryset()
self.object_list = self.filter_queryset(queryset)
# Default is to allow empty querysets. This can be altered by setting # Default is to allow empty querysets. This can be altered by setting
# `.allow_empty = False`, to raise 404 errors on empty querysets. # `.allow_empty = False`, to raise 404 errors on empty querysets.
allow_empty = self.get_allow_empty() allow_empty = self.get_allow_empty()
if not allow_empty and len(self.object_list) == 0: if not allow_empty and not self.object_list:
error_args = {'class_name': self.__class__.__name__} class_name = self.__class__.__name__
raise Http404(self.empty_error % error_args) error_msg = self.empty_error % {'class_name': class_name}
raise Http404(error_msg)
# Pagination size is set by the `.paginate_by` attribute, # Pagination size is set by the `.paginate_by` attribute,
# which may be `None` to disable pagination. # which may be `None` to disable pagination.
@ -82,17 +84,18 @@ class UpdateModelMixin(object):
def update(self, request, *args, **kwargs): def update(self, request, *args, **kwargs):
try: try:
self.object = self.get_object() self.object = self.get_object()
success_status = status.HTTP_200_OK created = False
except Http404: except Http404:
self.object = None self.object = None
success_status = status.HTTP_201_CREATED created = True
serializer = self.get_serializer(self.object, data=request.DATA) serializer = self.get_serializer(self.object, data=request.DATA)
if serializer.is_valid(): if serializer.is_valid():
self.pre_save(serializer.object) self.pre_save(serializer.object)
self.object = serializer.save() self.object = serializer.save()
return Response(serializer.data, status=success_status) status_code = created and status.HTTP_201_CREATED or status.HTTP_200_OK
return Response(serializer.data, status=status_code)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)