Update bug When I click on "Create backup task," it doesn’t seem to work. The console shows this error: "Uncaught ReferenceError: backupType is not defined."

This commit is contained in:
petrunetworking 2025-02-03 22:32:36 +02:00
parent a730057a6b
commit 9f26425a4f
2 changed files with 99 additions and 100 deletions

View file

@ -27,18 +27,14 @@
<th>Auth</th>
<th></th>
</tr>
</thead>
<tbody>
{% for router in router_list %}
<tr {% if not router.enabled %}style="text-decoration: line-through;"{% endif %}>
<td><input type="checkbox" class="router-checkbox" data-uuid="{{ router.uuid }}"></td>
<td>
<a href="/router/details/?uuid={{ router.uuid }}">{{ router.name }}</a>
</td>
<td><a href="/router/details/?uuid={{ router.uuid }}">{{ router.name }}</a></td>
<td>{{ router.get_router_type_display }}</td>
<td>{{ router.address }}</td>
<td id="status-{{ router.uuid }}">
{% if router.monitoring %}
{% if router.routerstatus.status_online %}
@ -52,16 +48,14 @@
</td>
<td>
{% if router.router_type != 'monitoring' %}
{% if router.backup_profile %}
{{ router.backup_profile }} {% if router.routerstatus.last_backup_failed %}<i class="fas fa-exclamation-triangle text-danger" title="Last backup failed to complete"></i>{% endif %}
{% else %}
<i class="fas fa-exclamation-triangle text-warning" title="No backup profile selected"></i>
{% endif %}
{% if router.backup_profile %}
{{ router.backup_profile }} {% if router.routerstatus.last_backup_failed %}<i class="fas fa-exclamation-triangle text-danger" title="Last backup failed to complete"></i>{% endif %}
{% else %}
<i class="fas fa-exclamation-triangle text-warning" title="No backup profile selected"></i>
{% endif %}
{% endif %}
</td>
<td>
{{ router.routergroup_set.count }}
</td>
<td>{{ router.routergroup_set.count }}</td>
<td class="min-width">
{% if router.router_type != 'monitoring' %}
{% if router.ssh_key %}
@ -80,20 +74,20 @@
{% endfor %}
</tbody>
</table>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<a href="/router/manage/" class="btn btn-primary">Add Router</a>
<a href="/router/import_tool/" class="btn btn-outline-primary">Import tool</a>
</div>
<div class="row mb-3">
<div class="col-lg-12 text-right">
<div class="btn-group" role="group">
<a href="/router/manage/" class="btn btn-primary">Add Router</a>
<a href="/router/import_tool/" class="btn btn-warning">Import Tool</a>
<button id="select-all" class="btn btn-outline-success"><i class="fas fa-check-square"></i> Select All</button>
<button id="select-none" class="btn btn-outline-danger"><i class="fas fa-square"></i> Select None</button>
<button id="create-backup" class="btn btn-warning" style="display: none;">Create Backup Task</button>
</div>
</div>
</div>
<div class="form-group mb-0" id="backup-type-container" style="display: none;">
</div>
<button id="create-backup" class="btn btn-warning mr-2" style="display: none;">Create Backup Task</button>
</div>
</div>
</div>
</div>
@ -166,49 +160,57 @@
});
</script>
<script>
$(document).ready(function() {
$('.router-checkbox').prop('checked', false);
// Check checkbox change event
$('.router-checkbox').change(function() {
var checkedCount = $('.router-checkbox:checked').length;
// Show the backup type selector and the backup button only if 2 or more routers are selected
if (checkedCount >= 2) {
$('#backup-type-container').show(); // Show the backup type dropdown
$('#create-backup').show(); // Show the backup button
} else {
$('#backup-type-container').hide(); // Hide the backup type dropdown
$('#create-backup').hide(); // Hide the backup button
$(document).ready(function() {
$('.router-checkbox').prop('checked', false);
$('#select-all').click(function() {
$('.router-checkbox').prop('checked', true).trigger('change');
});
$('#select-none').click(function() {
$('.router-checkbox').prop('checked', false).trigger('change');
});
$('.router-checkbox').change(function() {
var checkedCount = $('.router-checkbox:checked').length;
$('#create-backup').toggle(checkedCount >= 1);
});
$('#create-backup').click(function() {
var selectedRouters = $('.router-checkbox:checked').map(function() {
return $(this).data('uuid');
}).get();
$.ajax({
url: '/router/create_instant_backup/multiple/',
method: 'POST',
data: {
routers: selectedRouters,
csrfmiddlewaretoken: '{{ csrf_token }}'
},
success: function(response) {
let messages = response.results.map(item => `${item.router}: ${item.status}`).join('\n');
// Create success toast
$(document).Toasts('create', {
class: 'bg-success',
title: 'Backup Status',
body: 'The following routers were backed up: ' + messages,
delay: 10000,
autohide: true
});
},
error: function(xhr) {
// Create error toast
$(document).Toasts('create', {
class: 'bg-danger',
title: 'Error',
body: 'Error: ' + xhr.responseJSON.error,
delay: 10000,
autohide: true
});
}
});
// Backup button click event
$('#create-backup').click(function() {
var selectedRouters = [];
$('.router-checkbox:checked').each(function() {
selectedRouters.push($(this).data('uuid')); // Collect UUIDs of selected routers
});
// Perform the backup operation (AJAX or redirect)
alert('Creating ' + backupType + ' backup for routers: ' + selectedRouters.join(', '));
// Example AJAX request
$.ajax({
url: '/router/create_instant_backup/multiple/',
method: 'POST',
data: {
routers: selectedRouters,
csrfmiddlewaretoken: '{{ csrf_token }}' // Ensure CSRF token is included for Django
},
success: function(response) {
alert('Backup started for selected routers');
},
error: function(xhr, status, error) {
alert('Error starting backup: ' + error);
}
});
});
});
});
</script>
{% endblock %}