uprawnienia ocr i uploadu
This commit is contained in:
61
app.py
61
app.py
@@ -8,9 +8,8 @@ import platform
|
||||
import psutil
|
||||
import secrets
|
||||
import hashlib
|
||||
|
||||
import re
|
||||
import tempfile
|
||||
|
||||
|
||||
from pillow_heif import register_heif_opener
|
||||
|
||||
@@ -228,17 +227,6 @@ def serve_css_lib(filename):
|
||||
|
||||
app.register_blueprint(static_bp)
|
||||
|
||||
|
||||
def user_has_list_access(list_obj, user):
|
||||
if not user.is_authenticated:
|
||||
return False
|
||||
if list_obj.owner_id == user.id:
|
||||
return True
|
||||
if db.session.query(SharedList).filter_by(list_id=list_obj.id, user_id=user.id).first():
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def allowed_file(filename):
|
||||
return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
|
||||
|
||||
@@ -376,7 +364,6 @@ def preprocess_image_for_tesseract(pil_image):
|
||||
|
||||
|
||||
def extract_total_tesseract(image):
|
||||
|
||||
text = pytesseract.image_to_string(image, lang="pol", config="--psm 6")
|
||||
lines = text.splitlines()
|
||||
candidates = []
|
||||
@@ -415,23 +402,30 @@ def extract_total_tesseract(image):
|
||||
except:
|
||||
continue
|
||||
|
||||
# Rozszerzone słowa kluczowe
|
||||
keywords = r"sum[aąo]?|razem|zapłat[ay]?|sprzedaż|opodatk|należność|do zapłaty"
|
||||
|
||||
preferred = [
|
||||
val
|
||||
for val, line in candidates
|
||||
if re.search(r"sum[aąo]?|razem|zapłaty", line.lower())
|
||||
if re.search(keywords, line.lower())
|
||||
]
|
||||
|
||||
if preferred:
|
||||
max_val = round(max(preferred), 2)
|
||||
return max_val, lines
|
||||
|
||||
# Fallback: wybierz największą wartość jeśli jest sensowna
|
||||
if candidates:
|
||||
max_val = round(max([val for val, _ in candidates]), 2)
|
||||
return max_val, lines
|
||||
# Jeśli np. większa niż 10 PLN, zakładamy że to może być suma końcowa
|
||||
if max_val >= 10:
|
||||
return max_val, lines
|
||||
|
||||
return 0.0, lines
|
||||
|
||||
|
||||
|
||||
############# END OCR #######################
|
||||
|
||||
|
||||
@@ -1050,9 +1044,6 @@ def all_products():
|
||||
@app.route("/upload_receipt/<int:list_id>", methods=["POST"])
|
||||
@login_required
|
||||
def upload_receipt(list_id):
|
||||
list_obj = db.session.get(ShoppingList, list_id)
|
||||
if not list_obj or not user_has_list_access(list_obj, current_user):
|
||||
return _receipt_error("Gość/niezalogowany nie może wgrywać plików")
|
||||
|
||||
if "receipt" not in request.files:
|
||||
return _receipt_error("Brak pliku")
|
||||
@@ -1062,8 +1053,6 @@ def upload_receipt(list_id):
|
||||
return _receipt_error("Nie wybrano pliku")
|
||||
|
||||
if file and allowed_file(file.filename):
|
||||
import hashlib
|
||||
|
||||
file_bytes = file.read()
|
||||
file.seek(0)
|
||||
file_hash = hashlib.sha256(file_bytes).hexdigest()
|
||||
@@ -1147,11 +1136,12 @@ def reorder_items():
|
||||
@app.route("/lists/<int:list_id>/analyze", methods=["POST"])
|
||||
@login_required
|
||||
def analyze_receipts_for_list(list_id):
|
||||
list_obj = db.session.get(ShoppingList, list_id)
|
||||
if not list_obj or not user_has_list_access(list_obj, current_user):
|
||||
return jsonify({"error": "Brak dostępu"}), 403
|
||||
|
||||
receipt_objs = Receipt.query.filter_by(list_id=list_id).all()
|
||||
existing_expenses = {
|
||||
e.receipt_filename for e in Expense.query.filter_by(list_id=list_id).all()
|
||||
if e.receipt_filename
|
||||
}
|
||||
|
||||
results = []
|
||||
total = 0.0
|
||||
|
||||
@@ -1171,13 +1161,18 @@ def analyze_receipts_for_list(list_id):
|
||||
value = 0.0
|
||||
lines = []
|
||||
|
||||
already_added = receipt.filename in existing_expenses
|
||||
|
||||
results.append({
|
||||
"id": receipt.id,
|
||||
"filename": receipt.filename,
|
||||
"amount": round(value, 2),
|
||||
"debug_text": lines,
|
||||
"already_added": already_added
|
||||
})
|
||||
total += value
|
||||
|
||||
if not already_added:
|
||||
total += value
|
||||
|
||||
return jsonify({"results": results, "total": round(total, 2)})
|
||||
|
||||
@@ -1483,8 +1478,6 @@ def generate_receipt_hash(receipt_id):
|
||||
flash("Plik nie istnieje", "danger")
|
||||
return redirect(request.referrer)
|
||||
|
||||
import hashlib
|
||||
|
||||
try:
|
||||
with open(file_path, "rb") as f:
|
||||
file_hash = hashlib.sha256(f.read()).hexdigest()
|
||||
@@ -2174,8 +2167,18 @@ def handle_update_note(data):
|
||||
def handle_add_expense(data):
|
||||
list_id = data["list_id"]
|
||||
amount = data["amount"]
|
||||
receipt_filename = data.get("receipt_filename")
|
||||
|
||||
if receipt_filename:
|
||||
existing = Expense.query.filter_by(list_id=list_id, receipt_filename=receipt_filename).first()
|
||||
if existing:
|
||||
return
|
||||
new_expense = Expense(
|
||||
list_id=list_id,
|
||||
amount=amount,
|
||||
receipt_filename=receipt_filename
|
||||
)
|
||||
|
||||
new_expense = Expense(list_id=list_id, amount=amount)
|
||||
db.session.add(new_expense)
|
||||
db.session.commit()
|
||||
|
||||
|
Reference in New Issue
Block a user