From f25bf54f2666175d68f9f4b9375f071007beab3c Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Sat, 2 Feb 2019 15:08:20 +0000 Subject: [PATCH] Condense code --- manager/views.py | 57 +++++++++++++++++++----------------------------- 1 file changed, 23 insertions(+), 34 deletions(-) diff --git a/manager/views.py b/manager/views.py index 6b06fc4..e252276 100644 --- a/manager/views.py +++ b/manager/views.py @@ -204,62 +204,51 @@ def devices(request): @login_required def editdevice(request, device_id): - device = None - subnets = set() - wifis = set() - - for organization in Organization.objects.filter(users=request.user): - device = device or Device.objects.filter(id=device_id, organization=organization) - if not device: - return redirect("/devices/") - - for subnet in Network.objects.filter(organization=device[0].organization): - subnets.add(subnet) - - for wifi in Wifi.objects.filter(organization=device[0].organization): - wifis.add(wifi) + device = get_object_or_404(Organization, id=device_id, organization__in=request.user.organization_set.all()) + subnets = Network.objects.filter(organization=device.organization) + wifis = Wifi.objects.filter(organization=device.organization) if request.POST.get("subnet", ""): - subnet = Network.objects.filter(intip=request.POST.get("subnet", device[0].network.intip if device[0].network else "No VPN")) + subnet = Network.objects.get(intip=request.POST.get("subnet", device.network.intip if device.network else "No VPN")) if subnet[0] in subnets: newname = request.POST.get("name", "") - if newname != device[0].name: - sigRenameDevice(device[0].serial, request.user.username, device[0].name, newname) - device[0].name = newname + if newname != device.name: + sigRenameDevice(device.serial, request.user.username, device[0].name, newname) + device.name = newname - if subnet[0] != device[0].network: - sigNetDevice(device[0].serial, request.user.username, str(device[0].network), str(subnet[0])) - device[0].network = subnet[0] + 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[0].reboot: - sigRebootDevice(device[0].serial, request.user.username, newreboot) - device[0].reboot = newreboot + 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[0].update: - sigUpdateDevice(device[0].serial, request.user.username, newupdate) - device[0].update = newupdate + if newupdate != device.update: + sigUpdateDevice(device.serial, request.user.username, newupdate) + device.update = newupdate newwifis = set(request.POST.getlist("wifi", [])) - oldwifis = set(device[0].wifi.all()) + oldwifis = set(device.wifi.all()) if newwifis != oldwifis: - sigWifiDevice(device[0].serial, request.user.username, oldwifis, newwifis) - device[0].wifi.set(newwifis) + sigWifiDevice(device.serial, request.user.username, oldwifis, newwifis) + device.wifi.set(newwifis) - device[0].changed = timezone.now() - device[0].save() + device.changed = timezone.now() + device.save() return redirect("/devices/") return render(request, "manager/edit.html", { "title": "Edit Device", - "device": device[0], + "device": device, "subnets": subnets, "wifis": wifis, - "curfis": Wifi.objects.filter(device=device[0]) + "curfis": Wifi.objects.filter(device=device) } )