Replace some custom forms by Django forms
This commit is contained in:
parent
35b43df7b4
commit
bff0c04c33
2 changed files with 33 additions and 93 deletions
|
@ -1,6 +1,6 @@
|
|||
from django import forms
|
||||
|
||||
from .models import Organization, Network, Device
|
||||
from .models import *
|
||||
|
||||
class NetworkForm(forms.ModelForm):
|
||||
class Meta:
|
||||
|
@ -12,3 +12,12 @@ class OrgaForm(forms.ModelForm):
|
|||
model = Organization
|
||||
fields = ["name", "userlimit"]
|
||||
|
||||
class DeviceForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Device
|
||||
fields = ["serial", "secret", "password", "mac", "name", "model", "organization", "network", "ssid", "key", "wifi", "update", "reboot"]
|
||||
|
||||
class WifiForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Wifi
|
||||
fields = ["serial", "ssid", "key", "organization"]
|
||||
|
|
111
manager/views.py
111
manager/views.py
|
@ -145,49 +145,19 @@ def editdevice(request, device_id):
|
|||
subnets = Network.objects.filter(organization=device.organization)
|
||||
wifis = Wifi.objects.filter(organization=device.organization)
|
||||
|
||||
if request.POST.get("subnet", ""):
|
||||
subnet = Network.objects.get(intip=request.POST.get("subnet", device.network.intip if device.network else "No VPN"))
|
||||
form = DeviceForm(request.POST if request.method == "POST" else None, instance=device)
|
||||
|
||||
if subnet in subnets:
|
||||
newname = request.POST.get("name", "")
|
||||
if newname != device.name:
|
||||
sigRenameDevice(device.serial, request.user.username, device.name, newname)
|
||||
device.name = newname
|
||||
|
||||
if subnet != device.network:
|
||||
sigNetDevice(device.serial, request.user.username, str(device.network), str(subnet))
|
||||
device.network = subnet
|
||||
|
||||
newreboot = True if request.POST.get("reboot", "0") == "True" else False
|
||||
if newreboot != device.reboot:
|
||||
sigRebootDevice(device.serial, request.user.username, newreboot)
|
||||
device.reboot = newreboot
|
||||
|
||||
newupdate = True if request.POST.get("update", "0") == "True" else False
|
||||
if newupdate != device.update:
|
||||
sigUpdateDevice(device.serial, request.user.username, newupdate)
|
||||
device.update = newupdate
|
||||
|
||||
newwifis = set(request.POST.getlist("wifi", []))
|
||||
oldwifis = set(device.wifi.all())
|
||||
if newwifis != oldwifis:
|
||||
sigWifiDevice(device.serial, request.user.username, oldwifis, newwifis)
|
||||
device.wifi.set(newwifis)
|
||||
|
||||
device.changed = timezone.now()
|
||||
device.save()
|
||||
form.fields["mac"].disabled = True
|
||||
form.fields["serial"].disabled = True
|
||||
form.fields["secret"].disabled = True
|
||||
form.fields["model"].disabled = True
|
||||
form.fields["password"].disabled = True
|
||||
|
||||
if request.method == "POST" and form.is_valid():
|
||||
form.save()
|
||||
return redirect(reverse("devices"))
|
||||
|
||||
return render(request, "manager/edit.html",
|
||||
{
|
||||
"title": "Edit Device",
|
||||
"device": device,
|
||||
"subnets": subnets,
|
||||
"wifis": wifis,
|
||||
"curfis": Wifi.objects.filter(device=device)
|
||||
}
|
||||
)
|
||||
return render(request, "manager/form.html", { "title": "Edit Device", "form": form })
|
||||
|
||||
@user_passes_test(is_superuser)
|
||||
def makeuser(request):
|
||||
|
@ -273,40 +243,14 @@ def edituser(request, user_id):
|
|||
|
||||
@login_required
|
||||
def editwifi(request, wifi_id):
|
||||
wifi = None
|
||||
|
||||
for organization in Organization.objects.filter(users=request.user):
|
||||
wifi = wifi or Wifi.objects.filter(id=wifi_id, organization=organization)
|
||||
|
||||
if not wifi:
|
||||
wifi = get_object_or_404(Wifi, id=wifi_id, organization__in=request.user.organization_set.all())
|
||||
form = WifiForm(request.POST if request.method == "POST" else None, instance=wifi)
|
||||
form.fields["organization"].queryset = request.user.organization_set.all()
|
||||
if request.method == "POST" and form.is_valid():
|
||||
form.save()
|
||||
return redirect(reverse("wifi"))
|
||||
|
||||
if request.POST.get("serial", ""):
|
||||
newserial = request.POST.get("serial", "")
|
||||
if newserial != wifi[0].serial:
|
||||
sigRenameWifi(wifi[0].serial, request.user.username, wifi[0].serial, newserial)
|
||||
wifi[0].serial = newserial
|
||||
|
||||
newssid = request.POST.get("ssid", "")
|
||||
if newssid != wifi[0].ssid:
|
||||
sigSSIDWifi(wifi[0].serial, request.user.username, wifi[0].ssid, newssid)
|
||||
wifi[0].ssid = newssid
|
||||
|
||||
newkey = request.POST.get("key", "")
|
||||
if newkey != wifi[0].key:
|
||||
sigKeyWifi(wifi[0].serial, request.user.username, wifi[0].key, newkey)
|
||||
wifi[0].key = newkey
|
||||
|
||||
wifi[0].save()
|
||||
|
||||
return redirect(reverse("wifi"))
|
||||
|
||||
return render(request, "manager/editwifi.html",
|
||||
{
|
||||
"title": "Edit WiFi",
|
||||
"wifi": wifi[0]
|
||||
}
|
||||
)
|
||||
return render(request, "manager/form.html", { "title": "Edit WiFi", "form": form })
|
||||
|
||||
@user_passes_test(is_superuser)
|
||||
def getconfig(request, device_id):
|
||||
|
@ -394,27 +338,14 @@ def deleteorga(request, orga_id):
|
|||
|
||||
@login_required
|
||||
def makewifi(request):
|
||||
wifi_serial = request.POST.get("serial", "")
|
||||
wifi_ssid = request.POST.get("ssid", "")
|
||||
wifi_key = request.POST.get("key", "")
|
||||
wifi_organization = request.POST.get("organization", "")
|
||||
|
||||
if not (wifi_serial and wifi_organization):
|
||||
return render(request, "manager/addwifi.html",
|
||||
{
|
||||
"title": "Add WiFi"
|
||||
}
|
||||
)
|
||||
|
||||
wifi = Wifi.objects.create(
|
||||
serial = wifi_serial,
|
||||
ssid = wifi_ssid,
|
||||
key = wifi_key,
|
||||
organization = get_object_or_404(Organization, id=wifi_organization, users=request.user)
|
||||
)
|
||||
|
||||
form = WifiForm(request.POST if request.method == "POST" else None, initial={"organization": request.user.userstatus.orga} if request.method == "GET" else None)
|
||||
form.fields["organization"].queryset = request.user.organization_set.all()
|
||||
if request.method == "POST" and form.is_valid():
|
||||
form.save()
|
||||
return redirect(reverse("wifi"))
|
||||
|
||||
return render(request, "manager/form.html", { "title": "Add WiFi", "form": form })
|
||||
|
||||
@user_passes_test(is_superuser)
|
||||
def makenetwork(request):
|
||||
if request.method == "POST":
|
||||
|
|
Loading…
Reference in a new issue