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

Filename set when /results called directly #3359

Merged
merged 5 commits into from
Jan 29, 2019
Merged
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions redash/handlers/query_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ def get(self, query_id=None, query_result_id=None, filetype='json'):
max_age = int(request.args.get('maxAge', 0))

query_result = None
query = None

if query_result_id:
query_result = get_object_or_404(models.QueryResult.get_by_id_and_org, query_result_id, self.current_org)
Expand Down Expand Up @@ -276,6 +277,16 @@ def get(self, query_id=None, query_result_id=None, filetype='json'):
if should_cache:
response.headers.add_header('Cache-Control', 'private,max-age=%d' % ONE_YEAR)

str_date = query_result.retrieved_at.strftime("%Y_%m_%d")

if query is not None:
filename = query.name.replace(" ", "_") + "_" + str_date
else:
filename = str(query_result.id) + "_" + str_date
filename += "." + filetype
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, I think that on the backend it will be safer to just use the query id (when available) instead of the name to avoid any unicode issues 😔 (for example, at the moment, line 288 will probably throw an error if the query name has unicode characters)

I would also use .format here. Something along the lines of:

if query is not None:
    prefix = query.id
else:
    prefix = query_result.id

filename = "{}_{}.{}".format(prefix, str_date, filetype)

What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh silly me! I literally had format method used right below that code (when using add_header). I've made some changes according to it.


response.headers.add_header("Content-Disposition", 'attachment; filename="{}"'.format(filename,))

return response

else:
Expand Down