Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]: Fix EDB download buttons #1226

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions jwql/website/apps/jwql/data_containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,8 @@ def get_edb_components(request):
# If else to determine data visualization.
if type(mnemonic_query_result.data['euvalues'][0]) == np.str_:
if len(np.unique(mnemonic_query_result.data['euvalues'])) > 4:
mnemonic_table_result = mnemonic_query_result.get_table_data()
#mnemonic_table_result = mnemonic_query_result.get_table_data()
pass
else:
mnemonic_query_result_plot = mnemonic_query_result.bokeh_plot_text_data()
else:
Expand All @@ -650,27 +651,44 @@ def get_edb_components(request):
# generate table download in web app
result_table = mnemonic_query_result.data





# save file locally to be available for download
static_dir = os.path.join(settings.BASE_DIR, 'static')
ensure_dir_exists(static_dir)
file_name_root = 'mnemonic_query_result_table'
#static_dir = os.path.join(settings.BASE_DIR, 'static')
#ensure_dir_exists(static_dir)
file_name_root = f"{mnemonic_identifier}_{start_time.iso.split(' ')[0]}_{end_time.iso.split(' ')[0]}"
file_for_download = '{}.csv'.format(file_name_root)
path_for_download = os.path.join(static_dir, file_for_download)
#path_for_download = os.path.join(static_dir, file_for_download)


###############################################################
#####MOVE THESE LINES INTO views.download_edb_data

# add meta data to saved table
comments = []
comments.append('DMS EDB query of {}:'.format(mnemonic_identifier))
for key, value in mnemonic_query_result.info.items():
comments.append('{} = {}'.format(key, str(value)))
result_table.meta['comments'] = comments
comments.append(' ')
comments.append('Start time {}'.format(start_time.isot))
comments.append('End time {}'.format(end_time.isot))
comments.append('Number of rows {}'.format(len(result_table)))
comments.append(' ')
result_table.write(path_for_download, format='ascii.fixed_width',
overwrite=True, delimiter=',', bookend=False)
mnemonic_query_result.file_for_download = path_for_download
result_table.meta['comments'] = comments
#result_table.write(path_for_download, format='ascii.fixed_width',
# overwrite=True, delimiter=',', bookend=False)

#####MOVE THESE LINES INTO views.download_edb_data
###############################################################



mnemonic_query_result.file_for_download = file_for_download




# create forms for search fields not clicked
mnemonic_name_search_form = MnemonicSearchForm(prefix='mnemonic_name_search')
Expand Down
2 changes: 1 addition & 1 deletion jwql/website/apps/jwql/templates/engineering_database.html
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ <h4>Query for records of an EDB mnemonic</h4>
{% else %}
<b>
Query returned {{ edb_components['mnemonic_query_result'].data|length }} records:
<a id="download_exploration_result" class="btn btn-primary my-2 mx-2" role="button" href='{{ static("") }}{{ edb_components["mnemonic_query_result"].file_for_download }}' download>Download data</a>
<a id="download_exploration_result" class="btn btn-primary my-2 mx-2" role="button" href='{{ base_url }}/edb/data_table/' download>Download data</a>
</b></div>
{% if edb_components['mnemonic_table_result'] %}
{% autoescape off %}
Expand Down
1 change: 1 addition & 0 deletions jwql/website/apps/jwql/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
path('dashboard/', views.dashboard, name='dashboard'),
path('download_table/<str:tablename>', views.export, name='download_table'),
path('edb/', views.engineering_database, name='edb'),
path('edb/data_table', views.download_edb_data, name='download_edb_data')
path('jwqldb/', views.jwqldb_table_viewer, name='jwqldb'),
path('jwqldb/<str:tablename_param>', views.jwqldb_table_viewer, name='jwqldb_table_viewer'),
path('log_view/', views.log_view, name='log_view'),
Expand Down
36 changes: 36 additions & 0 deletions jwql/website/apps/jwql/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,42 @@ def dashboard(request):
return render(request, template, context)


def download_edb_data(request, mnemonic_result):
"""
"""
mnemonic_identifier = mnemonic_result.mnemonic_identifier
start_time = mnemonic_result.start_time
end_time = mnemonic_result.end_time

result_table = mnemonic_result.data

# add meta data to saved table
comments = []
comments.append('DMS EDB query of {}:'.format(mnemonic_identifier))
for key, value in mnemonic_query_result.info.items():
comments.append('{} = {}'.format(key, str(value)))
comments.append(' ')
comments.append('Start time {}'.format(start_time.isot))
comments.append('End time {}'.format(end_time.isot))
comments.append('Number of rows {}'.format(len(result_table)))
comments.append(' ')
result_table.meta['comments'] = comments

response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = f'attachment; filename="{mnemonic_query_result.file_for_download}"'

writer = csv.writer(response)

for comment in result_table.meta['comments']:
writer.writerow(comment)
for i in range(len(result_table)):
writer.writerow([result_table['dates'][i], result_table['euvalues'][i]])

now do we need a new html file that this sends the response to? probably.

return response


def download_report(request, inst):
"""Download data report by look status.

Expand Down