From 2fdbbf551953cf4e85cf90937569032178c5d1d0 Mon Sep 17 00:00:00 2001 From: itbencn Date: Fri, 21 Apr 2023 01:28:56 +0800 Subject: [PATCH] fix add_machine bug & foramt --- server.py | 2 +- static/js/custom.js | 528 ++++++++++++++++++++++---------------------- 2 files changed, 267 insertions(+), 263 deletions(-) diff --git a/server.py b/server.py index 5454143..85c0b79 100644 --- a/server.py +++ b/server.py @@ -439,7 +439,7 @@ def register_machine(): url = headscale.get_url() api_key = headscale.get_api_key() - return str(headscale.register_machine(url, api_key, machine_key, user)) + return headscale.register_machine(url, api_key, machine_key, user) ######################################################################################## # User API Endpoints diff --git a/static/js/custom.js b/static/js/custom.js index 5c1ea75..3a34636 100644 --- a/static/js/custom.js +++ b/static/js/custom.js @@ -25,9 +25,9 @@ function hide_search() { function liveSearch() { let cards = document.querySelectorAll('.searchable'); let search_query = document.getElementById("search").value; - + for (var i = 0; i < cards.length; i++) { - if(cards[i].textContent.toLowerCase() .includes(search_query.toLowerCase())) { + if (cards[i].textContent.toLowerCase().includes(search_query.toLowerCase())) { cards[i].classList.remove("hide"); } else { cards[i].classList.add("hide"); @@ -37,7 +37,7 @@ function liveSearch() { //----------------------------------------------------------- // General Helpers //----------------------------------------------------------- -function loading () { +function loading() { return `
@@ -80,9 +80,9 @@ function get_color(id) { // Generic modal used for alerts / problems function load_modal_generic(type, title, message) { console.log("Loading the generic modal") - element = document.getElementById('generic_modal') + element = document.getElementById('generic_modal') content_element = document.getElementById('generic_modal_content') - title_element = document.getElementById('generic_modal_title') + title_element = document.getElementById('generic_modal_title') content_element.innerHTML = loading() title_element.innerHTML = "Loading..." @@ -91,7 +91,7 @@ function load_modal_generic(type, title, message) { switch (type) { case "warning" || "Warning": title_html = "Warning" - content_html =` + content_html = `
  • priority_high @@ -102,7 +102,7 @@ function load_modal_generic(type, title, message) { break; case "success" || "Success": title_html = "Success" - content_html =` + content_html = `
    • check @@ -111,9 +111,9 @@ function load_modal_generic(type, title, message) {
    ` break; - case "error" || "Error": + case "error" || "Error": title_html = "Error" - content_html =` + content_html = `
    • warning @@ -124,7 +124,7 @@ function load_modal_generic(type, title, message) { break; case "information" || "Information": title_html = "Information" - content_html =` + content_html = `
      • help @@ -142,20 +142,20 @@ function load_modal_generic(type, title, message) { } // https://stackoverflow.com/questions/3043775/how-to-escape-html#22706073 -function escapeHTML(str){ +function escapeHTML(str) { var p = document.createElement("p"); p.appendChild(document.createTextNode(str)); return p.innerHTML; } // Enables the Floating Action Button (FAB) for the Machines and Users page -document.addEventListener('DOMContentLoaded', function() { +document.addEventListener('DOMContentLoaded', function () { var elems = document.querySelectorAll('.fixed-action-btn'); - var instances = M.FloatingActionButton.init(elems, {hoverEnabled:false}); + var instances = M.FloatingActionButton.init(elems, { hoverEnabled: false }); }); // Init the date picker when adding PreAuth keys -document.addEventListener('DOMContentLoaded', function() { +document.addEventListener('DOMContentLoaded', function () { var elems = document.querySelectorAll('.datepicker'); var instances = M.Datepicker.init(elems); }); @@ -166,10 +166,10 @@ document.addEventListener('DOMContentLoaded', function() { function test_key() { document.getElementById('test_modal_results').innerHTML = loading() var data = $.ajax({ - type:"GET", + type: "GET", url: "api/test_key", - success: function(response) { - if(response == "Unauthenticated") { + success: function (response) { + if (response == "Unauthenticated") { html = `
        • @@ -234,14 +234,14 @@ function save_key() { document.getElementById('test_modal_results').innerHTML = html return }; - var data = {"api_key": api_key}; + var data = { "api_key": api_key }; $.ajax({ - type:"POST", + type: "POST", url: "api/save_key", data: JSON.stringify(data), contentType: "application/json", - success: function(response) { - M.toast({html: 'Key saved. Testing...'}); + success: function (response) { + M.toast({ html: 'Key saved. Testing...' }); test_key(); } }) @@ -256,12 +256,12 @@ function load_modal_rename_user(user_id, old_name) { document.getElementById('modal_confirm').className = "green btn-flat white-text" document.getElementById('modal_confirm').innerText = "Rename" - modal = document.getElementById('card_modal'); - modal_title = document.getElementById('modal_title'); - modal_body = document.getElementById('modal_content'); + modal = document.getElementById('card_modal'); + modal_title = document.getElementById('modal_title'); + modal_body = document.getElementById('modal_content'); modal_confirm = document.getElementById('modal_confirm'); - modal_title.innerHTML = "Rename user '"+old_name+"'?" + modal_title.innerHTML = "Rename user '" + old_name + "'?" body_html = `
          • @@ -278,9 +278,9 @@ function load_modal_rename_user(user_id, old_name) { ` modal_body.innerHTML = body_html - $(document).ready(function() { $('input#new_user_name_form').characterCounter(); }); + $(document).ready(function () { $('input#new_user_name_form').characterCounter(); }); - modal_confirm.setAttribute('onclick', 'rename_user('+user_id+', "'+old_name+'")') + modal_confirm.setAttribute('onclick', 'rename_user(' + user_id + ', "' + old_name + '")') } function load_modal_delete_user(user_id, user_name) { @@ -289,12 +289,12 @@ function load_modal_delete_user(user_id, user_name) { document.getElementById('modal_confirm').className = "red btn-flat white-text" document.getElementById('modal_confirm').innerText = "Delete" - modal = document.getElementById('card_modal'); - modal_title = document.getElementById('modal_title'); - modal_body = document.getElementById('modal_content'); + modal = document.getElementById('card_modal'); + modal_title = document.getElementById('modal_title'); + modal_body = document.getElementById('modal_content'); modal_confirm = document.getElementById('modal_confirm'); - modal_title.innerHTML = "Delete user '"+user_name+"'?" + modal_title.innerHTML = "Delete user '" + user_name + "'?" body_html = `
            • @@ -305,7 +305,7 @@ function load_modal_delete_user(user_id, user_name) {
            ` modal_body.innerHTML = body_html - modal_confirm.setAttribute('onclick', 'delete_user("'+user_id+'", "'+user_name+'")') + modal_confirm.setAttribute('onclick', 'delete_user("' + user_id + '", "' + user_name + '")') } function load_modal_add_preauth_key(user_name) { @@ -314,12 +314,12 @@ function load_modal_add_preauth_key(user_name) { document.getElementById('modal_confirm').className = "green btn-flat white-text" document.getElementById('modal_confirm').innerText = "Add" - modal = document.getElementById('card_modal'); - modal_title = document.getElementById('modal_title'); - modal_body = document.getElementById('modal_content'); + modal = document.getElementById('card_modal'); + modal_title = document.getElementById('modal_title'); + modal_body = document.getElementById('modal_content'); modal_confirm = document.getElementById('modal_confirm'); - modal_title.innerHTML = "Adding a PreAuth key to '"+user_name+"'" + modal_title.innerHTML = "Adding a PreAuth key to '" + user_name + "'" body_html = `
            • @@ -354,9 +354,9 @@ function load_modal_add_preauth_key(user_name) { modal_body.innerHTML = body_html // Init the date picker - M.Datepicker.init(document.querySelector('.datepicker'), {format:'yyyy-mm-dd'}); + M.Datepicker.init(document.querySelector('.datepicker'), { format: 'yyyy-mm-dd' }); - modal_confirm.setAttribute('onclick', 'add_preauth_key("'+user_name+'")') + modal_confirm.setAttribute('onclick', 'add_preauth_key("' + user_name + '")') } function load_modal_expire_preauth_key(user_name, key) { @@ -365,9 +365,9 @@ function load_modal_expire_preauth_key(user_name, key) { document.getElementById('modal_confirm').className = "red lighten-2 btn-flat white-text" document.getElementById('modal_confirm').innerText = "Expire" - modal = document.getElementById('card_modal'); - modal_title = document.getElementById('modal_title'); - modal_body = document.getElementById('modal_content'); + modal = document.getElementById('card_modal'); + modal_title = document.getElementById('modal_title'); + modal_body = document.getElementById('modal_content'); modal_confirm = document.getElementById('modal_confirm'); modal_title.innerHTML = "Expire PreAuth Key?" @@ -381,7 +381,7 @@ function load_modal_expire_preauth_key(user_name, key) {
            ` modal_body.innerHTML = body_html - modal_confirm.setAttribute('onclick', 'expire_preauth_key("'+user_name+'", "'+key+'")') + modal_confirm.setAttribute('onclick', 'expire_preauth_key("' + user_name + '", "' + key + '")') } function load_modal_move_machine(machine_id) { @@ -390,30 +390,30 @@ function load_modal_move_machine(machine_id) { document.getElementById('modal_confirm').className = "green btn-flat white-text" document.getElementById('modal_confirm').innerText = "Move" - var data = {"id": machine_id} + var data = { "id": machine_id } $.ajax({ - type: "POST", + type: "POST", url: "api/machine_information", data: JSON.stringify(data), contentType: "application/json", - success: function(headscale) { + success: function (headscale) { $.ajax({ - type: "POST", + type: "POST", url: "api/get_users", - success: function(response) { - modal = document.getElementById('card_modal'); - modal_title = document.getElementById('modal_title'); - modal_body = document.getElementById('modal_content'); + success: function (response) { + modal = document.getElementById('card_modal'); + modal_title = document.getElementById('modal_title'); + modal_body = document.getElementById('modal_content'); modal_confirm = document.getElementById('modal_confirm'); - - modal_title.innerHTML = "Move machine '"+headscale.machine.givenName+"'?" + + modal_title.innerHTML = "Move machine '" + headscale.machine.givenName + "'?" select_html = `
            Select a User
            ` + select_html = select_html + `` body_html = `
              @@ -423,8 +423,8 @@ function load_modal_move_machine(machine_id) {

              You are about to move ${headscale.machine.givenName} to a new user.

            ` - body_html = body_html+select_html - body_html = body_html+`
            Machine Information
            + body_html = body_html + select_html + body_html = body_html + `
            Machine Information
            @@ -447,7 +447,7 @@ function load_modal_move_machine(machine_id) { M.FormSelect.init(document.querySelectorAll('select')) } }) - modal_confirm.setAttribute('onclick', 'move_machine('+machine_id+')') + modal_confirm.setAttribute('onclick', 'move_machine(' + machine_id + ')') } }) } @@ -458,19 +458,19 @@ function load_modal_delete_machine(machine_id) { document.getElementById('modal_confirm').className = "red btn-flat white-text" document.getElementById('modal_confirm').innerText = "Delete" - var data = {"id": machine_id} + var data = { "id": machine_id } $.ajax({ - type: "POST", + type: "POST", url: "api/machine_information", data: JSON.stringify(data), contentType: "application/json", - success: function(response) { - modal = document.getElementById('card_modal'); - modal_title = document.getElementById('modal_title'); - modal_body = document.getElementById('modal_content'); + success: function (response) { + modal = document.getElementById('card_modal'); + modal_title = document.getElementById('modal_title'); + modal_body = document.getElementById('modal_content'); modal_confirm = document.getElementById('modal_confirm'); - modal_title.innerHTML = "Delete machine '"+response.machine.givenName+"'?" + modal_title.innerHTML = "Delete machine '" + response.machine.givenName + "'?" body_html = `
            • @@ -498,7 +498,7 @@ function load_modal_delete_machine(machine_id) {
            ` modal_body.innerHTML = body_html - modal_confirm.setAttribute('onclick', 'delete_machine('+machine_id+')') + modal_confirm.setAttribute('onclick', 'delete_machine(' + machine_id + ')') } }) } @@ -508,19 +508,19 @@ function load_modal_rename_machine(machine_id) { document.getElementById('modal_title').innerHTML = "Loading..." document.getElementById('modal_confirm').className = "green btn-flat white-text" document.getElementById('modal_confirm').innerText = "Rename" - var data = {"id": machine_id} + var data = { "id": machine_id } $.ajax({ - type: "POST", + type: "POST", url: "api/machine_information", data: JSON.stringify(data), contentType: "application/json", - success: function(response) { - modal = document.getElementById('card_modal'); - modal_title = document.getElementById('modal_title'); - modal_body = document.getElementById('modal_content'); + success: function (response) { + modal = document.getElementById('card_modal'); + modal_title = document.getElementById('modal_title'); + modal_body = document.getElementById('modal_content'); modal_confirm = document.getElementById('modal_confirm'); - modal_title.innerHTML = "Rename machine '"+response.machine.givenName+"'?" + modal_title.innerHTML = "Rename machine '" + response.machine.givenName + "'?" body_html = `
            • @@ -553,16 +553,16 @@ function load_modal_rename_machine(machine_id) { ` modal_body.innerHTML = body_html - modal_confirm.setAttribute('onclick', 'rename_machine('+machine_id+')') + modal_confirm.setAttribute('onclick', 'rename_machine(' + machine_id + ')') } }) } function load_modal_add_machine() { $.ajax({ - type: "POST", + type: "POST", url: "api/get_users", - success: function(response) { + success: function (response) { modal_body = document.getElementById('default_add_new_machine_modal'); modal_confirm = document.getElementById('new_machine_modal_confirm'); @@ -572,9 +572,9 @@ function load_modal_add_machine() { language