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

#123 delete all button on report listing screen #181

Open
wants to merge 8 commits into
base: master
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
4 changes: 4 additions & 0 deletions notebooker/serialization/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,10 @@ def delete_result(self, job_id: AnyStr) -> Dict[str, Any]:
self.result_data_store.delete(filename)
return {"deleted_result_document": result, "gridfs_filenames": gridfs_filenames}

def delete_all_for_report_name(self, report_name: AnyStr) -> Dict[str, Any]:
results = self.get_all_result_keys(mongo_filter={"report_name": report_name})
return {job_id: self.delete_result(job_id) for _, job_id in results}


def _pdf_filename(job_id: str) -> str:
return f"{job_id}.pdf"
Expand Down
22 changes: 21 additions & 1 deletion notebooker/web/routes/report_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import Any, Dict, List, Tuple, NamedTuple, Optional, AnyStr

import nbformat
from flask import Blueprint, abort, jsonify, render_template, request, url_for, current_app
from flask import Blueprint, abort, jsonify, render_template, request, url_for, current_app, redirect
from nbformat import NotebookNode

from notebooker.constants import DEFAULT_RESULT_LIMIT
Expand Down Expand Up @@ -300,3 +300,23 @@ def delete_report(job_id):
except Exception:
error_info = traceback.format_exc()
return jsonify({"status": "error", "error": error_info}), 500


@run_report_bp.route("/delete_all_reports/<path:report_name>", methods=["GET", "POST"])
def delete_all_reports(report_name):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Needs a test please

"""
Deletes all reports associated with a particular report_name from the underlying storage.
Only marks as "status=deleted" so the report is retrievable at a later date.

:param report_name: The parameter here should be a "/"-delimited string which mirrors the directory structure of \
the notebook templates.

:return: A JSON which contains "status" which will either be "ok" or "error".
"""
try:
get_serializer().delete_all_for_report_name(report_name)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Where is this implemented?

get_all_result_keys(get_serializer(), limit=DEFAULT_RESULT_LIMIT, force_reload=True)
return redirect("/")
except Exception:
error_info = traceback.format_exc()
return jsonify({"status": "error", "error": error_info}), 500
76 changes: 74 additions & 2 deletions notebooker/web/templates/result_listing.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ <h1 class="ui huge centered header">{{ titleised_report_name }} ({{ report_name
</div>
{% endif %}
{% if not readonly_mode %}
<div class="three wide column">
<a class="ui button green" id="runReportButton" href="/run_report/{{ report_name }}">
<div class="three wide column" style="display: flex;">
<a class="ui button green" id="runReportButton" href="/run_report/{{ report_name }}" style="margin-right: 40px;">
Manually run this report
</a>
<button class="ui button red" id="deleteAllReportsButton" data-href="/delete_all_reports/{{ report_name }}">
Delete all reports
</button>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ideally we could add a Jest test for this as well please

</div>
{% endif %}
</div>
Expand All @@ -56,5 +59,74 @@ <h1 class="ui huge centered header">{{ titleised_report_name }} ({{ report_name
<div class="ui cancel button">Cancel</div>
</div>
</div>
<script type="application/javascript">
document.getElementById('deleteAllReportsButton').addEventListener('click', function(e) {
e.preventDefault();
var confirmed = confirm('Are you sure you wish to delete all reports?');
if (confirmed) {
// Show the spinner
document.getElementById('spinner').style.display = 'block';

// Use fetch API to make the request
fetch(this.getAttribute('data-href'))
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
// Hide the spinner
document.getElementById('spinner').style.display = 'none';
// Refresh the page or redirect as needed
window.location.reload();
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
// Hide the spinner
document.getElementById('spinner').style.display = 'none';
});
}
});
</script>
<!-- Spinner -->
<div id="spinner" style="display: none;">
<div class="loader"></div>
</div>

<!-- CSS for the spinner -->
<style>
#spinner {
display: flex;
justify-content: center;
align-items: center;
position: absolute;
z-index: 9999;
width: 100%;
height: 100%;
overflow: auto;
top: 0:
left: 0;
}

.loader {
border: 16px solid #f3f3f3;
border-radius: 50%;
border-top: 16px solid #3498db;
width: 120px;
height: 120px;
-webkit-animation: spin 2s linear infinite; /* Safari */
animation: spin 2s linear infinite;
margin: auto;
}

/* Safari */
@-webkit-keyframes spin {
0% { -webkit-transform: rotate(0deg); }
100% { -webkit-transform: rotate(360deg); }
}

@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
</body>
</html>