Skip to content

Commit

Permalink
Concept brief response '?brief=true' | returns uuid and id only
Browse files Browse the repository at this point in the history
  • Loading branch information
snyaggarwal committed Aug 5, 2021
1 parent 8c1be68 commit 34a4eff
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
1 change: 1 addition & 0 deletions core/common/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
SUPER_ADMIN_USER_ID = 1
OCL_ORG_ID = 1
VERBOSE_PARAM = 'verbose'
BRIEF_PARAM = 'brief'
UPDATED_SINCE_PARAM = 'updatedSince'
LAST_LOGIN_SINCE_PARAM = 'lastLoginSince'
LAST_LOGIN_BEFORE_PARAM = 'lastLoginBefore'
Expand Down
6 changes: 5 additions & 1 deletion core/common/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

from core import __version__
from core.common.constants import SEARCH_PARAM, LIST_DEFAULT_LIMIT, CSV_DEFAULT_LIMIT, \
LIMIT_PARAM, NOT_FOUND, MUST_SPECIFY_EXTRA_PARAM_IN_BODY, INCLUDE_RETIRED_PARAM, VERBOSE_PARAM, HEAD, LATEST
LIMIT_PARAM, NOT_FOUND, MUST_SPECIFY_EXTRA_PARAM_IN_BODY, INCLUDE_RETIRED_PARAM, VERBOSE_PARAM, HEAD, LATEST, \
BRIEF_PARAM
from core.common.mixins import PathWalkerMixin
from core.common.serializers import RootSerializer
from core.common.utils import compact_dict_by_values, to_snake_case, to_camel_case, parse_updated_since_param, \
Expand Down Expand Up @@ -65,6 +66,9 @@ def _should_include_private(self):
def is_verbose(self):
return self.request.query_params.get(VERBOSE_PARAM, False) in ['true', True]

def is_brief(self):
return self.request.query_params.get(BRIEF_PARAM, False) in ['true', True]

def initial(self, request, *args, **kwargs):
super().initial(request, *args, **kwargs)
self.initialize(request, request.path_info, **kwargs)
Expand Down
9 changes: 9 additions & 0 deletions core/concepts/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,15 @@ def get_versions(obj):
return obj.versions.count()


class ConceptMinimalSerializer(ModelSerializer):
uuid = CharField(source='id', read_only=True)
id = CharField(source='mnemonic', read_only=True)

class Meta:
model = Concept
fields = ('uuid', 'id')


class ConceptDetailSerializer(ModelSerializer):
uuid = CharField(source='id', read_only=True)
version = CharField(read_only=True)
Expand Down
33 changes: 22 additions & 11 deletions core/concepts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from core.concepts.serializers import (
ConceptDetailSerializer, ConceptListSerializer, ConceptDescriptionSerializer, ConceptNameSerializer,
ConceptVersionDetailSerializer,
ConceptVersionListSerializer, ConceptHierarchySerializer, ConceptSummarySerializer)
ConceptVersionListSerializer, ConceptHierarchySerializer, ConceptSummarySerializer, ConceptMinimalSerializer)
from core.mappings.serializers import MappingListSerializer


Expand All @@ -54,14 +54,19 @@ class ConceptVersionListAllView(ConceptBaseView, ListWithHeadersMixin):
permission_classes = (CanViewParentDictionary,)

def get_serializer_class(self):
return ConceptDetailSerializer if self.is_verbose() else ConceptListSerializer
if self.is_verbose():
return ConceptDetailSerializer
if self.is_brief():
return ConceptMinimalSerializer
return ConceptListSerializer

def get_queryset(self):
return Concept.global_listing_queryset(
self.get_filter_params(), self.request.user
).select_related(
'parent__organization', 'parent__user',
).prefetch_related('names', 'descriptions')
queryset = Concept.global_listing_queryset(self.get_filter_params(), self.request.user)
if self.is_brief():
return queryset

return queryset.select_related(
'parent__organization', 'parent__user',).prefetch_related('names', 'descriptions')

@swagger_auto_schema(
manual_parameters=[
Expand All @@ -84,7 +89,12 @@ def get_permissions(self):
return [CanViewParentDictionary(), ]

def get_serializer_class(self):
if (self.request.method == 'GET' and self.is_verbose()) or self.request.method == 'POST':
method = self.request.method
is_get = method == 'GET'

if is_get and self.is_brief():
return ConceptMinimalSerializer
if (is_get and self.is_verbose()) or method == 'POST':
return ConceptDetailSerializer

return ConceptListSerializer
Expand All @@ -96,9 +106,10 @@ def get_queryset(self):
if is_latest_version:
queryset = queryset.filter(is_latest_version=True)

return queryset.select_related(
'parent__organization', 'parent__user', 'created_by'
).prefetch_related('names')
if self.is_brief():
return queryset

return queryset.select_related('parent__organization', 'parent__user', 'created_by').prefetch_related('names')

def get(self, request, *args, **kwargs):
self.set_parent_resource(False)
Expand Down

0 comments on commit 34a4eff

Please sign in to comment.