Skip to content

Commit

Permalink
all list api to have overriden http head request only
Browse files Browse the repository at this point in the history
  • Loading branch information
snyaggarwal committed Feb 11, 2021
1 parent 4cc2c3e commit 580efba
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 32 deletions.
16 changes: 5 additions & 11 deletions core/collections/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,14 @@ def get_permissions(self):
return [IsAuthenticated(), CanEditConceptDictionary()]


class CollectionRetrieveUpdateDestroyView(CollectionBaseView, ConceptDictionaryUpdateMixin):
class CollectionRetrieveUpdateDestroyView(CollectionBaseView, ConceptDictionaryUpdateMixin, RetrieveAPIView):
serializer_class = CollectionDetailSerializer

def get_object(self, queryset=None):
return self.get_queryset().filter(is_active=True).order_by('-created_at').first()
instance = self.get_queryset().filter(is_active=True).order_by('-created_at').first()
if not instance:
raise Http404()
return instance

def get_permissions(self):
if self.request.method in ['GET', 'HEAD']:
Expand All @@ -213,15 +216,6 @@ def delete(self, request, *args, **kwargs): # pylint: disable=unused-argument

return Response({'detail': DELETE_SUCCESS}, status=status.HTTP_204_NO_CONTENT)

def get(self, request, *args, **kwargs): # pylint: disable=unused-argument
instance = self.get_object()

if not instance:
return Response(status=status.HTTP_404_NOT_FOUND)

serializer = self.get_serializer(instance)
return Response(serializer.data)


class CollectionReferencesView(
CollectionBaseView, ConceptDictionaryUpdateMixin, RetrieveAPIView, DestroyAPIView, ListWithHeadersMixin
Expand Down
6 changes: 6 additions & 0 deletions core/common/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ class ListWithHeadersMixin(ListModelMixin):
object_list = None
limit = LIST_DEFAULT_LIMIT

def head(self, request, **kwargs): # pylint: disable=unused-argument
queryset = self.filter_queryset(self.get_queryset())
res = Response()
res['num_found'] = get(self, 'total_count') or queryset.count()
return res

def list(self, request, *args, **kwargs): # pylint:disable=too-many-locals
query_params = request.query_params.dict()
is_csv = query_params.get('csv', False)
Expand Down
8 changes: 1 addition & 7 deletions core/common/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.http import HttpResponse, Http404
from django.http import Http404
from django.shortcuts import get_object_or_404
from django.utils.functional import cached_property
from elasticsearch_dsl import Q
Expand Down Expand Up @@ -92,12 +92,6 @@ def destroy(self, request, *args, **kwargs): # pylint: disable=unused-argument
def get_host_url(self):
return self.request.META['wsgi.url_scheme'] + '://' + self.request.get_host()

def head(self, request, **kwargs): # pylint: disable=unused-argument
res = HttpResponse()
queryset = self.filter_queryset(self.get_queryset())
res['num_found'] = get(self, 'total_count') or queryset.count()
return res

def filter_queryset(self, queryset):
if self.is_searchable and self.should_perform_es_search():
return self.get_search_results_qs()
Expand Down
15 changes: 12 additions & 3 deletions core/concepts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,10 @@ def get_permissions(self):
return [CanEditParentDictionary()]

def get_object(self, queryset=None):
return super().get_queryset(None).first()
instance = super().get_queryset(None).first()
if not instance:
raise Http404()
return instance

def get_queryset(self, _=None):
if not self.parent_list_attribute:
Expand Down Expand Up @@ -323,10 +326,16 @@ def get_queryset(self, _=None):
return getattr(instance, self.parent_list_attribute).all()

def get_resource_object(self):
return super().get_queryset(None).first()
instance = super().get_queryset(None).first()
if not instance:
raise Http404()
return instance

def get_object(self, queryset=None):
return get(self.get_resource_object(), self.parent_list_attribute).filter(id=self.kwargs['uuid']).first()
instance = get(self.get_resource_object(), self.parent_list_attribute).filter(id=self.kwargs['uuid']).first()
if not instance:
raise Http404()
return instance

def update(self, request, **_):
partial = True
Expand Down
18 changes: 7 additions & 11 deletions core/sources/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from celery_once import AlreadyQueued
from django.core.exceptions import ValidationError
from django.db import IntegrityError
from django.http import Http404
from django.shortcuts import get_object_or_404
from drf_yasg.utils import swagger_auto_schema
from pydash import get
Expand Down Expand Up @@ -150,11 +151,15 @@ def get_permissions(self):
return [CanEditConceptDictionary()]


class SourceRetrieveUpdateDestroyView(SourceBaseView, ConceptDictionaryUpdateMixin):
class SourceRetrieveUpdateDestroyView(SourceBaseView, ConceptDictionaryUpdateMixin, RetrieveAPIView):
serializer_class = SourceDetailSerializer

def get_object(self, queryset=None):
return self.get_queryset().filter(is_active=True).order_by('-created_at').first()
instance = self.get_queryset().filter(is_active=True).order_by('-created_at').first()
if not instance:
raise Http404()

return instance

def get_permissions(self):
if self.request.method in ['GET', 'HEAD']:
Expand All @@ -171,15 +176,6 @@ def delete(self, request, *args, **kwargs): # pylint: disable=unused-argument

return Response({'detail': DELETE_SUCCESS}, status=status.HTTP_204_NO_CONTENT)

def get(self, request, *args, **kwargs): # pylint: disable=unused-argument
instance = self.get_object()

if not instance:
return Response(status=status.HTTP_404_NOT_FOUND)

serializer = self.get_serializer(instance)
return Response(serializer.data)


class SourceVersionListView(SourceVersionBaseView, mixins.CreateModelMixin, ListWithHeadersMixin):
released_filter = None
Expand Down

0 comments on commit 580efba

Please sign in to comment.