uprawnienia ocr i uploadu
This commit is contained in:
40
app.py
40
app.py
@@ -227,6 +227,7 @@ def serve_css_lib(filename):
|
||||
|
||||
app.register_blueprint(static_bp)
|
||||
|
||||
|
||||
def allowed_file(filename):
|
||||
return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
|
||||
|
||||
@@ -405,11 +406,7 @@ def extract_total_tesseract(image):
|
||||
# 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(keywords, line.lower())
|
||||
]
|
||||
preferred = [val for val, line in candidates if re.search(keywords, line.lower())]
|
||||
|
||||
if preferred:
|
||||
max_val = round(max(preferred), 2)
|
||||
@@ -425,7 +422,6 @@ def extract_total_tesseract(image):
|
||||
return 0.0, lines
|
||||
|
||||
|
||||
|
||||
############# END OCR #######################
|
||||
|
||||
|
||||
@@ -1099,7 +1095,6 @@ def upload_receipt(list_id):
|
||||
return _receipt_error("Niedozwolony format pliku")
|
||||
|
||||
|
||||
|
||||
@app.route("/uploads/<filename>")
|
||||
def uploaded_file(filename):
|
||||
response = send_from_directory(app.config["UPLOAD_FOLDER"], filename)
|
||||
@@ -1138,7 +1133,8 @@ def reorder_items():
|
||||
def analyze_receipts_for_list(list_id):
|
||||
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()
|
||||
e.receipt_filename
|
||||
for e in Expense.query.filter_by(list_id=list_id).all()
|
||||
if e.receipt_filename
|
||||
}
|
||||
|
||||
@@ -1157,19 +1153,22 @@ def analyze_receipts_for_list(list_id):
|
||||
|
||||
except Exception as e:
|
||||
import traceback
|
||||
|
||||
print(f"OCR error for {receipt.filename}:\n{traceback.format_exc()}")
|
||||
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
|
||||
})
|
||||
results.append(
|
||||
{
|
||||
"id": receipt.id,
|
||||
"filename": receipt.filename,
|
||||
"amount": round(value, 2),
|
||||
"debug_text": lines,
|
||||
"already_added": already_added,
|
||||
}
|
||||
)
|
||||
|
||||
if not already_added:
|
||||
total += value
|
||||
@@ -1177,7 +1176,6 @@ def analyze_receipts_for_list(list_id):
|
||||
return jsonify({"results": results, "total": round(total, 2)})
|
||||
|
||||
|
||||
|
||||
@app.route("/admin")
|
||||
@login_required
|
||||
@admin_required
|
||||
@@ -2170,13 +2168,13 @@ def handle_add_expense(data):
|
||||
receipt_filename = data.get("receipt_filename")
|
||||
|
||||
if receipt_filename:
|
||||
existing = Expense.query.filter_by(list_id=list_id, receipt_filename=receipt_filename).first()
|
||||
existing = Expense.query.filter_by(
|
||||
list_id=list_id, receipt_filename=receipt_filename
|
||||
).first()
|
||||
if existing:
|
||||
return
|
||||
return
|
||||
new_expense = Expense(
|
||||
list_id=list_id,
|
||||
amount=amount,
|
||||
receipt_filename=receipt_filename
|
||||
list_id=list_id, amount=amount, receipt_filename=receipt_filename
|
||||
)
|
||||
|
||||
db.session.add(new_expense)
|
||||
|
Reference in New Issue
Block a user