Skip to content

Commit

Permalink
Add confirmation before deletion (#289)
Browse files Browse the repository at this point in the history
Prompt user for confirmation before deleting storage service
or fetch job.
  • Loading branch information
mcantelon committed Nov 21, 2024
1 parent 911875f commit 285fc37
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
8 changes: 8 additions & 0 deletions AIPscan/Aggregator/templates/confirm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% extends "base.html" %}

{% block content %}
<form method="GET" action="{{ request.url }}">
<input type="hidden" name="confirm" value="1" />
<input type="submit" value="{{ action }}" class='btn btn-danger' /> <a href="{{ url_for(cancel_route) }}" class='btn btn-default'>Cancel</a>
</form>
{% endblock %}
2 changes: 1 addition & 1 deletion AIPscan/Aggregator/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ def test_delete_fetch_job(app_with_populated_files, mocker):

mocker.patch("AIPscan.Aggregator.tasks.delete_fetch_job.delay")

response = test_client.get("/aggregator/delete_fetch_job/1")
response = test_client.get("/aggregator/delete_fetch_job/1?confirm=1")
assert response.status_code == 302
4 changes: 3 additions & 1 deletion AIPscan/Aggregator/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
url_for,
)

from AIPscan import db, typesense_helpers
from AIPscan import db, decorators, typesense_helpers
from AIPscan.Aggregator import database_helpers, tasks
from AIPscan.Aggregator.forms import StorageServiceForm
from AIPscan.Aggregator.task_helpers import (
Expand Down Expand Up @@ -171,6 +171,7 @@ def new_storage_service():


@aggregator.route("/delete_storage_service/<storage_service_id>", methods=["GET"])
@decorators.confirm_required("Delete storage service", "aggregator.ss_default")
def delete_storage_service(storage_service_id):
storage_service = StorageService.query.get(storage_service_id)

Expand Down Expand Up @@ -241,6 +242,7 @@ def new_fetch_job(fetch_job_id):


@aggregator.route("/delete_fetch_job/<fetch_job_id>", methods=["GET"])
@decorators.confirm_required("Delete fetch job", "aggregator.ss_default")
def delete_fetch_job(fetch_job_id):
fetch_job = FetchJob.query.get(fetch_job_id)

Expand Down
21 changes: 21 additions & 0 deletions AIPscan/decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from functools import wraps

from flask import render_template, request


def confirm_required(action, cancel_route):
def decorator(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if request.args.get("confirm"):
return f(*args, **kwargs)

return render_template(
"confirm.html",
action=action,
cancel_route=cancel_route,
)

return decorated_function

return decorator

0 comments on commit 285fc37

Please sign in to comment.