When I download a CSV of query results, the downloaded file reflects the query_id and the current date instead of the query name. This is different than what I expect: it should be the query name and today's date.
Basically, we specify the name of the file in the download attribute of the link:
<a target="_self" href="api/queries/67/results/466.csv" download="New_Query_2019_03_07.csv">
Download as CSV File
</a>
It doesn't work though, if there's also a Content-Disposition header, which in fact was added in https://github.com/getredash/redash/pull/3359.
@arikfr ok to just change the server formatting to include query.name instead of query.id?
str_key = None
if query is not None:
if query.name is not None:
str_key = query.name
else
str_key = str(query.id)
else:
str_key = str(query_result.id)
filename = "{}_{}.{}".format(str_key, str_date, filetype,)
@ranbena +1. Also, query name should also be sanitized: filename cannot include some characters, like /, \, :, #, etc. (need to check which are forbidden in each OS and replace all of them with _ or -)
The forbidden printable ASCII characters are:
/ (forward slash)
< (less than)
> (greater than)
: (colon - sometimes works, but is actually NTFS Alternate Data Streams)
" (double quote)
/ (forward slash)
\ (backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)
Also, all non-alphanumeric/punctuation characters should be removed (most of them user cannot type in query name, but _can_ copy/paste from somewhere).
https://github.com/un33k/python-slugify seems to do the job:
>>> slugify("1/2<3>4:5\"6\7|8?9*10 11.12,13!14'15=16+17_18#19--20茅")
'1-2-3-4-5-6-8-9-10-11-1213-14-15-16-17-18-19-20e'
It will transliterate national symbols - that's not needed in this case, all file systems can handle Unicode. Regex that matches \p{L}|\p{N}|\p{P}|\p{S} (any letters, numbers, punctuation, symbols) will be enough.
P.S. And remove punctuation mentioned in my previous comment, of course.
Here's the slugify function we use in the SaaS codebase: (in case it's helpful)
function slugify(text) {
return text.toString().toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text
}
Also need to be mindful of Unicode when constructing the filename (i.e. u'' instead of '').
@arikfr I'll take this if you don't mind
I don't mind :)
Just a note here:
If you rename the query, you need to execute it again otherwise the new name doesn't register.
Most helpful comment
Just a note here:
If you rename the query, you need to execute it again otherwise the new name doesn't register.