saved the creation of warehows locations
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user