Skip to content

Commit

Permalink
Merge pull request #1561 from BradleySappington/download_csv
Browse files Browse the repository at this point in the history
WIP: Add Download CSV button to query page
  • Loading branch information
mfixstsci committed May 3, 2024
2 parents 60beaeb + 80986ca commit a728d9c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
5 changes: 3 additions & 2 deletions jwql/website/apps/jwql/templates/jwql_query.html
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,9 @@ <h1>Dynamic Query Form</h1>
</div>
<hr class="col-md-12">
<div class='form-group'>
<input type="submit" value="Submit" class="btn btn-primary" name="jwstqueryform" />
<input type="reset" value="Clear Form" class="btn btn-primary mx-4" name="jwstqueryform" onclick="hide_all();" />
<input type="submit" value="Submit" class="btn btn-primary" name="submit_jwstqueryform" />
<input type="submit" value="Download" class="btn btn-primary" name="download_jwstqueryform" />
<input type="reset" value="Clear Form" class="btn btn-primary mx-4" name="reset_jwstqueryform" onclick="hide_all();" />
</div>
</div>
</form>
Expand Down
2 changes: 2 additions & 0 deletions jwql/website/apps/jwql/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
path('jwqldb/<str:tablename_param>', views.jwqldb_table_viewer, name='jwqldb_table_viewer'),
path('log_view/', views.log_view, name='log_view'),
path('query_submit/', views.query_submit, name='query_submit'),
path('query_download/', views.query_download, name='query_download'),

re_path(r'^(?P<inst>({}))/$'.format(instruments), views.instrument, name='instrument'),
re_path(r'^(?P<inst>({}))/archive/$'.format(instruments), views.archived_proposals, name='archive'),
re_path(r'^(?P<inst>({}))/unlooked/$'.format(instruments), views.unlooked_images, name='unlooked'),
Expand Down
41 changes: 38 additions & 3 deletions jwql/website/apps/jwql/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,13 @@
from django.core.paginator import Paginator
from django.http import HttpResponse, JsonResponse
from django.shortcuts import redirect, render
import numpy as np
from sqlalchemy import inspect

from jwql.database.database_interface import load_connection
from jwql.utils import monitor_utils
from jwql.utils.interactive_preview_image import InteractivePreviewImg
from jwql.utils.constants import JWST_INSTRUMENT_NAMES_MIXEDCASE, URL_DICT, QUERY_CONFIG_TEMPLATE, QueryConfigKeys
from jwql.utils.utils import filename_parser, filesystem_path, get_base_url, get_config
from jwql.utils.utils import filename_parser, get_base_url, get_config
from jwql.utils.utils import get_rootnames_for_instrument_proposal, query_unformat

from .data_containers import build_table
Expand Down Expand Up @@ -142,7 +141,13 @@ def jwql_query(request):

# save the query config settings to a session
request.session['query_config'] = parameters
return redirect('/query_submit')
# Check if the download button value exists in the POST message (meaning Download was pressed)
download_button_value = request.POST.get('download_jwstqueryform', None)
if(download_button_value):
return redirect('/query_download')
else:
# submit was pressed go to the query_submit page
return redirect('/query_submit')

context = {'form': form,
'inst': ''}
Expand Down Expand Up @@ -771,6 +776,36 @@ def query_submit(request):
return render(request, template, context)


def query_download(request):
"""Download query results in csv format
Parameters
----------
request : HttpRequest object
Incoming request from the webpage.
Returns
-------
response : HttpResponse object
Outgoing response sent to the webpage (csv file to be downloaded)
"""
parameters = request.session.get("query_config", QUERY_CONFIG_TEMPLATE.copy())
filtered_rootnames = get_rootnames_from_query(parameters)

today = datetime.datetime.now().strftime('%Y%m%d_%H:%M')
filename = f'jwql_query_{today}.csv'
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = f'attachment; filename="{filename}"'

header_row = ["Index", "Name"]
writer = csv.writer(response)
writer.writerow(header_row)
for index, rootname in enumerate(filtered_rootnames):
writer.writerow([index, rootname])

return response


def unlooked_images(request, inst):
"""Generate the page listing all unlooked images in the database
Expand Down

0 comments on commit a728d9c

Please sign in to comment.