Skip to content

Commit 8859618

Browse files
committed
delete fixed
1 parent fd593b6 commit 8859618

1 file changed

Lines changed: 7 additions & 35 deletions

File tree

backend/app.py

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,16 @@ def decorated(*args, **kwargs):
5959

6060
# ── HELPER: fetch all images + find doc by filename ────────
6161
def get_doc_id_by_filename(filename):
62-
"""
63-
Calls LOGIC_READ and returns the Cosmos DB document id
64-
matching the given filename. Returns None if not found.
65-
Also logs the raw response for debugging.
66-
"""
6762
response = requests.get(LOGIC_READ, timeout=30)
6863
logger.info(f"LOGIC_READ status: {response.status_code}")
69-
logger.info(f"LOGIC_READ preview: {response.text[:500]}")
7064

7165
data = response.json()
7266
images = data if isinstance(data, list) else data.get("value", [])
7367

7468
logger.info(f"Total images returned: {len(images)}")
75-
if images:
76-
logger.info(f"Sample image keys: {list(images[0].keys())}")
7769

7870
for img in images:
7971
if img.get("filename") == filename:
80-
# Cosmos DB NoSQL uses 'id' but check common variants too
8172
doc_id = img.get("id") or img.get("_id") or img.get("ID")
8273
logger.info(f"Found match — filename: {filename}, doc_id: {doc_id}")
8374
return doc_id
@@ -208,7 +199,7 @@ def handle_upload():
208199
flash(f"Upload failed: {e}", "error")
209200
return redirect(url_for("index"))
210201

211-
# ── DELETE (DELETE via Logic App) ─────────────────────────
202+
# ── DELETE (POST to Logic App — DELETE method doesn't support body)
212203
@app.route("/delete/<filename>", methods=["POST"])
213204
@login_required
214205
def delete_image(filename):
@@ -223,53 +214,34 @@ def delete_image(filename):
223214
except Exception as e:
224215
logger.warning(f"Blob delete failed (may not exist): {e}")
225216

226-
# Look up Cosmos DB doc id and call LOGIC_DELETE
217+
# Look up Cosmos DB doc id and call LOGIC_DELETE via POST
227218
try:
228219
doc_id = get_doc_id_by_filename(filename)
229220

230221
if doc_id:
231222
delete_payload = {"id": doc_id}
232223
logger.info(f"Calling LOGIC_DELETE with payload: {delete_payload}")
233-
r = requests.request("DELETE", LOGIC_DELETE, json=delete_payload, timeout=30)
224+
# Use POST — Azure Logic Apps HTTP trigger rejects body on DELETE requests
225+
r = requests.post(LOGIC_DELETE, json=delete_payload, timeout=30)
234226
logger.info(f"LOGIC_DELETE status: {r.status_code}, response: {r.text[:200]}")
235227
flash("Image deleted.", "info")
236228
else:
237-
flash("Image not found in database — blob removed but DB record may remain.", "warning")
229+
flash("Image not found in database.", "warning")
238230

239231
except Exception as e:
240232
logger.error(f"Delete error: {e}")
241233
flash(f"Delete failed: {e}", "error")
242234

243235
return redirect(url_for("index"))
244236

245-
@app.route("/test-delete")
246-
def test_delete():
247-
import json
248-
# Use a real doc id from your Cosmos DB
249-
test_id = "bcfa52fb-b5a5-4834-9c50-4c4d867faf85"
250-
payload = {"id": test_id}
251-
try:
252-
r = requests.request("DELETE", f"{LOGIC_DELETE}&docid={doc_id}", timeout=30)
253-
return {
254-
"LOGIC_DELETE_URL": LOGIC_DELETE,
255-
"payload_sent": payload,
256-
"status_code": r.status_code,
257-
"response": r.text[:500]
258-
}
259-
except Exception as e:
260-
return {"error": str(e), "LOGIC_DELETE_URL": LOGIC_DELETE}
261-
262-
263-
264-
# ── UPDATE (UPDATE via Logic App) ──────────────────────────
237+
# ── UPDATE (PATCH via Logic App) ───────────────────────────
265238
@app.route("/update/<filename>", methods=["POST"])
266239
@login_required
267240
def update_image(filename):
268241
if not session.get("is_admin"):
269242
flash("Only admin can edit images.", "error")
270243
return redirect(url_for("index"))
271244

272-
# Must look up real Cosmos DB doc id — filename is NOT the id
273245
try:
274246
doc_id = get_doc_id_by_filename(filename)
275247

@@ -279,7 +251,7 @@ def update_image(filename):
279251

280252
tags = [t.strip() for t in request.form.get("tags", "").split(",") if t.strip()]
281253
payload = {
282-
"id": doc_id, # ← fixed: was sending filename before
254+
"id": doc_id,
283255
"title": request.form.get("title", "").strip(),
284256
"description": request.form.get("description", "").strip(),
285257
"category": request.form.get("category", "").strip(),

0 commit comments

Comments
 (0)