settings page: add custom device

This commit is contained in:
Maxi Quoß
2021-10-05 22:10:55 +02:00
parent abf05a2b21
commit ffc17824ec
6 changed files with 54 additions and 11 deletions

View File

@@ -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")

View File

@@ -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);
}
}

View File

@@ -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);

View File

@@ -119,7 +119,27 @@
<div class="columns is-multiline is-mobile">
<div class="column is-full-mobile is-full-tablet is-full-desktop is-full-widescreen is-half-fullhd">
<div class="box no-flex">
<h2 class="title is-size-3 mb-4">Add devices</h2>
<h2 class="title is-size-3 mb-4">Add custom device</h2>
<form action="/settings/custom_add/" method="post">
{% csrf_token %}
<table class="table is-fullwidth">
<thead>
<th>Name</th>
<th>IP</th>
<th>MAC</th>
<th>Action</th>
</thead>
<tbody>
<td><input type="text" class="input" name="custom_add_name"></td>
<td><input type="text" class="input" name="custom_add_ip"></td>
<td><input type="text" class="input" name="custom_add_mac"></td>
<td class="is-vcentered">
<button id="btn-custom-add" class="button is-primary is-small" type="submit">Add</button>
</td>
</tbody>
</table>
</form>
<h2 class="title is-size-3 mb-4">Scan for devices</h2>
{% if settings.scan_address %}
<button id="scan-button" class="button is-primary">Scan network</button>
{% else %}
@@ -141,7 +161,7 @@
<tbody></tbody>
</table>
</div>
</div>
</div>
</div>
<div class="column is-full-mobile is-full-tablet is-full-desktop is-full-widescreen is-half-fullhd">
<div class="box no-flex">
@@ -166,7 +186,7 @@
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</section>

View File

@@ -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")
]

View File

@@ -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 = {}