This commit is contained in:
2025-09-17 20:47:22 +03:00
parent 76b8246edf
commit 18a1c638e1
9 changed files with 565 additions and 203 deletions

View File

@@ -939,6 +939,130 @@ def label_templates():
def create_template():
return render_template('create_template.html')
@bp.route('/get_database_tables', methods=['GET'])
def get_database_tables():
"""Get list of database tables for template creation"""
if 'role' not in session or session['role'] not in ['superadmin', 'warehouse_manager', 'etichete']:
return jsonify({'error': 'Access denied'}), 403
try:
# Get database connection using the same method as other functions
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
connection = mariadb.connect(
user=settings['username'],
password=settings['password'],
host=settings['server_domain'],
port=int(settings['port']),
database=settings['database_name']
)
cursor = connection.cursor()
# Get all tables in the database
cursor.execute("SHOW TABLES")
all_tables = [table[0] for table in cursor.fetchall()]
# Filter to show relevant tables (prioritize order_for_labels)
relevant_tables = []
if 'order_for_labels' in all_tables:
relevant_tables.append('order_for_labels')
# Add other potentially relevant tables
for table in all_tables:
if table not in relevant_tables and any(keyword in table.lower() for keyword in ['order', 'label', 'product', 'customer']):
relevant_tables.append(table)
connection.close()
return jsonify({
'success': True,
'tables': relevant_tables,
'recommended': 'order_for_labels'
})
except Exception as e:
return jsonify({'error': f'Database error: {str(e)}'}), 500
@bp.route('/get_table_columns/<table_name>', methods=['GET'])
def get_table_columns(table_name):
"""Get column names and descriptions for a specific table"""
if 'role' not in session or session['role'] not in ['superadmin', 'warehouse_manager', 'etichete']:
return jsonify({'error': 'Access denied'}), 403
try:
# Get database 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
connection = mariadb.connect(
user=settings['username'],
password=settings['password'],
host=settings['server_domain'],
port=int(settings['port']),
database=settings['database_name']
)
cursor = connection.cursor()
# Verify table exists
cursor.execute("SHOW TABLES LIKE %s", (table_name,))
if not cursor.fetchone():
return jsonify({'error': f'Table {table_name} not found'}), 404
# Get column information
cursor.execute(f"DESCRIBE {table_name}")
columns_info = cursor.fetchall()
columns = []
for col_info in columns_info:
column_name = col_info[0]
column_type = col_info[1]
is_nullable = col_info[2] == 'YES'
column_default = col_info[4]
# Get column comment if available
cursor.execute(f"""
SELECT COLUMN_COMMENT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = %s AND COLUMN_NAME = %s AND TABLE_SCHEMA = DATABASE()
""", (table_name, column_name))
comment_result = cursor.fetchone()
column_comment = comment_result[0] if comment_result and comment_result[0] else ''
# Create user-friendly description
description = column_comment or column_name.replace('_', ' ').title()
columns.append({
'name': column_name,
'type': column_type,
'nullable': is_nullable,
'default': column_default,
'description': description,
'field_id': f"db_{column_name}" # Unique ID for template fields
})
connection.close()
return jsonify({
'success': True,
'table': table_name,
'columns': columns
})
except Exception as e:
return jsonify({'error': f'Database error: {str(e)}'}), 500
@bp.route('/edit_template/<int:template_id>')
def edit_template(template_id):
# Logic for editing a template will go here