diff --git a/archive/frames/models.py b/archive/frames/models.py index 7e2f79f..b8f2ffe 100644 --- a/archive/frames/models.py +++ b/archive/frames/models.py @@ -134,7 +134,7 @@ def get_header_dict(self): archive_settings.PUBLIC_DATE_KEY: self.public_date, } - def as_dict(self, include_thumbnails=False): + def as_dict(self, include_thumbnails=False, include_related_frames=False): ret_dict = model_to_dict(self, exclude=('related_frames', 'area')) ret_dict['version_set'] = [v.as_dict() for v in self.version_set.all()] ret_dict['url'] = self.url if self.version_set.exists() else None @@ -159,9 +159,9 @@ def as_dict(self, include_thumbnails=False): if self.area: ret_dict['area'] = json.loads(self.area.geojson) - if self.configuration_type in settings.EXCLUDE_RELATED_FRAME_TYPES: - ret_dict['related_frames'] = [] - else: + if include_thumbnails: + ret_dict['thumbnails'] = [t.as_dict() for t in Thumbnail.objects.filter(frame=self)] + if include_related_frames: ret_dict['related_frames'] = list(self.related_frames.all().values_list('id', flat=True)) return ret_dict diff --git a/archive/frames/views.py b/archive/frames/views.py index a59b20e..b7664c3 100644 --- a/archive/frames/views.py +++ b/archive/frames/views.py @@ -58,7 +58,7 @@ class FrameViewSet(viewsets.ModelViewSet): ordering_fields = ('id', 'basename', 'observation_date', 'primary_optical_element', 'configuration_type', 'proposal_id', 'instrument_id', 'target_name', 'reduction_level', 'exposure_time') - def get_queryset(self): + def get_queryset(self, include_related_frames=False): """ Filter frames depending on the logged in user. Admin users see all frames, excluding ones which have no versions. @@ -69,9 +69,11 @@ def get_queryset(self): queryset = ( Frame.objects.exclude(observation_date=None) .prefetch_related('version_set') - .prefetch_related(Prefetch('related_frames', queryset=Frame.objects.all().only('id'))) .prefetch_related('thumbnails') ) + # We're not including related frames in the response, so don't prefetch them + if include_related_frames: + queryset = queryset.prefetch_related(Prefetch('related_frames', queryset=Frame.objects.all().only('id'))) if self.request.user.is_superuser: return queryset elif self.request.user.is_authenticated: @@ -84,16 +86,25 @@ def get_queryset(self): # These two method overrides just force the use of the as_dict method for serialization for list and detail endpoints def list(self, request, *args, **kwargs): - queryset = self.filter_queryset(self.get_queryset()) - page = self.paginate_queryset(queryset) + # TODO: Default to not include related frames once we've announced it to users + include_related_frames = True + if request.query_params.get('include_related_frames', '').lower() == 'false': + include_related_frames = False include_thumbnails = True if request.query_params.get('include_thumbnails', '').lower() == 'true' else False - json_models = [model.as_dict(include_thumbnails) for model in page] + + queryset = self.filter_queryset(self.get_queryset(include_related_frames)) + page = self.paginate_queryset(queryset) + json_models = [model.as_dict(include_thumbnails, include_related_frames) for model in page] json_models = [model for model in json_models if model['version_set']] # Filter out frames with no versions return self.get_paginated_response(json_models) def retrieve(self, request, *args, **kwargs): instance = self.get_object() - return Response(instance.as_dict()) + include_thumbnails = True if request.query_params.get('include_thumbnails', '').lower() == 'true' else False + include_related_frames = True + if request.query_params.get('include_related_frames', '').lower() == 'false': + include_related_frames = False + return Response(instance.as_dict(include_thumbnails, include_related_frames)) def create(self, request): basename = request.data.get('basename') diff --git a/archive/settings.py b/archive/settings.py index d438d1d..b855444 100644 --- a/archive/settings.py +++ b/archive/settings.py @@ -248,7 +248,6 @@ CONFIGDB_URL = os.getenv('CONFIGDB_URL', '') CONFIGURATION_TYPES = get_tuple_from_environment('CONFIGURATION_TYPES', 'BIAS,DARK,EXPOSE,SPECTRUM,LAMPFLAT,SKYFLAT,STANDARD,TRAILED,GUIDE,EXPERIMENTAL,CATALOG') SCIENCE_CONFIGURATION_TYPES = get_tuple_from_environment('SCIENCE_CONFIGURATION_TYPES', 'EXPOSE,TARGET,SPECTRUM,CATALOG,OBJECT') -EXCLUDE_RELATED_FRAME_TYPES = get_tuple_from_environment('EXCLUDE_RELATED_FRAME_TYPES', 'BPM,') # Additional Customization ZIP_DOWNLOAD_FILENAME_BASE = os.getenv('ZIP_DOWNLOAD_FILENAME_BASE', 'ocs_archive_data')