thadillo commited on
Commit
2fe24d3
·
1 Parent(s): d6a961e

Add Clear All Data feature to admin panel

Browse files

- New 'Clear All Data' button on Overview page
- API endpoint to delete all submissions and tokens (keeps admin)
- Double confirmation to prevent accidents
- Resets settings to defaults

Useful for starting fresh sessions or testing

app/routes/admin.py CHANGED
@@ -396,3 +396,26 @@ def import_data():
396
  except Exception as e:
397
  db.session.rollback()
398
  return jsonify({'success': False, 'error': str(e)}), 500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
396
  except Exception as e:
397
  db.session.rollback()
398
  return jsonify({'success': False, 'error': str(e)}), 500
399
+
400
+ @bp.route('/api/clear-all-data', methods=['POST'])
401
+ @admin_required
402
+ def clear_all_data():
403
+ """Clear all submissions and tokens (except admin)"""
404
+ try:
405
+ # Delete all submissions
406
+ Submission.query.delete()
407
+
408
+ # Delete all tokens except admin
409
+ Token.query.filter(Token.token != 'ADMIN123').delete()
410
+
411
+ # Optionally reset settings to defaults
412
+ Settings.set_setting('submission_open', 'true')
413
+ Settings.set_setting('token_generation_enabled', 'true')
414
+
415
+ db.session.commit()
416
+
417
+ return jsonify({'success': True, 'message': 'All data cleared successfully'})
418
+
419
+ except Exception as e:
420
+ db.session.rollback()
421
+ return jsonify({'success': False, 'error': str(e)}), 500
app/templates/admin/overview.html CHANGED
@@ -92,6 +92,12 @@
92
  <i class="bi bi-upload"></i> Import Session
93
  </button>
94
  <input type="file" id="importFile" accept=".json" style="display: none;" onchange="importData(this)">
 
 
 
 
 
 
95
  </div>
96
 
97
  {% if total_submissions > 0 and unanalyzed_count > 0 %}
@@ -193,5 +199,40 @@ function importData(input) {
193
  input.value = '';
194
  });
195
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
  </script>
197
  {% endblock %}
 
92
  <i class="bi bi-upload"></i> Import Session
93
  </button>
94
  <input type="file" id="importFile" accept=".json" style="display: none;" onchange="importData(this)">
95
+
96
+ {% if total_submissions > 0 or total_tokens > 0 %}
97
+ <button class="btn btn-danger" onclick="clearAllData()">
98
+ <i class="bi bi-trash-fill"></i> Clear All Data
99
+ </button>
100
+ {% endif %}
101
  </div>
102
 
103
  {% if total_submissions > 0 and unanalyzed_count > 0 %}
 
199
  input.value = '';
200
  });
201
  }
202
+
203
+ function clearAllData() {
204
+ if (!confirm('⚠️ WARNING: This will permanently DELETE all submissions and tokens (except admin)!\n\nThis action CANNOT be undone.\n\nAre you absolutely sure?')) {
205
+ return;
206
+ }
207
+
208
+ if (!confirm('Last chance! Click OK to permanently delete all data.')) {
209
+ return;
210
+ }
211
+
212
+ const btn = event.target;
213
+ btn.disabled = true;
214
+ btn.innerHTML = '<i class="bi bi-hourglass-split"></i> Clearing...';
215
+
216
+ fetch('{{ url_for("admin.clear_all_data") }}', {
217
+ method: 'POST',
218
+ headers: {'Content-Type': 'application/json'}
219
+ })
220
+ .then(response => response.json())
221
+ .then(data => {
222
+ if (data.success) {
223
+ alert('✅ All data cleared successfully!');
224
+ location.reload();
225
+ } else {
226
+ alert('Error: ' + (data.error || 'Failed to clear data'));
227
+ btn.disabled = false;
228
+ btn.innerHTML = '<i class="bi bi-trash-fill"></i> Clear All Data';
229
+ }
230
+ })
231
+ .catch(error => {
232
+ alert('Error clearing data');
233
+ btn.disabled = false;
234
+ btn.innerHTML = '<i class="bi bi-trash-fill"></i> Clear All Data';
235
+ });
236
+ }
237
  </script>
238
  {% endblock %}