update views and orders
This commit is contained in:
@@ -7,6 +7,7 @@ import mariadb
|
||||
from flask import current_app, request, render_template, session, redirect, url_for, flash
|
||||
import csv
|
||||
import os
|
||||
import json
|
||||
import tempfile
|
||||
from datetime import datetime
|
||||
|
||||
@@ -236,6 +237,24 @@ def process_csv_file(file_path):
|
||||
return orders_data, all_errors, all_warnings
|
||||
|
||||
def upload_orders_handler():
|
||||
# Handle clear table POST
|
||||
if request.method == 'POST' and request.form.get('clear_table') == '1':
|
||||
session.pop('orders_csv_data', None)
|
||||
session.pop('csv_filename', None)
|
||||
session.pop('orders_csv_filepath', None)
|
||||
session.pop('orders_validation_errors', None)
|
||||
session.pop('orders_validation_warnings', None)
|
||||
session.pop('leftover_orders', None)
|
||||
session.pop('leftover_description', None)
|
||||
# Remove the leftovers JSON file if it exists
|
||||
leftovers_path = os.path.join(current_app.root_path, 'static', 'leftover_orders.json')
|
||||
try:
|
||||
if os.path.exists(leftovers_path):
|
||||
os.remove(leftovers_path)
|
||||
except Exception:
|
||||
pass
|
||||
flash('Leftover orders have been cleared.', 'info')
|
||||
return redirect(url_for('main.view_orders'))
|
||||
"""
|
||||
Main handler for the upload orders functionality
|
||||
Handles both CSV upload/preview and database insertion
|
||||
@@ -295,12 +314,28 @@ def upload_orders_handler():
|
||||
success_count += 1
|
||||
else:
|
||||
failed_count += 1
|
||||
failed_orders.append(f"{order.get('comanda_productie', 'Unknown')}: {message}")
|
||||
order_with_error = order.copy()
|
||||
order_with_error['error_message'] = message
|
||||
failed_orders.append(order_with_error)
|
||||
|
||||
# Save leftovers to static/leftover_orders.json if any
|
||||
if failed_orders:
|
||||
static_folder = os.path.join(current_app.root_path, 'static')
|
||||
os.makedirs(static_folder, exist_ok=True)
|
||||
leftovers_path = os.path.join(static_folder, 'leftover_orders.json')
|
||||
with open(leftovers_path, 'w', encoding='utf-8') as f:
|
||||
json.dump(failed_orders, f, ensure_ascii=False, indent=2)
|
||||
|
||||
# Create report
|
||||
report = f"✅ Successfully imported {success_count} orders."
|
||||
if failed_count > 0:
|
||||
report += f" ❌ {failed_count} orders failed to import."
|
||||
leftover_description = (
|
||||
"These orders were not uploaded to the database because they already exist. "
|
||||
"Check your order list or modify them in the database."
|
||||
)
|
||||
session['leftover_orders'] = failed_orders
|
||||
session['leftover_description'] = leftover_description
|
||||
for failure in failed_orders[:5]: # Show first 5 failures
|
||||
report += f"<br>• {failure}"
|
||||
if len(failed_orders) > 5:
|
||||
@@ -313,10 +348,30 @@ def upload_orders_handler():
|
||||
session.pop('orders_csv_filepath', None)
|
||||
session.pop('orders_validation_errors', None)
|
||||
session.pop('orders_validation_warnings', None)
|
||||
session.pop('leftover_orders', None)
|
||||
session.pop('leftover_description', None)
|
||||
|
||||
flash(report, "success" if failed_count == 0 else "warning")
|
||||
|
||||
return redirect(url_for('main.view_orders'))
|
||||
if failed_count > 0:
|
||||
# Show leftover orders on the upload_orders page
|
||||
# Try to reload leftovers from JSON if available
|
||||
leftovers_path = os.path.join(current_app.root_path, 'static', 'leftover_orders.json')
|
||||
orders_data = session.get('leftover_orders', [])
|
||||
try:
|
||||
if os.path.exists(leftovers_path):
|
||||
with open(leftovers_path, 'r', encoding='utf-8') as f:
|
||||
orders_data = json.load(f)
|
||||
except Exception:
|
||||
pass
|
||||
leftover_description = session.get('leftover_description', '')
|
||||
return render_template('upload_orders.html',
|
||||
orders=orders_data,
|
||||
leftover_description=leftover_description,
|
||||
validation_errors=validation_errors,
|
||||
validation_warnings=validation_warnings,
|
||||
report=report)
|
||||
else:
|
||||
return redirect(url_for('main.view_orders'))
|
||||
|
||||
# Load data from session if available
|
||||
elif 'orders_csv_data' in session:
|
||||
|
||||
Reference in New Issue
Block a user