Skip to content

Commit 182052b

Browse files
CorentinCorentin
Corentin
authored and
Corentin
committed
Delete button as AJAX JSON request
1 parent 617a981 commit 182052b

File tree

5 files changed

+89
-17
lines changed

5 files changed

+89
-17
lines changed

app/imgannot/routes.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,27 @@ def upload_file():
8181

8282

8383
# To change to form insead of simple get
84-
@bp.route("/delete_image", methods=["GET", "POST"])
84+
@bp.route("/delete_image", methods=["POST"])
8585
@login_required
8686
def delete_image():
87-
"""Page to delete an image record from database"""
87+
"""Page to delete an image record from database from AJAX request"""
88+
# Get AJAX JSON data and parse it
89+
raw_data = request.get_data()
90+
parsed = json.loads(raw_data)
8891
image_requested = Image.query.filter_by(
89-
image_name=request.args.get("filename"),
90-
patient_id=request.args.get("patient_ID")).first()
91-
# Check if image to delete have been created by current user
92+
image_name=parsed["image_name"],
93+
patient_id=parsed["patient_id"]).first()
94+
# If current user is the creator of image: delete from DB
9295
if image_requested != None and image_requested.expert_id == current_user.id:
9396
db.session.delete(image_requested)
9497
db.session.commit()
95-
return redirect(url_for('imgannot.upload_file'))
98+
return json.dumps({"success": True}), 200, {
99+
"ContentType": "application/json"
100+
}
101+
# Error message if not the right user for given image
102+
else:
103+
flash('Unautorized database manipulation (delete_image)', "error")
104+
return redirect(url_for('imgannot.upload_file'))
96105

97106

98107
@bp.route("/annot", methods=["GET", "POST"])

app/ocr/routes.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import json
23

34
from app import db
45
from app.ocr import bp
@@ -149,14 +150,23 @@ def write_ocr_report():
149150
return render_template("ocr/ocr_sucess.html")
150151

151152

152-
@bp.route("/delete_pdf", methods=["GET", "POST"])
153+
@bp.route("/delete_pdf", methods=["POST"])
153154
@login_required
154155
def delete_pdf():
155-
"""Page to delete a PDF record from database"""
156+
"""Page to delete an PDF record from database from AJAX request"""
157+
# Get AJAX JSON data and parse it
158+
raw_data = request.get_data()
159+
parsed = json.loads(raw_data)
156160
pdf_requested = Pdf.query.filter_by(
157-
pdf_name=request.args.get("filename"),
158-
patient_id=request.args.get("patient_ID")).first()
161+
pdf_name=parsed["pdf_name"], patient_id=parsed["patient_id"]).first()
162+
# If current user is the creator of PDF: delete from DB
159163
if pdf_requested != None and pdf_requested.expert_id == current_user.id:
160164
db.session.delete(pdf_requested)
161165
db.session.commit()
162-
return redirect(url_for('ocr.upload_pdf'))
166+
return json.dumps({"success": True}), 200, {
167+
"ContentType": "application/json"
168+
}
169+
# Error message if not the right user for given PDF
170+
else:
171+
flash('Unautorized database manipulation (delete_pdf)', "error")
172+
return redirect(url_for('ocr.upload_file'))

app/templates/imgannot/upload_img.html

+27-3
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,35 @@ <h1>Image History</h1>
4848
<td>{{ image.patient_id }}</td>
4949
<td>{{ image.diagnostic }}</td>
5050
<td><a class="btn btn-success" role="button"
51-
href="annot?filename={{image.image_name}}&patient_ID={{image.patient_id}}">Resume</a> <a
52-
class="btn btn-danger" role="button"
53-
href="delete_image?filename={{image.image_name}}&patient_ID={{image.patient_id}}">Delete</a></td>
51+
href="annot?filename={{image.image_name}}&patient_ID={{image.patient_id}}">Resume</a>
52+
<button type="button" class="btn btn-danger"
53+
onclick="delete_image('{{image.image_name}}', '{{image.patient_id}}')">Delete</button>
54+
</td>
5455
</tr>
5556
{% endfor %}
5657
</table>
5758
</div>
59+
60+
<script>
61+
function delete_image(image_name, patient_id) {
62+
var dict = {
63+
"image_name": image_name,
64+
"patient_id": patient_id,
65+
};
66+
67+
var myJSON = JSON.stringify(dict);
68+
console.log(myJSON)
69+
$.ajax({
70+
type: "POST",
71+
url: "{{url_for('imgannot.delete_image')}}",
72+
data: myJSON,
73+
success: function () {
74+
console.log("Image deleted in Database Successfully");
75+
location.reload();
76+
},
77+
dataType: "text"
78+
});
79+
}
80+
</script>
81+
5882
{% endblock %}

app/templates/ocr/ocr_upload.html

+28-3
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,36 @@ <h1>PDF History</h1>
4949
<td>{{ pdf.patient_id }}</td>
5050
<td>{{ pdf.lang }}</td>
5151
<td><a class="btn btn-success" role="button"
52-
href="ocr_results?filename={{pdf.pdf_name}}&patient_ID={{pdf.patient_id}}">Resume</a> <a
53-
class="btn btn-danger" role="button"
54-
href="delete_pdf?filename={{pdf.pdf_name}}&patient_ID={{pdf.patient_id}}">Delete</a></td>
52+
href="ocr_results?filename={{pdf.pdf_name}}&patient_ID={{pdf.patient_id}}">Resume</a>
53+
<button type="button" class="btn btn-danger"
54+
onclick="delete_pdf('{{pdf.pdf_name}}', '{{pdf.patient_id}}')">Delete</button>
55+
</td>
5556
</tr>
5657
{% endfor %}
5758
</table>
5859
</div>
60+
61+
<script>
62+
function delete_pdf(pdf_name, patient_id) {
63+
var dict = {
64+
"pdf_name": pdf_name,
65+
"patient_id": patient_id,
66+
};
67+
68+
var myJSON = JSON.stringify(dict);
69+
console.log(myJSON)
70+
$.ajax({
71+
type: "POST",
72+
url: "{{url_for('ocr.delete_pdf')}}",
73+
data: myJSON,
74+
success: function () {
75+
console.log("PDF deleted in Database Successfully");
76+
location.reload();
77+
},
78+
dataType: "text"
79+
});
80+
}
81+
</script>
82+
83+
5984
{% endblock %}

logs/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Ignore everything in this directory
2+
*
3+
# Except this file
4+
!.gitignore

0 commit comments

Comments
 (0)