diff --git a/py_app/app/__pycache__/warehouse.cpython-312.pyc b/py_app/app/__pycache__/warehouse.cpython-312.pyc
index 563fc06..82cd5f0 100644
Binary files a/py_app/app/__pycache__/warehouse.cpython-312.pyc and b/py_app/app/__pycache__/warehouse.cpython-312.pyc differ
diff --git a/py_app/app/templates/create_locations.html b/py_app/app/templates/create_locations.html
index 469ac09..05d5649 100644
--- a/py_app/app/templates/create_locations.html
+++ b/py_app/app/templates/create_locations.html
@@ -17,6 +17,14 @@
+
+
+
Import Locations from CSV
+
+
@@ -42,13 +50,5 @@
-
-
{% endblock %}
diff --git a/py_app/app/templates/import_locations_csv.html b/py_app/app/templates/import_locations_csv.html
index 5cc3a16..6bd6635 100644
--- a/py_app/app/templates/import_locations_csv.html
+++ b/py_app/app/templates/import_locations_csv.html
@@ -2,20 +2,69 @@
{% block title %}Import Warehouse Locations from CSV{% endblock %}
{% block content %}
-
+
-
-
+
+
Preview Table
@@ -46,26 +95,5 @@
{{ report }}
{% endif %}
-
-
{% endblock %}
diff --git a/py_app/app/templates/main_page_warehouse.html b/py_app/app/templates/main_page_warehouse.html
index 56dcc6e..7b7cbfa 100644
--- a/py_app/app/templates/main_page_warehouse.html
+++ b/py_app/app/templates/main_page_warehouse.html
@@ -20,7 +20,7 @@
diff --git a/py_app/app/warehouse.py b/py_app/app/warehouse.py
index ad70b1f..e6ed2b5 100644
--- a/py_app/app/warehouse.py
+++ b/py_app/app/warehouse.py
@@ -1,6 +1,6 @@
import mariadb
-from flask import current_app, request, render_template
-import csv
+from flask import current_app, request, render_template, session, redirect, url_for
+import csv, os, tempfile
def get_db_connection():
settings_file = current_app.instance_path + '/external_server.conf'
@@ -90,23 +90,42 @@ def import_locations_csv_handler():
report = None
locations = []
errors = []
+ temp_dir = tempfile.gettempdir()
if request.method == 'POST':
file = request.files.get('csv_file')
if file and file.filename.endswith('.csv'):
- content = file.read().decode('utf-8').splitlines()
- reader = csv.DictReader(content)
- locations = [(row.get('location_code'), row.get('size'), row.get('description')) for row in reader]
- if request.form.get('create_locations'):
- added = 0
- failed = 0
- errors = []
- for loc in locations:
- location_code, size, description = loc
- result = add_location(location_code, size, description)
- if result and 'success' in result.lower():
- added += 1
- else:
- failed += 1
- errors.append(location_code)
- report = f"{added} locations were added to warehouse_locations table. {failed} locations failed: {', '.join(errors)}"
+ temp_path = os.path.join(temp_dir, file.filename)
+ file.save(temp_path)
+ session['csv_filename'] = file.filename
+ session['csv_filepath'] = temp_path
+ with open(temp_path, 'r', encoding='utf-8') as f:
+ reader = csv.DictReader(f)
+ locations = []
+ for row in reader:
+ location_code = row.get('Location Code') or row.get('location_code') or ''
+ size = row.get('Size') or row.get('size') or ''
+ description = row.get('Description') or row.get('description') or ''
+ locations.append((location_code, size, description))
+ session['csv_locations'] = locations
+ elif 'csv_locations' in session:
+ locations = session['csv_locations']
+ if request.form.get('create_locations') and locations:
+ added = 0
+ failed = 0
+ errors = []
+ for loc in locations:
+ location_code, size, description = loc
+ result = add_location(location_code, size, description)
+ if result and 'success' in result.lower():
+ added += 1
+ else:
+ failed += 1
+ errors.append(location_code or '')
+ report = f"{added} locations were added to warehouse_locations table. {failed} locations failed: {', '.join(errors)}"
+ session.pop('csv_locations', None)
+ session.pop('csv_filename', None)
+ session.pop('csv_filepath', None)
+ return redirect(url_for('warehouse.import_locations_csv') + '#created')
+ elif 'csv_locations' in session:
+ locations = session['csv_locations']
return render_template('import_locations_csv.html', report=report, locations=locations)