From a727796ed2ab53199c0a0a853e3286f96c9c8dda Mon Sep 17 00:00:00 2001 From: Matt Hills Date: Mon, 20 Oct 2025 21:13:05 -0400 Subject: [PATCH] Add optional default product fields on tenant creation When creating a new tenant, users can now choose to initialize with default product fields (Product Name, Item ID, Sale Price, and Image). The option is presented via a checkbox in the tenant creation modal and is enabled by default. --- src/app/blueprints/tenant/routes.py | 13 +++++- src/app/models/ar_field.py | 53 +++++++++++++++++++++++++ src/app/templates/tenant_selection.html | 16 +++++++- 3 files changed, 80 insertions(+), 2 deletions(-) diff --git a/src/app/blueprints/tenant/routes.py b/src/app/blueprints/tenant/routes.py index d6bfc37..967e23c 100644 --- a/src/app/blueprints/tenant/routes.py +++ b/src/app/blueprints/tenant/routes.py @@ -13,9 +13,20 @@ def index(tenant_id): if tenant is None: return jsonify({"error": f"'{tenant_id}' is a reserved name and cannot be used as a tenant ID"}), 404 - products = ProductModel.get_all(tenant_id) + # Check if we should create default fields for a new tenant + create_defaults = request.args.get('create_defaults', '0') == '1' custom_fields = ARFieldModel.get_all(tenant_id) + # If no custom fields exist and user requested defaults, create them + if create_defaults and not custom_fields: + ARFieldModel.create_default_fields(tenant_id) + custom_fields = ARFieldModel.get_all(tenant_id) + flash('Tenant created with default product fields!', 'success') + # Redirect to remove the query parameter + return redirect(f'/{tenant_id}/') + + products = ProductModel.get_all(tenant_id) + return render_template('index.html', tenant=tenant, products=products, custom_fields=custom_fields) @tenant_bp.route('/delete', methods=['POST']) diff --git a/src/app/models/ar_field.py b/src/app/models/ar_field.py index 8e63f7c..80aed46 100644 --- a/src/app/models/ar_field.py +++ b/src/app/models/ar_field.py @@ -82,3 +82,56 @@ class ARFieldModel: cursor.execute('DELETE FROM custom_ar_fields WHERE id = ? AND tenant_id = ?', (field_id, tenant_id)) conn.commit() + + @staticmethod + def create_default_fields(tenant_id: str): + """Create default product fields for a new tenant""" + tenant_id = tenant_id.lower() + + default_fields = [ + { + 'fieldName': '_name', + 'label': 'Product Name', + 'fieldType': 'TEXT', + 'editable': 'true', + 'displayOrder': 0 + }, + { + 'fieldName': '_id', + 'label': 'Item ID', + 'fieldType': 'TEXT', + 'editable': 'false', + 'displayOrder': 1 + }, + { + 'fieldName': '_price', + 'label': 'Sale Price', + 'fieldType': 'TEXT', + 'editable': 'true', + 'displayOrder': 2 + }, + { + 'fieldName': '_image', + 'label': 'Image', + 'fieldType': 'IMAGE_URI', + 'editable': 'true', + 'displayOrder': 3 + } + ] + + with get_db() as conn: + cursor = conn.cursor() + for field in default_fields: + cursor.execute(''' + INSERT INTO custom_ar_fields + (tenant_id, field_name, label, field_type, editable, display_order) + VALUES (?, ?, ?, ?, ?, ?) + ''', ( + tenant_id, + field['fieldName'], + field['label'], + field['fieldType'], + field['editable'], + field['displayOrder'] + )) + conn.commit() diff --git a/src/app/templates/tenant_selection.html b/src/app/templates/tenant_selection.html index 7540360..ab4de58 100644 --- a/src/app/templates/tenant_selection.html +++ b/src/app/templates/tenant_selection.html @@ -160,6 +160,15 @@ Only letters, numbers, hyphens, and underscores allowed +
+
+ + +
+ Adds: Product Name, Item ID, Sale Price, and Image fields +
Default credentials: admin / admin @@ -237,13 +246,18 @@ document.getElementById('newTenantForm').addEventListener('submit', function(e) { e.preventDefault(); const tenantId = document.getElementById('tenantId').value; + const createDefaults = document.getElementById('createDefaultFields').checked; + if (tenantId) { const normalizedId = tenantId.toLowerCase(); if (reservedIds.includes(normalizedId)) { alert(`"${tenantId}" is a reserved name and cannot be used as a tenant ID.`); return; } - window.location.href = '/' + normalizedId + '/'; + + // Redirect with query parameter for default fields + const url = '/' + normalizedId + '/' + (createDefaults ? '?create_defaults=1' : ''); + window.location.href = url; } });