diff --git a/app.py b/app.py index 5b61065..c4eff22 100644 --- a/app.py +++ b/app.py @@ -1526,10 +1526,8 @@ def admin_panel(): purchased_count = l.purchased_count percent = (purchased_count / total_count * 100) if total_count > 0 else 0 comments_count = len([i for i in items if i.note and i.note.strip() != ""]) - receipt_pattern = f"list_{l.id}" - receipt_files = [f for f in all_files if receipt_pattern in f] + receipts_count = Receipt.query.filter_by(list_id=l.id).count() - # obliczenie czy wygasła if l.is_temporary and l.expires_at: expires_at = l.expires_at if expires_at.tzinfo is None: @@ -1545,7 +1543,7 @@ def admin_panel(): "purchased_count": purchased_count, "percent": round(percent), "comments_count": comments_count, - "receipts_count": len(receipt_files), + "receipts_count": receipts_count, "total_expense": l.total_expense, "expired": is_expired, } @@ -1743,7 +1741,44 @@ def admin_receipts(id): flash("Nieprawidłowe ID listy.", "danger") return redirect(url_for("admin_panel")) - return render_template("admin/receipts.html", receipts=receipts) + # Przeszukaj folder upload pod kątem „sierot” + upload_folder = app.config["UPLOAD_FOLDER"] + db_filenames = set(r.filename for r in receipts) + all_db_filenames = set(r.filename for r in Receipt.query.all()) # Wszystko z bazy + files_on_disk = set(os.listdir(upload_folder)) + stale_files = [ + f for f in files_on_disk + if f.endswith(".webp") and f not in all_db_filenames and f.startswith("list_") + ] + # Przekaż do template: receipts (z bazy) i orphan_files (sieroty) + return render_template( + "admin/receipts.html", + receipts=receipts, + orphan_files=stale_files, + orphan_files_count=len(stale_files) + ) + + +@app.route("/admin/delete_orphan_receipt_file/") +@login_required +@admin_required +def delete_orphan_receipt_file(filename): + upload_folder = app.config["UPLOAD_FOLDER"] + safe_filename = os.path.basename(filename) + file_path = os.path.join(upload_folder, safe_filename) + # Dowolnego pliku nie kasujemy jeśli jest w bazie (Receipt.filename) + if Receipt.query.filter_by(filename=safe_filename).first(): + flash("Nie możesz usunąć pliku powiązanego z bazą!", "danger") + return redirect(url_for("admin_receipts", id="all")) + if not os.path.exists(file_path): + flash("Plik już nie istnieje.", "warning") + else: + try: + os.remove(file_path) + flash(f"Usunięto plik: {safe_filename}", "success") + except Exception as e: + flash(f"Błąd przy usuwaniu pliku: {e}", "danger") + return redirect(url_for("admin_receipts", id="all")) @app.route("/admin/rotate_receipt/") diff --git a/templates/admin/receipts.html b/templates/admin/receipts.html index 3361b37..5673415 100644 --- a/templates/admin/receipts.html +++ b/templates/admin/receipts.html @@ -64,6 +64,31 @@ +{% if orphan_files %} +
+

🧐 Znalezione nieprzypisane pliki ({{ orphan_files_count }})

+
+ {% for f in orphan_files %} +
+
+ +
+

{{ f }}

+
Brak powiązania z listą!
+ + 🗑 Usuń plik z serwera + +
+
+
+ {% endfor %} +
+{% endif %} + + +