From 3ebb364322869eaec1d1eacf2bb9951241190bf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Gruszczy=C5=84ski?= Date: Sun, 20 Jul 2025 17:34:21 +0200 Subject: [PATCH] webp support --- receitp_to_list.py => add_receipt_to_list.py | 0 alters.txt | 3 ++ app.py | 29 ++++++-------- templates/admin/edit_list.html | 11 ++++++ templates/admin/receipts.html | 10 +++-- update_missing_image_data.py | 40 ++++++++++++++++++++ 6 files changed, 73 insertions(+), 20 deletions(-) rename receitp_to_list.py => add_receipt_to_list.py (100%) create mode 100644 update_missing_image_data.py diff --git a/receitp_to_list.py b/add_receipt_to_list.py similarity index 100% rename from receitp_to_list.py rename to add_receipt_to_list.py diff --git a/alters.txt b/alters.txt index 27ee04c..7bb3d9a 100644 --- a/alters.txt +++ b/alters.txt @@ -46,3 +46,6 @@ CREATE TABLE receipt ( uploaded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (list_id) REFERENCES shopping_list(id) ); + +ALTER TABLE receipt ADD COLUMN filesize INTEGER; + diff --git a/app.py b/app.py index 2a1de6e..9d41a74 100644 --- a/app.py +++ b/app.py @@ -147,9 +147,8 @@ class Receipt(db.Model): list_id = db.Column(db.Integer, db.ForeignKey("shopping_list.id"), nullable=False) filename = db.Column(db.String(255), nullable=False) uploaded_at = db.Column(db.DateTime, default=datetime.utcnow) - shopping_list = db.relationship("ShoppingList", backref="receipts", lazy=True) - + filesize = db.Column(db.Integer, nullable=True) with app.app_context(): db.create_all() @@ -298,6 +297,7 @@ def delete_receipts_for_list(list_id): print(f"Nie udało się usunąć pliku {filename}: {e}") + # zabezpieczenie logowani do systemu - błędne hasła def is_ip_blocked(ip): now = time.time() @@ -935,6 +935,7 @@ def upload_receipt(list_id): return redirect(request.referrer) """ +from datetime import datetime @app.route("/upload_receipt/", methods=["POST"]) def upload_receipt(list_id): @@ -953,7 +954,15 @@ def upload_receipt(list_id): save_resized_image(file, file_path) - new_receipt = Receipt(list_id=list_id, filename=webp_filename) + filesize = os.path.getsize(file_path) if os.path.exists(file_path) else None + uploaded_at = datetime.utcnow() + + new_receipt = Receipt( + list_id=list_id, + filename=webp_filename, + filesize=filesize, + uploaded_at=uploaded_at + ) db.session.add(new_receipt) db.session.commit() @@ -967,12 +976,6 @@ def upload_receipt(list_id): return _receipt_error("Niedozwolony format pliku") -def _receipt_error(message): - if request.is_json or request.headers.get("X-Requested-With") == "XMLHttpRequest": - return jsonify({"success": False, "message": message}), 400 - flash(message, "danger") - return redirect(request.referrer) - @app.route("/uploads/") @@ -1195,9 +1198,6 @@ def delete_user(user_id): flash("Użytkownik usunięty", "success") return redirect(url_for("list_users")) - -import os - @app.route("/admin/receipts/") @login_required @admin_required @@ -1216,14 +1216,9 @@ def admin_receipts(id): flash("Nieprawidłowe ID listy.", "danger") return redirect(url_for("admin_panel")) - for r in receipts: - path = os.path.join(app.config["UPLOAD_FOLDER"], r.filename) - r.filesize = os.path.getsize(path) if os.path.exists(path) else 0 - return render_template("admin/receipts.html", receipts=receipts) - @app.route("/admin/delete_receipt/") @login_required @admin_required diff --git a/templates/admin/edit_list.html b/templates/admin/edit_list.html index bda1085..37d58a6 100644 --- a/templates/admin/edit_list.html +++ b/templates/admin/edit_list.html @@ -202,13 +202,24 @@

{{ r.filename }}

+ + {% if r.filesize and r.filesize >= 1024 * 1024 %} +

Rozmiar: {{ (r.filesize / 1024 / 1024) | round(2) }} MB

+ {% elif r.filesize %} +

Rozmiar: {{ (r.filesize / 1024) | round(1) }} kB

+ {% else %} +

Rozmiar nieznany

+ {% endif %} +

Wgrano: {{ r.uploaded_at.strftime('%Y-%m-%d %H:%M') }}

+ 🗑️ Usuń
{% endfor %} + {% if not receipts %} diff --git a/templates/admin/receipts.html b/templates/admin/receipts.html index d8db03f..9c70dd7 100644 --- a/templates/admin/receipts.html +++ b/templates/admin/receipts.html @@ -21,15 +21,19 @@

{{ r.filename }}

Wgrano: {{ r.uploaded_at.strftime('%Y-%m-%d %H:%M') }}

- {% if r.filesize >= 1024 * 1024 %} + {% if r.filesize and r.filesize >= 1024 * 1024 %}

Rozmiar: {{ (r.filesize / 1024 / 1024) | round(2) }} MB

- {% else %} + {% elif r.filesize %}

Rozmiar: {{ (r.filesize / 1024) | round(1) }} kB

+ {% else %} +

Brak danych o rozmiarze

{% endif %} + ✏️ Edytuj listę #{{ r.list_id }} - 🗑️ Usuń +
diff --git a/update_missing_image_data.py b/update_missing_image_data.py new file mode 100644 index 0000000..6a19c8c --- /dev/null +++ b/update_missing_image_data.py @@ -0,0 +1,40 @@ +import os +from datetime import datetime +from app import app, db, Receipt + +def update_missing_receipt_fields(): + with app.app_context(): + folder = app.config["UPLOAD_FOLDER"] + updated = 0 + + receipts = Receipt.query.filter( + (Receipt.filesize == None) | (Receipt.filesize == 0) | (Receipt.uploaded_at == None) + ).all() + + for r in receipts: + path = os.path.join(folder, r.filename) + if not os.path.exists(path): + print(f"Brak pliku: {r.filename}") + continue + + changed = False + + if not r.filesize: + r.filesize = os.path.getsize(path) + changed = True + print(f"{r.filename} → filesize: {r.filesize} B") + + if not r.uploaded_at: + timestamp = os.path.getmtime(path) + r.uploaded_at = datetime.fromtimestamp(timestamp) + changed = True + print(f"{r.filename} → uploaded_at: {r.uploaded_at}") + + if changed: + updated += 1 + + db.session.commit() + print(f"\nZaktualizowano {updated} rekordów.") + +if __name__ == "__main__": + update_missing_receipt_fields()