Fix foreign key cascades and add validation for missing AR fields
- Enable SQLite foreign key constraints to properly cascade deletes - Add validation to prevent adding products when no AR fields configured - Display helpful warnings on product pages when fields are missing - Redirect users to AR field configuration when needed
This commit is contained in:
@@ -10,6 +10,11 @@ def add_product(tenant_id):
|
|||||||
"""Add a new product"""
|
"""Add a new product"""
|
||||||
custom_fields = ARFieldModel.get_all(tenant_id)
|
custom_fields = ARFieldModel.get_all(tenant_id)
|
||||||
|
|
||||||
|
# Prevent adding products if no fields are configured
|
||||||
|
if not custom_fields:
|
||||||
|
flash('Cannot add products. Please configure AR fields first.', 'warning')
|
||||||
|
return redirect(url_for('admin.manage_ar_fields', tenant_id=tenant_id))
|
||||||
|
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
product_id = request.form.get('product_id')
|
product_id = request.form.get('product_id')
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ def get_db():
|
|||||||
db_path = current_app.config['DATABASE_PATH']
|
db_path = current_app.config['DATABASE_PATH']
|
||||||
conn = sqlite3.connect(db_path)
|
conn = sqlite3.connect(db_path)
|
||||||
conn.row_factory = sqlite3.Row
|
conn.row_factory = sqlite3.Row
|
||||||
|
# Enable foreign key constraints
|
||||||
|
conn.execute('PRAGMA foreign_keys = ON')
|
||||||
try:
|
try:
|
||||||
yield conn
|
yield conn
|
||||||
finally:
|
finally:
|
||||||
|
|||||||
@@ -10,6 +10,21 @@
|
|||||||
<h2>Add New Product</h2>
|
<h2>Add New Product</h2>
|
||||||
</div>
|
</div>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
|
{% if not custom_fields %}
|
||||||
|
<div class="alert alert-warning" role="alert">
|
||||||
|
<h4 class="alert-heading"><i class="bi bi-exclamation-triangle-fill me-2"></i>No Fields Configured</h4>
|
||||||
|
<p>Unable to add products. You must configure custom AR fields before adding products.</p>
|
||||||
|
<hr>
|
||||||
|
<p class="mb-0">
|
||||||
|
<a href="/{{ tenant_id }}/ar_fields" class="btn btn-primary">
|
||||||
|
<i class="bi bi-gear-fill me-1"></i>Configure AR Fields
|
||||||
|
</a>
|
||||||
|
<a href="/{{ tenant_id }}/" class="btn btn-secondary ms-2">
|
||||||
|
<i class="bi bi-arrow-left me-1"></i>Back to Products
|
||||||
|
</a>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
{% else %}
|
||||||
<form action="/{{ tenant_id }}/add" method="POST" enctype="multipart/form-data">
|
<form action="/{{ tenant_id }}/add" method="POST" enctype="multipart/form-data">
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label for="product_id" class="form-label">Product ID *</label>
|
<label for="product_id" class="form-label">Product ID *</label>
|
||||||
@@ -71,6 +86,7 @@
|
|||||||
<button type="submit" class="btn btn-primary">Add Product</button>
|
<button type="submit" class="btn btn-primary">Add Product</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -16,6 +16,18 @@
|
|||||||
<hr class="my-4">
|
<hr class="my-4">
|
||||||
|
|
||||||
<div class="mt-4">
|
<div class="mt-4">
|
||||||
|
{% if not custom_fields %}
|
||||||
|
<div class="alert alert-warning" role="alert">
|
||||||
|
<h4 class="alert-heading"><i class="bi bi-exclamation-triangle-fill me-2"></i>No AR Fields Configured</h4>
|
||||||
|
<p>Before you can add products, you need to configure custom AR fields that define what information is returned for each product.</p>
|
||||||
|
<hr>
|
||||||
|
<p class="mb-0">
|
||||||
|
<a href="/{{ tenant.id }}/ar_fields" class="btn btn-primary">
|
||||||
|
<i class="bi bi-gear-fill me-1"></i>Configure AR Fields
|
||||||
|
</a>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
{% else %}
|
||||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||||
<h2>Products</h2>
|
<h2>Products</h2>
|
||||||
<div>
|
<div>
|
||||||
@@ -27,7 +39,7 @@
|
|||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% if products %}
|
{% if products %}
|
||||||
<div class="table-responsive">
|
<div class="table-responsive">
|
||||||
<table class="table table-striped table-hover">
|
<table class="table table-striped table-hover">
|
||||||
@@ -91,6 +103,7 @@
|
|||||||
<i class="bi bi-info-circle"></i> No products available yet. Click "Add New Product" to get started.
|
<i class="bi bi-info-circle"></i> No products available yet. Click "Add New Product" to get started.
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="mt-4">
|
<div class="mt-4">
|
||||||
|
|||||||
Reference in New Issue
Block a user