Skip to content

Commit

Permalink
Bringing device name sanitation from master. (#94)
Browse files Browse the repository at this point in the history
* Sanitising device names in data export code.

* Now using HTML5 datepicker.

* Restored lost spinner.
  • Loading branch information
dajtxx authored Jul 22, 2024
1 parent 3b88382 commit 9bf91a3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
6 changes: 4 additions & 2 deletions src/www/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ def DownloadData():

msgs = get_messages(token, l_uid, start, end)
if len(msgs) < 1:
return 'Success', 200
return 'No messages.', 204

dataset = []
for msg in msgs:
Expand All @@ -663,7 +663,9 @@ def DownloadData():
df.to_csv(buffer, encoding='UTF-8')
buffer.seek(0)

return send_file(buffer, as_attachment=True, download_name=f'{logical_dev.name}.csv')
sanitised_dev_name = re.sub(r'[^a-zA-Z0-9_-]', '', logical_dev.name)

return send_file(buffer, as_attachment=True, download_name=f'{sanitised_dev_name}.csv')


except requests.exceptions.HTTPError as e:
Expand Down
36 changes: 20 additions & 16 deletions src/www/app/templates/logical_device_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
if ('Export'.localeCompare(dialog.returnValue) === 0) {
try {
showSpinner();

console.log('Exporting data');
const formElement = document.getElementById('single-ld-export');
const formData = new FormData(formElement);
Expand All @@ -62,40 +63,44 @@
body: formData
})
.then(async response => {
if (response.status == 204) {
alert('No messages found in selected date range.');
return;
}

if (!response.ok) {
throw new Error('Network response was not ok');
}
const blob = await response.blob();
const filename = response.headers.get('Content-Disposition')?.split('filename=')[1]?.replace(/['"]/g, '') || 'download.csv';

// Check if the showSaveFilePicker API is available
if ('showSaveFilePicker' in window) {
try {
try {
const handle = await window.showSaveFilePicker({
suggestedName: filename,
types: [{
suggestedName: filename,
types: [{
description: 'CSV File',
accept: {'text/csv': ['.csv']},
}],
}],
});
const writable = await handle.createWritable();
await writable.write(blob);
await writable.close();
} catch (err) {
} catch (err) {
if (err.name !== 'AbortError') {
console.error('Failed to save file:', err);
// Fallback to the older method
saveBlobAsFile(blob, filename);
console.error('Failed to save file:', err);
// Fallback to the older method
saveBlobAsFile(blob, filename);
}
}
}
} else {
// Fallback for browsers that don't support showSaveFilePicker
saveBlobAsFile(blob, filename);
// Fallback for browsers that don't support showSaveFilePicker
saveBlobAsFile(blob, filename);
}
})
.catch(error => {
console.error('Error:', error);
// Does this show up when the spinnder is active?
alert("Error occured on submission", error);
});

Expand All @@ -117,8 +122,7 @@
dialog.addEventListener("close", doFetch);
});

function exportData(l_uid, name) {
console.log("exportData " + l_uid + " " + name);
function exportData(l_uid) {
dialog.returnValue = "Cancel";
dialog.showModal();
}
Expand Down Expand Up @@ -180,7 +184,7 @@ <h3 style="padding: 8px">Export data</h3>
<div class="command-bar">
<div class="form-buttons">
<ul>
<li><span class="btn" onclick="exportData('{{ ld_data.uid }}', '{{ ld_data.name}}')">Export Data</span></li>
<li><span class="btn" onclick="exportData('{{ ld_data.uid }}')">Export Data</span></li>
<li><span class="btn" onclick="handleSubmit('device-form', 'Are you sure you want to Save?')">Save</span></li>
<li><span class="btn" onclick="handleMapping('logical device')">Update Mapping</span></li>
<li><span class="btn" onclick="handleEndMapping('{{ ld_data.uid }}', 'LD')">End Mapping</span></li>
Expand Down

0 comments on commit 9bf91a3

Please sign in to comment.