Add 'download results as CSV'

This commit is contained in:
Kane York 2015-08-03 15:07:29 -07:00
parent d1a58e83a2
commit 1601f4f269
4 changed files with 87 additions and 56 deletions

View File

@ -100,8 +100,7 @@ const QueryResultComponent = Ember.Component.extend({
});
}.property('content', 'columns.@each'),
actions: {
downloadResult() {
downloadResult(format) {
// Create a frame to submit the form in (?)
// to avoid leaving an about:blank behind
let windowName = randomIdShort();
@ -112,7 +111,7 @@ const QueryResultComponent = Ember.Component.extend({
let form = document.createElement("form");
form.setAttribute('id', 'query-download-result');
form.setAttribute('method', 'post');
form.setAttribute('action', Discourse.getURL('/admin/plugins/explorer/queries/' + this.get('query.id') + '/run.json?download=1'));
form.setAttribute('action', Discourse.getURL('/admin/plugins/explorer/queries/' + this.get('query.id') + '/run.' + format + '?download=1'));
form.setAttribute('target', windowName);
form.setAttribute('style', 'display:none;');
@ -137,6 +136,14 @@ const QueryResultComponent = Ember.Component.extend({
document.body.removeChild(form);
})
});
},
actions: {
downloadResultJson() {
this.downloadResult('json');
},
downloadResultCsv() {
this.downloadResult('csv');
}
},

View File

@ -1,5 +1,7 @@
<div class="result-info">
{{d-button action="downloadResult" icon="download" label="explorer.download_json"}}
{{i18n "explorer.download"}}
{{d-button action="downloadResultJson" icon="download" label="explorer.download_json"}}
{{d-button action="downloadResultCsv" icon="download" label="explorer.download_csv"}}
<div class="result-about">
{{duration}}
</div>

View File

@ -46,13 +46,15 @@ en:
edit: "Edit"
delete: "Delete"
recover: "Undelete Query"
download_json: "Download Results"
download: "Download Results"
download_json: "JSON"
download_csv: "CSV"
others_dirty: "A query has unsaved changes that will be lost if you navigate away."
run_time: "Query completed in {{value}} ms."
column: "Column {{number}}"
explain_label: "Include query plan?"
save_params: "Set Defaults"
reset_params: "Reset"
https_warning: "Use of the Data Explorer on sites not protected by HTTPS is discouraged. Please be careful to not retrieve sensitive information over insecure links."
https_warning: "This site is not protected by HTTPS. Please be careful to not retrieve sensitive information over insecure links."
no_queries: "There are no queries. Why not "
no_queries_hook: "create one?"

View File

@ -917,8 +917,6 @@ SQL
check_xhr unless params[:download]
query = DataExplorer::Query.find(params[:id].to_i)
if params[:download]
response.headers['Content-Disposition'] =
"attachment; filename=#{query.slug}@#{Slug.for(Discourse.current_hostname, 'discourse')}-#{Date.today}.dcqresult.json"
response.sending_file = true
end
@ -952,6 +950,12 @@ SQL
else
pg_result = result[:pg_result]
cols = pg_result.fields
respond_to do |format|
format.json do
if params[:download]
response.headers['Content-Disposition'] =
"attachment; filename=#{query.slug}@#{Slug.for(Discourse.current_hostname, 'discourse')}-#{Date.today}.dcqresult.json"
end
json = {
success: true,
errors: [],
@ -970,6 +974,22 @@ SQL
render json: json
end
format.csv do
response.headers['Content-Disposition'] =
"attachment; filename=#{query.slug}@#{Slug.for(Discourse.current_hostname, 'discourse')}-#{Date.today}.dcqresult.csv"
require 'csv'
text = CSV.generate do |csv|
csv << cols
pg_result.values.each do |row|
csv << row
end
end
render text: text
end
end
end
end
end