diff --git a/router_manager/views.py b/router_manager/views.py index 07b2c14..65f2ac3 100644 --- a/router_manager/views.py +++ b/router_manager/views.py @@ -13,7 +13,8 @@ from user_manager.models import UserAcl from .forms import RouterForm, RouterGroupForm, SSHKeyForm from .models import Router, RouterGroup, RouterInformation, RouterStatus, SSHKey, BackupSchedule from django.conf import settings - +import json +from urllib.parse import unquote @login_required def view_router_list(request): @@ -41,12 +42,25 @@ def view_router_list(request): if not filter_group and request.GET.get('filter_group') != 'all': filter_group = RouterGroup.objects.filter(default_group=True).first() + # Parse the router_visible_columns cookie + visible_columns = [] + if 'router_visible_columns' in request.COOKIES: + try: + visible_columns = json.loads(unquote(request.COOKIES['router_visible_columns'])) + except json.JSONDecodeError: + # If the cookie is invalid, use default columns + visible_columns = ["name", "type", "status", "backup", "groups"] + else: + # Default columns if cookie doesn't exist + visible_columns = ["name", "type", "status", "backup", "groups"] + context = { 'router_list': router_list, 'page_title': 'Router List', 'filter_group_list': RouterGroup.objects.all().order_by('name'), 'filter_group': filter_group, 'last_status_change_timestamp': last_status_change_timestamp, + 'visible_columns': visible_columns, } return render(request, 'router_manager/router_list.html', context=context) diff --git a/templates/router_manager/router_list.html b/templates/router_manager/router_list.html index 45f9160..011f45f 100644 --- a/templates/router_manager/router_list.html +++ b/templates/router_manager/router_list.html @@ -12,30 +12,90 @@ {% endif %}
{% include 'router_manager/router_nav_tabs.html' %} + + +
- - - - - - - - - - - - - - - - - - + {% if "name" in visible_columns %} + + {% endif %} + {% if "type" in visible_columns %} + + {% endif %} + {% if "address" in visible_columns %} + + {% endif %} + {% if "status" in visible_columns %} + + {% endif %} + {% if "backup" in visible_columns %} + + {% endif %} + {% if "last_backup" in visible_columns %} + + {% endif %} + {% if "next_daily_backup" in visible_columns %} + + {% endif %} + {% if "next_weekly_backup" in visible_columns %} + + {% endif %} + {% if "next_monthly_backup" in visible_columns %} + + {% endif %} + {% if "groups" in visible_columns %} + + {% endif %} + {% if "auth" in visible_columns %} + + {% endif %} + {% if "os_version" in visible_columns %} + + {% endif %} + {% if "model_name" in visible_columns %} + + {% endif %} + {% if "model_version" in visible_columns %} + + {% endif %} + {% if "serial_number" in visible_columns %} + + {% endif %} + {% if "firmware_version" in visible_columns %} + + {% endif %} + {% if "architecture" in visible_columns %} + + {% endif %} + {% if "cpu" in visible_columns %} + + {% endif %} @@ -43,11 +103,17 @@ {% for router in router_list %} - - - - - + {% endif %} + {% if "type" in visible_columns %} + + {% endif %} + {% if "address" in visible_columns %} + + {% endif %} + {% if "status" in visible_columns %} + - - - - - - - - - - - - - - - + {% endif %} @@ -182,6 +276,28 @@ var status_online_text = 'online'; var status_offline_text = 'offline'; + // Column definitions for the router table + var columnDefinitions = [ + { name: "name", label: "Name", default: true }, + { name: "type", label: "Type", default: true }, + { name: "address", label: "Address", default: false }, + { name: "status", label: "Status", default: true }, + { name: "backup", label: "Backup", default: true }, + { name: "last_backup", label: "Last Backup", default: false }, + { name: "next_daily_backup", label: "Next Daily Backup", default: false }, + { name: "next_weekly_backup", label: "Next Weekly Backup", default: false }, + { name: "next_monthly_backup", label: "Next Monthly Backup", default: false }, + { name: "groups", label: "Groups", default: true }, + { name: "auth", label: "Auth", default: false }, + { name: "os_version", label: "OS Version", default: false }, + { name: "model_name", label: "Model Name", default: false }, + { name: "model_version", label: "Model Version", default: false }, + { name: "serial_number", label: "Serial Number", default: false }, + { name: "firmware_version", label: "Firmware Version", default: false }, + { name: "architecture", label: "Architecture", default: false }, + { name: "cpu", label: "CPU", default: false } + ]; + function checkStatusChange() { $.ajax({ url: "/monitoring/last_status_change/", @@ -233,6 +349,8 @@ }); } } + + // No need to reapply column visibility as it's now handled by Django template tags } setInterval(checkStatusChange, 30000); @@ -245,6 +363,105 @@ $(document).ready(function() { var table = dataTable; + // Column visibility functions + function getCookie(name) { + var cookieValue = null; + if (document.cookie && document.cookie !== '') { + var cookies = document.cookie.split(';'); + for (var i = 0; i < cookies.length; i++) { + var cookie = cookies[i].trim(); + if (cookie.substring(0, name.length + 1) === (name + '=')) { + cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); + break; + } + } + } + return cookieValue; + } + + function setCookie(name, value, days) { + var expires = ""; + if (days) { + var date = new Date(); + // Calculate expiration date: current date + days (in milliseconds) + date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); + expires = "; expires=" + date.toUTCString(); + } + // Set the cookie with the calculated expiration date + document.cookie = name + "=" + encodeURIComponent(value) + expires + "; path=/; max-age=" + (days * 24 * 60 * 60); + } + + function getVisibleColumns() { + var cookieValue = getCookie('router_visible_columns'); + if (cookieValue) { + return JSON.parse(cookieValue); + } + // Default columns if cookie doesn't exist + return columnDefinitions.filter(function(col) { + return col.default; + }).map(function(col) { + return col.name; + }); + } + + // No need for applyColumnVisibility function as column visibility is now handled by Django template tags + + function populateColumnModal() { + var visibleColumns = getVisibleColumns(); + var $container = $('#columnCheckboxes'); + $container.empty(); + + columnDefinitions.forEach(function(col) { + var isChecked = visibleColumns.includes(col.name); + var $div = $('
'); + var $input = $(''); + var $label = $(''); + + $div.append($input).append($label); + $container.append($div); + }); + } + + // No need to initialize column visibility as it's now handled by Django template tags + + // Show/Hide Columns button click handler + $('#showHideColumnsBtn').on('click', function(e) { + e.preventDefault(); + populateColumnModal(); + $('#columnVisibilityModal').modal('show'); + }); + + // Apply button click handler + $('#applyColumnVisibility').on('click', function() { + var selectedColumns = []; + $('#columnCheckboxes input:checked').each(function() { + selectedColumns.push($(this).val()); + }); + + // Save to cookie + setCookie('router_visible_columns', JSON.stringify(selectedColumns), 90); + + // Reload the page with the same GET parameters + var currentUrl = window.location.href; + window.location.href = currentUrl; + }); + + // Function to delete a cookie + function deleteCookie(name) { + document.cookie = name + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;'; + } + + // Reset button click handler + $('#resetColumnVisibility').on('click', function() { + // Delete the router_visible_columns cookie + deleteCookie('router_visible_columns'); + + // Reload the page with the same GET parameters + var currentUrl = window.location.href; + window.location.href = currentUrl; + }); + + // Router selection functions function updateCheckboxes(checked) { table.$('.router-checkbox').prop('checked', checked).trigger('change'); } diff --git a/templates/router_manager/router_nav_tabs.html b/templates/router_manager/router_nav_tabs.html index a3fd3b3..e411cc2 100644 --- a/templates/router_manager/router_nav_tabs.html +++ b/templates/router_manager/router_nav_tabs.html @@ -31,4 +31,10 @@
{% endif %} - \ No newline at end of file + + +
NameTypeAddressStatusBackupLast BackupNext Daily BackupNext Weekly BackupNext Monthly BackupGroupsAuthOS VersionModel NameModel VersionSerial NumberFirmware VersionArchitectureCPUNameTypeAddressStatusBackupLast BackupNext Daily BackupNext Weekly BackupNext Monthly BackupGroupsAuthOS VersionModel NameModel VersionSerial NumberFirmware VersionArchitectureCPU
{{ router.name }}{{ router.get_router_type_display }}{{ router.address }} + {% if "name" in visible_columns %} + {{ router.name }}{{ router.get_router_type_display }}{{ router.address }} {% if router.monitoring %} {% if router.routerstatus.status_online %} online @@ -58,7 +124,9 @@ --- {% endif %} + {% endif %} + {% if "backup" in visible_columns %} + {% if router.router_type != 'monitoring' %} {% if router.backup_profile %} {{ router.backup_profile }} {% if router.routerstatus.last_backup_failed %}{% endif %} @@ -67,35 +135,45 @@ {% endif %} {% endif %} + {% endif %} + {% if "last_backup" in visible_columns %} + {% if router.router_type != 'monitoring' and router.routerstatus.last_backup %} {{ router.routerstatus.last_backup|date:"Y-m-d H:i:s" }} {% else %} --- {% endif %} + {% endif %} + {% if "next_daily_backup" in visible_columns %} + {% if router.router_type != 'monitoring' and router.backupschedule.next_daily_backup %} {{ router.backupschedule.next_daily_backup|date:"Y-m-d H:i:s" }} {% else %} --- {% endif %} + {% endif %} + {% if "next_weekly_backup" in visible_columns %} + {% if router.router_type != 'monitoring' and router.backupschedule.next_weekly_backup %} {{ router.backupschedule.next_weekly_backup|date:"Y-m-d H:i:s" }} {% else %} --- {% endif %} + {% endif %} + {% if "next_monthly_backup" in visible_columns %} + {% if router.router_type != 'monitoring' and router.backupschedule.next_monthly_backup %} {{ router.backupschedule.next_monthly_backup|date:"Y-m-d H:i:s" }} {% else %} --- {% endif %} + {% endif %} + {% if "groups" in visible_columns %} + {% if router.routergroup_set.exists %} {% for group in router.routergroup_set.all %} {{ group.name }}{% if not forloop.last %}, {% endif %} @@ -104,7 +182,9 @@ --- {% endif %} + {% endif %} + {% if "auth" in visible_columns %} + {% if router.router_type != 'monitoring' %} {% if router.ssh_key %} sshkey @@ -115,42 +195,56 @@ {% endif %} {% endif %} + {% endif %} + {% if "os_version" in visible_columns %} + {% if router.router_type != 'monitoring' %} {{ router.routerinformation.os_version|default_if_none:'' }} {% endif %} + {% endif %} + {% if "model_name" in visible_columns %} + {% if router.router_type != 'monitoring' %} {{ router.routerinformation.model_name|default_if_none:'' }} {% endif %} + {% endif %} + {% if "model_version" in visible_columns %} + {% if router.router_type != 'monitoring' %} {{ router.routerinformation.model_version|default_if_none:'' }} {% endif %} + {% endif %} + {% if "serial_number" in visible_columns %} + {% if router.router_type != 'monitoring' %} {{ router.routerinformation.serial_number|default_if_none:'' }} {% endif %} + {% endif %} + {% if "firmware_version" in visible_columns %} + {% if router.router_type != 'monitoring' %} {{ router.routerinformation.firmware_version|default_if_none:'' }} {% endif %} + {% endif %} + {% if "architecture" in visible_columns %} + {% if router.router_type != 'monitoring' %} {{ router.routerinformation.architecture|default_if_none:'' }} {% endif %} + {% endif %} + {% if "cpu" in visible_columns %} + {% if router.router_type != 'monitoring' %} {{ router.routerinformation.cpu|default_if_none:'' }} {% endif %}