Skip to content

Commit

Permalink
Refactoring | extracting truthy values
Browse files Browse the repository at this point in the history
  • Loading branch information
snyaggarwal committed Jul 12, 2023
1 parent e19b0a6 commit b245841
Show file tree
Hide file tree
Showing 19 changed files with 157 additions and 105 deletions.
17 changes: 10 additions & 7 deletions core/bundles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from core.common.constants import CASCADE_LEVELS_PARAM, CASCADE_MAPPINGS_PARAM, \
CASCADE_HIERARCHY_PARAM, CASCADE_METHOD_PARAM, MAP_TYPES_PARAM, EXCLUDE_MAP_TYPES_PARAM, CASCADE_DIRECTION_PARAM, \
INCLUDE_RETIRED_PARAM, RETURN_MAP_TYPES, ALL, OMIT_IF_EXISTS_IN, EQUIVALENCY_MAP_TYPES, HEAD, INCLUDE_SELF
from core.common.utils import get_truthy_values

TRUTHY = get_truthy_values()


class Bundle:
Expand All @@ -13,8 +16,8 @@ def __init__(self, root, repo_version, params=None, verbose=False, requested_url
self.repo_version = repo_version
self.params = params
self.verbose = verbose
self.listing = params.get('listing', '') in ['true', 'True']
self.brief = params.get('brief', '') in ['true', 'True'] or (not self.verbose and not self.listing)
self.listing = params.get('listing', '') in TRUTHY
self.brief = params.get('brief', '') in TRUTHY or (not self.verbose and not self.listing)
self.root = root
self.reverse = False
self.cascade_hierarchy = True
Expand Down Expand Up @@ -61,7 +64,7 @@ def is_hierarchy_view(self):

def set_include_retired(self):
if INCLUDE_RETIRED_PARAM in self.params:
self.include_retired = self.params[INCLUDE_RETIRED_PARAM] in ['true', True]
self.include_retired = self.params[INCLUDE_RETIRED_PARAM] in TRUTHY

def set_map_types(self):
self.map_types = self.params.get(MAP_TYPES_PARAM) or None
Expand All @@ -77,26 +80,26 @@ def set_cascade_levels(self):

def set_cascade_mappings(self):
if CASCADE_MAPPINGS_PARAM in self.params:
self.cascade_mappings = self.params[CASCADE_MAPPINGS_PARAM] in ['true', True]
self.cascade_mappings = self.params[CASCADE_MAPPINGS_PARAM] in TRUTHY

def set_cascade_hierarchy(self):
if CASCADE_HIERARCHY_PARAM in self.params:
self.cascade_hierarchy = self.params[CASCADE_HIERARCHY_PARAM] in ['true', True]
self.cascade_hierarchy = self.params[CASCADE_HIERARCHY_PARAM] in TRUTHY

def set_cascade_method(self):
if CASCADE_METHOD_PARAM in self.params:
self.cascade_method = self.params.get(CASCADE_METHOD_PARAM, '').lower()

def set_cascade_direction(self):
if CASCADE_DIRECTION_PARAM in self.params:
self.reverse = self.params[CASCADE_DIRECTION_PARAM] in ['true', True]
self.reverse = self.params[CASCADE_DIRECTION_PARAM] in TRUTHY

def set_omit_if_exists_in(self):
self.omit_if_exists_in = self.params.get(OMIT_IF_EXISTS_IN, None) or None

def set_include_self(self):
if INCLUDE_SELF in self.params:
self.include_self = self.params[INCLUDE_SELF] in ['true', True]
self.include_self = self.params[INCLUDE_SELF] in TRUTHY

@property
def resource_type(self):
Expand Down
8 changes: 6 additions & 2 deletions core/collections/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@
index_expansion_mappings
from core.common.utils import drop_version, to_owner_uri, generate_temp_version, es_id_in, \
es_wildcard_search, get_resource_class_from_resource_name, get_exact_search_fields, to_snake_case, \
es_exact_search, es_to_pks, batch_qs, split_list_by_condition, decode_string, is_canonical_uri, encode_string
es_exact_search, es_to_pks, batch_qs, split_list_by_condition, decode_string, is_canonical_uri, encode_string, \
get_truthy_values
from core.concepts.constants import LOCALES_FULLY_SPECIFIED
from core.concepts.models import Concept
from core.mappings.models import Mapping


TRUTHY = get_truthy_values()


class Collection(ConceptContainerModel):
OBJECT_TYPE = COLLECTION_TYPE
OBJECT_VERSION_TYPE = COLLECTION_VERSION_TYPE
Expand Down Expand Up @@ -103,7 +107,7 @@ def get_search_document():
def get_base_queryset(cls, params):
collection = params.pop('collection', None)
contains_uri = params.pop('contains', None)
include_references = params.pop('include_references', None) in [True, 'true']
include_references = params.pop('include_references', None) in TRUTHY
queryset = super().get_base_queryset(params)
if collection:
queryset = queryset.filter(cls.get_exact_or_criteria('mnemonic', collection))
Expand Down
16 changes: 10 additions & 6 deletions core/collections/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@
from core.common.constants import HEAD, DEFAULT_ACCESS_TYPE, NAMESPACE_REGEX, ACCESS_TYPE_CHOICES, INCLUDE_SUMMARY, \
INCLUDE_CLIENT_CONFIGS, INVALID_EXPANSION_URL
from core.common.serializers import AbstractRepoResourcesSerializer
from core.common.utils import get_truthy_values
from core.orgs.models import Organization
from core.settings import DEFAULT_LOCALE
from core.sources.serializers import SourceVersionListSerializer
from core.users.models import UserProfile


TRUTHY = get_truthy_values()


class CollectionMinimalSerializer(ModelSerializer):
id = CharField(source='mnemonic')

Expand Down Expand Up @@ -66,7 +70,7 @@ def __init__(self, *args, **kwargs):
self.query_params = {}
if params:
self.query_params = params if isinstance(params, dict) else params.dict()
self.include_summary = self.query_params.get(INCLUDE_SUMMARY) in ['true', True]
self.include_summary = self.query_params.get(INCLUDE_SUMMARY) in TRUTHY
try:
if not self.include_summary:
self.fields.pop('summary', None)
Expand Down Expand Up @@ -380,8 +384,8 @@ def __init__(self, *args, **kwargs):
self.query_params = {}
if params:
self.query_params = params if isinstance(params, dict) else params.dict()
self.include_summary = self.query_params.get(INCLUDE_SUMMARY) in ['true', True]
self.include_client_configs = self.query_params.get(INCLUDE_CLIENT_CONFIGS) in ['true', True]
self.include_summary = self.query_params.get(INCLUDE_SUMMARY) in TRUTHY
self.include_client_configs = self.query_params.get(INCLUDE_CLIENT_CONFIGS) in TRUTHY

try:
if not self.include_summary:
Expand Down Expand Up @@ -459,7 +463,7 @@ def __init__(self, *args, **kwargs):
self.include_summary = False
if params:
self.query_params = params.dict()
self.include_summary = self.query_params.get(INCLUDE_SUMMARY) in ['true', True]
self.include_summary = self.query_params.get(INCLUDE_SUMMARY) in TRUTHY

try:
if not self.include_summary:
Expand Down Expand Up @@ -535,7 +539,7 @@ def __init__(self, *args, **kwargs):
self.include_summary = False
if params:
self.query_params = params.dict()
self.include_summary = self.query_params.get(INCLUDE_SUMMARY) in ['true', True]
self.include_summary = self.query_params.get(INCLUDE_SUMMARY) in TRUTHY

try:
if not self.include_summary:
Expand Down Expand Up @@ -575,7 +579,7 @@ def __init__(self, *args, **kwargs):
self.include_summary = False
if params:
self.query_params = params.dict()
self.include_summary = self.query_params.get(INCLUDE_SUMMARY) in ['true', True]
self.include_summary = self.query_params.get(INCLUDE_SUMMARY) in TRUTHY

try:
if not self.include_summary:
Expand Down
25 changes: 13 additions & 12 deletions core/common/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
CanViewConceptDictionaryVersion
from .checksums import ChecksumModel
from .utils import write_csv_to_s3, get_csv_from_s3, get_query_params_from_url_string, compact_dict_by_values, \
to_owner_uri, parse_updated_since_param, get_export_service, to_int
to_owner_uri, parse_updated_since_param, get_export_service, to_int, get_truthy_values

logger = logging.getLogger('oclapi')
TRUTHY = get_truthy_values()


class CustomPaginator:
Expand Down Expand Up @@ -201,19 +202,19 @@ def serialize_list(self, results, paginator=None):
return data

def should_include_facets(self):
return self.request.META.get(INCLUDE_FACETS, False) in ['true', True]
return self.request.META.get(INCLUDE_FACETS, False) in TRUTHY

def should_include_search_stats(self):
return self.request.META.get(INCLUDE_SEARCH_STATS, False) in ['true', True]
return self.request.META.get(INCLUDE_SEARCH_STATS, False) in TRUTHY

def only_facets(self):
return self.request.query_params.get(FACETS_ONLY, False) in ['true', True]
return self.request.query_params.get(FACETS_ONLY, False) in TRUTHY

def only_search_stats(self):
return self.request.query_params.get(SEARCH_STATS_ONLY, False) in ['true', True]
return self.request.query_params.get(SEARCH_STATS_ONLY, False) in TRUTHY

def should_compress(self):
return self.request.META.get(HTTP_COMPRESS_HEADER, False) in ['true', True]
return self.request.META.get(HTTP_COMPRESS_HEADER, False) in TRUTHY

def get_object_ids(self):
self.object_list.limit_iter = False
Expand Down Expand Up @@ -488,8 +489,8 @@ def apply_user_criteria(queryset, user):

@staticmethod
def apply_attribute_based_filters(queryset, params):
is_latest = params.get('is_latest', None) in [True, 'true']
include_retired = params.get(INCLUDE_RETIRED_PARAM, None) in [True, 'true']
is_latest = params.get('is_latest', None) in TRUTHY
include_retired = params.get(INCLUDE_RETIRED_PARAM, None) in TRUTHY
updated_since = parse_updated_since_param(params)
if is_latest:
queryset = queryset.filter(is_latest_version=True)
Expand Down Expand Up @@ -708,7 +709,7 @@ def get(self, request, *args, **kwargs): # pylint: disable=unused-argument
if version.has_export():
export_url = version.get_export_url()

no_redirect = request.query_params.get('noRedirect', False) in ['true', 'True', True]
no_redirect = request.query_params.get('noRedirect', False) in TRUTHY
if no_redirect:
return Response({'url': export_url}, status=status.HTTP_200_OK)

Expand Down Expand Up @@ -736,13 +737,13 @@ def post(self, request, *args, **kwargs): # pylint: disable=unused-argument
if version.is_exporting:
return Response(status=status.HTTP_208_ALREADY_REPORTED)

force_export = request.query_params.get('force', False) in ['true', 'True', True]
force_export = request.query_params.get('force', False) in TRUTHY

if force_export or not version.has_export():
status_code = self.handle_export_version()
return Response(status=status_code)

no_redirect = request.query_params.get('noRedirect', False) in ['true', 'True', True]
no_redirect = request.query_params.get('noRedirect', False) in TRUTHY
if no_redirect:
return Response(status=status.HTTP_204_NO_CONTENT)

Expand Down Expand Up @@ -781,7 +782,7 @@ def get_object(self, queryset=None): # pylint: disable=unused-argument

def get(self, request, *args, **kwargs): # pylint: disable=unused-argument
version = self.get_object()
is_debug = request.query_params.get('debug', None) in ['true', True]
is_debug = request.query_params.get('debug', None) in TRUTHY

if is_debug:
return Response({'is_processing': version.is_processing, 'process_ids': version._background_process_ids}) # pylint: disable=protected-access
Expand Down
7 changes: 5 additions & 2 deletions core/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from core.common.tasks import update_collection_active_concepts_count, update_collection_active_mappings_count, \
delete_s3_objects
from core.common.utils import reverse_resource, reverse_resource_version, parse_updated_since_param, drop_version, \
to_parent_uri, is_canonical_uri, get_export_service, from_string_to_date
to_parent_uri, is_canonical_uri, get_export_service, from_string_to_date, get_truthy_values
from core.common.utils import to_owner_uri
from core.settings import DEFAULT_LOCALE
from .checksums import ChecksumModel
Expand All @@ -34,6 +34,9 @@
update_source_active_concepts_count, update_source_active_mappings_count


TRUTHY = get_truthy_values()


class BaseModel(models.Model):
"""
Base model from which all resources inherit.
Expand Down Expand Up @@ -446,7 +449,7 @@ def get_base_queryset(cls, params):
username = params.get('user', None)
org = params.get('org', None)
version = params.get('version', None)
is_latest = params.get('is_latest', None) in [True, 'true']
is_latest = params.get('is_latest', None) in TRUTHY
updated_since = parse_updated_since_param(params)

queryset = cls.objects.filter(is_active=True)
Expand Down
4 changes: 3 additions & 1 deletion core/common/negotiation.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from rest_framework.negotiation import DefaultContentNegotiation

from core.common.utils import get_truthy_values


class OptionallyCompressContentNegotiation(DefaultContentNegotiation):
def select_renderer(self, request, renderers, format_suffix=None):
if request.META.get('HTTP_COMPRESS', False) in ['true', True]:
if request.META.get('HTTP_COMPRESS', False) in get_truthy_values():
renderers = self.filter_renderers(renderers, 'zip')
if renderers:
return renderers[0], 'application/zip'
Expand Down
11 changes: 7 additions & 4 deletions core/common/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
from core.value_sets.constants import RESOURCE_TYPE as VALUESET_RESOURCE_TYPE


TRUTHY = get_truthy_values()


class RootSerializer(Serializer): # pylint: disable=abstract-method
version = CharField()
routes = JSONField()
Expand Down Expand Up @@ -197,7 +200,7 @@ def __init__(self, *args, **kwargs): # pylint: disable=too-many-branches
params = get(request, 'query_params')
self.query_params = (params or {}) if isinstance(params, dict) else (params.dict() if params else {})
self.include_search_meta = self.query_params.get(
INCLUDE_SEARCH_META_PARAM) in get_truthy_values() and self.query_params.get('q')
INCLUDE_SEARCH_META_PARAM) in TRUTHY and self.query_params.get('q')

try:
if not self.include_search_meta:
Expand Down Expand Up @@ -231,9 +234,9 @@ def __init__(self, *args, **kwargs):
self.include_references = False
if params:
self.query_params = params if isinstance(params, dict) else params.dict()
self.include_concepts = self.query_params.get(INCLUDE_CONCEPTS_PARAM) in ['true', True]
self.include_mappings = self.query_params.get(INCLUDE_MAPPINGS_PARAM) in ['true', True]
self.include_references = self.query_params.get(INCLUDE_VERBOSE_REFERENCES) in ['true', True]
self.include_concepts = self.query_params.get(INCLUDE_CONCEPTS_PARAM) in TRUTHY
self.include_mappings = self.query_params.get(INCLUDE_MAPPINGS_PARAM) in TRUTHY
self.include_references = self.query_params.get(INCLUDE_VERBOSE_REFERENCES) in TRUTHY
self.limit = to_int(self.query_params.get(LIMIT_PARAM), DEFAULT_LIMIT)
self.offset = to_int(self.query_params.get(OFFSET_PARAM), 0)
try:
Expand Down
20 changes: 11 additions & 9 deletions core/common/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
from core.users.constants import USER_OBJECT_TYPE


TRUTHY = get_truthy_values()


class BaseAPIView(generics.GenericAPIView, PathWalkerMixin):
"""
An extension of generics.GenericAPIView that:
Expand Down Expand Up @@ -78,33 +81,32 @@ def _should_exclude_retired_from_search_results(self):
return False

params = get(self, 'params') or self.request.query_params.dict()
include_retired = params.get('retired', None) in [True, 'true'] or params.get(
INCLUDE_RETIRED_PARAM, None) in [True, 'true']
include_retired = params.get('retired', None) in TRUTHY or params.get(INCLUDE_RETIRED_PARAM, None) in TRUTHY
return not include_retired

def should_include_inactive(self):
return self.request.query_params.get(INCLUDE_INACTIVE) in ['true', True]
return self.request.query_params.get(INCLUDE_INACTIVE) in TRUTHY

def _should_include_private(self):
return self.is_user_document() or self.request.user.is_staff or self.is_user_scope()

def is_verbose(self):
return self.request.query_params.get(VERBOSE_PARAM, False) in ['true', True]
return self.request.query_params.get(VERBOSE_PARAM, False) in TRUTHY

def is_raw(self):
return self.request.query_params.get(RAW_PARAM, False) in ['true', True]
return self.request.query_params.get(RAW_PARAM, False) in TRUTHY

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

def is_hard_delete_requested(self):
return self.request.query_params.get('hardDelete', None) in ['true', True, 'True']
return self.request.query_params.get('hardDelete', None) in TRUTHY

def is_async_requested(self):
return self.request.query_params.get('async', None) in ['true', True, 'True']
return self.request.query_params.get('async', None) in TRUTHY

def is_inline_requested(self):
return self.request.query_params.get('inline', None) in ['true', True, 'True']
return self.request.query_params.get('inline', None) in TRUTHY

def verify_scope(self):
pass
Expand Down
Loading

0 comments on commit b245841

Please sign in to comment.