Fix: Add missing printing module table and improve database management error handling
- Added order_for_labels table to init_db.py (printing/labels module) - Fixed fetch API calls in database management to properly check HTTP response status before parsing JSON - Prevents 'Unexpected token' errors when server returns HTML error pages instead of JSON - Improved error messages for better debugging (shows HTTP status codes) - All 10 database management API endpoints now have proper error handling
This commit is contained in:
@@ -477,7 +477,10 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(response => {
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
alert('Cleanup completed! ' + (data.deleted_count || 0) + ' old backups deleted.');
|
||||
@@ -512,7 +515,10 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(response => {
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
alert('Backup created successfully: ' + data.file);
|
||||
@@ -677,7 +683,15 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
},
|
||||
body: JSON.stringify({ table: table })
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
if (response.status === 401) {
|
||||
throw new Error('Unauthorized. Please log in again.');
|
||||
}
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
// Hide modal
|
||||
@@ -701,16 +715,16 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
} else {
|
||||
alert('Error: ' + data.error);
|
||||
// Re-enable button
|
||||
this.disabled = false;
|
||||
this.innerHTML = '<i class="fas fa-trash"></i> Yes, Clear All Data';
|
||||
confirmBtn.disabled = false;
|
||||
confirmBtn.innerHTML = '<i class="fas fa-trash"></i> Yes, Clear All Data';
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
alert('Error clearing table: ' + error);
|
||||
alert('Error clearing table: ' + error.message);
|
||||
// Re-enable button
|
||||
this.disabled = false;
|
||||
this.innerHTML = '<i class="fas fa-trash"></i> Yes, Clear All Data';
|
||||
confirmBtn.disabled = false;
|
||||
confirmBtn.innerHTML = '<i class="fas fa-trash"></i> Yes, Clear All Data';
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -739,7 +753,10 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
},
|
||||
body: JSON.stringify({ backup: backup })
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(response => {
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
alert('Database restored successfully!');
|
||||
@@ -758,7 +775,10 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
|
||||
function loadBackupsList() {
|
||||
fetch('{{ url_for("settings.get_backups_list") }}')
|
||||
.then(response => response.json())
|
||||
.then(response => {
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
if (data.backups && data.backups.length > 0) {
|
||||
const tbody = document.getElementById('backups-list');
|
||||
@@ -837,7 +857,10 @@ function toggleDayOfWeek() {
|
||||
|
||||
function loadBackupSchedules() {
|
||||
fetch('{{ url_for("settings.get_backup_schedules") }}')
|
||||
.then(response => response.json())
|
||||
.then(response => {
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
const tbody = document.getElementById('schedules-list');
|
||||
|
||||
@@ -925,7 +948,10 @@ function saveBackupSchedule() {
|
||||
},
|
||||
body: JSON.stringify(payload)
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(response => {
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
alert('Backup schedule created successfully!');
|
||||
@@ -949,7 +975,10 @@ function deleteSchedule(scheduleId) {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(response => {
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
alert('Schedule deleted successfully!');
|
||||
@@ -972,7 +1001,10 @@ function toggleSchedule(scheduleId) {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(response => {
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
loadBackupSchedules();
|
||||
@@ -1010,7 +1042,10 @@ function uploadBackupFile() {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(response => {
|
||||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
statusDiv.innerHTML = '<div class="alert alert-success"><i class="fas fa-check-circle"></i> File uploaded successfully: ' + data.filename + ' (' + data.size + ' bytes)</div>';
|
||||
|
||||
25
init_db.py
25
init_db.py
@@ -408,6 +408,31 @@ def create_tables():
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
|
||||
""", description="Table 'cp_location_history'")
|
||||
|
||||
# Order For Labels table (for printing module)
|
||||
execute_sql(conn, """
|
||||
CREATE TABLE IF NOT EXISTS order_for_labels (
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
comanda_productie VARCHAR(50) NOT NULL,
|
||||
cod_articol VARCHAR(50),
|
||||
descr_com_prod TEXT,
|
||||
cantitate DECIMAL(10, 2),
|
||||
com_achiz_client VARCHAR(50),
|
||||
nr_linie_com_client VARCHAR(50),
|
||||
customer_name VARCHAR(255),
|
||||
customer_article_number VARCHAR(100),
|
||||
open_for_order TINYINT(1) DEFAULT 1,
|
||||
line_number INT,
|
||||
printed_labels TINYINT(1) DEFAULT 0,
|
||||
data_livrare DATE,
|
||||
dimensiune VARCHAR(50),
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
INDEX idx_comanda_productie (comanda_productie),
|
||||
INDEX idx_printed_labels (printed_labels),
|
||||
INDEX idx_created_at (created_at)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
|
||||
""", description="Table 'order_for_labels'")
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
logger.info("✓ All tables created successfully")
|
||||
|
||||
Reference in New Issue
Block a user