From ffc17824ec4027e90dc7af14dbd3add405c3988e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxi=20Quo=C3=9F?= Date: Tue, 5 Oct 2021 22:10:55 +0200 Subject: [PATCH] settings page: add custom device --- app/wol/forms.py | 6 ++++++ app/wol/static/js/main.js | 2 +- app/wol/static/js/settings.js | 2 +- app/wol/templates/wol/settings.html | 26 +++++++++++++++++++++++--- app/wol/urls.py | 3 ++- app/wol/views.py | 26 +++++++++++++++++++++----- 6 files changed, 54 insertions(+), 11 deletions(-) diff --git a/app/wol/forms.py b/app/wol/forms.py index 92393db3..115653d4 100644 --- a/app/wol/forms.py +++ b/app/wol/forms.py @@ -11,3 +11,9 @@ class SettingsForm(forms.Form): console = forms.CharField(widget=forms.RadioSelect, label="console") sort = forms.ChoiceField(choices=[("name", "name"), ("ip", "ip")], widget=forms.RadioSelect, label="sort") ip_range = forms.CharField(label="ip_range") + + +class AddCustomDevice(forms.Form): + custom_add_name = forms.CharField(label="custom_add_name") + custom_add_ip = forms.CharField(label="custom_add_ip") + custom_add_mac = forms.CharField(label="custom_add_mac") diff --git a/app/wol/static/js/main.js b/app/wol/static/js/main.js index 7eebe0c7..938ef607 100644 --- a/app/wol/static/js/main.js +++ b/app/wol/static/js/main.js @@ -297,7 +297,7 @@ socket.onmessage = function (event) { if ("wake" in message) { document.querySelector(`[id="btn-wake"][data-id="${message.wake.id}"]`).classList.add("is-loading"); if (enableNotifications) { - notif.show("Wake started", "Wake command for" message.wake.name + "has been sent.", "is-info", 5000); + notif.show("Wake started", "Wake command for" + message.wake.name + "has been sent.", "is-info", 5000); } } diff --git a/app/wol/static/js/settings.js b/app/wol/static/js/settings.js index 6f899e1a..0c7daa12 100644 --- a/app/wol/static/js/settings.js +++ b/app/wol/static/js/settings.js @@ -66,7 +66,7 @@ async function scan() { function add_device(data) { var xhr = new XMLHttpRequest(); - var url = "/settings/add/"; + var url = "/settings/scan_add/"; xhr.open("POST", url, true); xhr.setRequestHeader("Content-Type", "application/json"); xhr.send(data); diff --git a/app/wol/templates/wol/settings.html b/app/wol/templates/wol/settings.html index ca2c34df..e221235d 100644 --- a/app/wol/templates/wol/settings.html +++ b/app/wol/templates/wol/settings.html @@ -119,7 +119,27 @@
-

Add devices

+

Add custom device

+
+ {% csrf_token %} + + + + + + + + + + + + + +
NameIPMACAction
+ +
+
+

Scan for devices

{% if settings.scan_address %} {% else %} @@ -141,7 +161,7 @@
-
+
@@ -166,7 +186,7 @@
-
+ diff --git a/app/wol/urls.py b/app/wol/urls.py index 88cd378d..3413ddcb 100644 --- a/app/wol/urls.py +++ b/app/wol/urls.py @@ -7,6 +7,7 @@ urlpatterns = [ path("settings/", views.settings, name="settings"), path("settings/save/", views.settings_save, name="settings_save"), path("settings/scan/", views.settings_scan, name="settings_scan"), - path("settings/add/", views.settings_add, name="settings_add"), + path("settings/scan_add/", views.settings_scan_add, name="settings_scan_add"), + path("settings/custom_add/", views.settings_custom_add, name="settings_custom_add"), path("settings/del/", views.settings_del, name="settings_del") ] diff --git a/app/wol/views.py b/app/wol/views.py index 1b223bd5..4df19eaf 100644 --- a/app/wol/views.py +++ b/app/wol/views.py @@ -12,7 +12,7 @@ from django.utils.decorators import method_decorator from django.views.decorators.csrf import csrf_exempt from django_wol.settings import VERSION as app_version -from .forms import SettingsForm +from .forms import AddCustomDevice, SettingsForm from .models import Device, Settings, Websocket @@ -74,7 +74,8 @@ def settings_scan(request): if not conf.scan_address: return JsonResponse(data=data) - p = subprocess.Popen(["nmap", "-sP", conf.scan_address], stdout=subprocess.PIPE) + p = subprocess.Popen( + ["nmap", "-sP", conf.scan_address], stdout=subprocess.PIPE) out = p.communicate()[0].decode("utf-8") ip_line = "Nmap scan report for" mac_line = "MAC Address:" @@ -89,11 +90,11 @@ def settings_scan(request): ip = ip.replace(")", "") else: name = "Unknown" - ip = line_splitted[4] + ip = line_splitted[4] elif line.startswith(mac_line): line_splitted = line.split() mac = line_splitted[2] - + data["devices"].append({ "name": name, "ip": ip, @@ -103,8 +104,22 @@ def settings_scan(request): return JsonResponse(data=data) +def settings_custom_add(request): + if request.method == "POST": + form = AddCustomDevice(request.POST) + if form.is_valid(): + Device.objects.update_or_create( + mac=form.cleaned_data["custom_add_mac"], + defaults={ + "name": form.cleaned_data["custom_add_name"], + "ip": form.cleaned_data["custom_add_ip"] + } + ) + return HttpResponseRedirect('/settings/') + + @method_decorator(csrf_exempt, name="dispatch") -def settings_add(request): +def settings_scan_add(request): data = {} if request.method == "POST": post_body = json.loads(request.body.decode('utf-8')) @@ -131,6 +146,7 @@ def settings_add(request): return JsonResponse(data=data) + @method_decorator(csrf_exempt, name="dispatch") def settings_del(request): data = {}