From a7e331aa387ca9f639ab1b5c61136efe517b0550 Mon Sep 17 00:00:00 2001 From: Scheianu Ionut Date: Wed, 10 Sep 2025 21:48:33 +0300 Subject: [PATCH] UI and logic improvements for warehouse CSV import: permanent preview table, create locations button, card reordering, and CSV format guidance. --- py_app/app/templates/create_locations.html | 54 +++++++++ .../app/templates/import_locations_csv.html | 71 +++++++++++ py_app/app/warehouse.py | 112 ++++++++++++++++++ 3 files changed, 237 insertions(+) create mode 100644 py_app/app/templates/create_locations.html create mode 100644 py_app/app/templates/import_locations_csv.html create mode 100644 py_app/app/warehouse.py diff --git a/py_app/app/templates/create_locations.html b/py_app/app/templates/create_locations.html new file mode 100644 index 0000000..469ac09 --- /dev/null +++ b/py_app/app/templates/create_locations.html @@ -0,0 +1,54 @@ +{% extends "base.html" %} +{% block title %}Create Warehouse Locations{% endblock %} +{% block content %} +
+ +
+

Add Warehouse Location

+ {% if message %} +
{{ message }}
+ {% endif %} +
+ +
+ +
+ +
+ +
+
+ +
+

Warehouse Locations

+ + + + + + + + + + + {% for loc in locations %} + + + + + + + {% endfor %} + +
IDLocation CodeSizeDescription
{{ loc[0] }}{{ loc[1] }}{{ loc[2] }}{{ loc[3] }}
+
+ +
+

Import Locations from CSV

+
+ Bulk import warehouse locations using a CSV file. + Go to Import Page +
+
+
+{% endblock %} diff --git a/py_app/app/templates/import_locations_csv.html b/py_app/app/templates/import_locations_csv.html new file mode 100644 index 0000000..5cc3a16 --- /dev/null +++ b/py_app/app/templates/import_locations_csv.html @@ -0,0 +1,71 @@ +{% extends "base.html" %} +{% block title %}Import Warehouse Locations from CSV{% endblock %} +{% block content %} +
+ +
+

Import Locations from CSV

+
+ +
+ + {% if locations %} + + {% endif %} +
+
+ +
+

Preview Table

+ + + + + + + + + + {% if locations %} + {% for loc in locations %} + + + + + + {% endfor %} + {% else %} + + {% endif %} + +
Location CodeSizeDescription
{{ loc[0] }}{{ loc[1] }}{{ loc[2] }}
No CSV file uploaded yet.
+
+ {% if report %} +
+

Import Report

+

{{ report }}

+
+ {% endif %} + +
+

CSV File Format

+ + + + + + + + + + + + + + + + +
Location CodeSizeDescription
EX123100Zona depozitare A
+
+
+{% endblock %} diff --git a/py_app/app/warehouse.py b/py_app/app/warehouse.py new file mode 100644 index 0000000..ad70b1f --- /dev/null +++ b/py_app/app/warehouse.py @@ -0,0 +1,112 @@ +import mariadb +from flask import current_app, request, render_template +import csv + +def get_db_connection(): + settings_file = current_app.instance_path + '/external_server.conf' + settings = {} + with open(settings_file, 'r') as f: + for line in f: + key, value = line.strip().split('=', 1) + settings[key] = value + return mariadb.connect( + user=settings['username'], + password=settings['password'], + host=settings['server_domain'], + port=int(settings['port']), + database=settings['database_name'] + ) + +def ensure_warehouse_locations_table(): + try: + conn = get_db_connection() + cursor = conn.cursor() + cursor.execute("SHOW TABLES LIKE 'warehouse_locations'") + result = cursor.fetchone() + if not result: + cursor.execute(''' + CREATE TABLE IF NOT EXISTS warehouse_locations ( + id BIGINT AUTO_INCREMENT PRIMARY KEY, + location_code VARCHAR(12) NOT NULL, + size INT, + description VARCHAR(250) + ) + ''') + conn.commit() + conn.close() + except Exception as e: + print(f"Error ensuring warehouse_locations table: {e}") + +# Add warehouse-specific functions below +def add_location(location_code, size, description): + conn = get_db_connection() + cursor = conn.cursor() + try: + cursor.execute( + "INSERT INTO warehouse_locations (location_code, size, description) VALUES (?, ?, ?)", + (location_code, size if size else None, description) + ) + conn.commit() + conn.close() + return "Location added successfully." + except mariadb.IntegrityError: + conn.close() + return "Failed: Location code already exists." + +def get_locations(): + conn = get_db_connection() + cursor = conn.cursor() + cursor.execute("SELECT id, location_code, size, description FROM warehouse_locations ORDER BY id DESC") + locations = cursor.fetchall() + conn.close() + return locations + +def recreate_warehouse_locations_table(): + conn = get_db_connection() + cursor = conn.cursor() + cursor.execute("DROP TABLE IF EXISTS warehouse_locations") + cursor.execute(""" + CREATE TABLE warehouse_locations ( + id INT AUTO_INCREMENT PRIMARY KEY, + location_code VARCHAR(12) UNIQUE NOT NULL, + size INT, + description VARCHAR(250) + ) + """) + conn.commit() + conn.close() + +def create_locations_handler(): + message = None + if request.method == "POST": + location_code = request.form.get("location_code") + size = request.form.get("size") + description = request.form.get("description") + message = add_location(location_code, size, description) + locations = get_locations() + return render_template("create_locations.html", locations=locations, message=message) + +def import_locations_csv_handler(): + report = None + locations = [] + errors = [] + 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)}" + return render_template('import_locations_csv.html', report=report, locations=locations)