Skip to content

Commit

Permalink
Add ability to specify whether to include related frames
Browse files Browse the repository at this point in the history
This is on by default, but currently users will be able to request to exclude related frames from their queries. Also update the get_queryset method to avoid prefetching related frames if we know we're not going to use them.
  • Loading branch information
mgdaily committed Oct 7, 2024
1 parent faeb484 commit fa5cb5d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
8 changes: 4 additions & 4 deletions archive/frames/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
23 changes: 17 additions & 6 deletions archive/frames/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand All @@ -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')
Expand Down
1 change: 0 additions & 1 deletion archive/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit fa5cb5d

Please sign in to comment.