Condense code

This commit is contained in:
Kumi 2019-02-02 15:08:20 +00:00
parent c85549c124
commit f25bf54f26

View file

@ -204,62 +204,51 @@ def devices(request):
@login_required @login_required
def editdevice(request, device_id): def editdevice(request, device_id):
device = None device = get_object_or_404(Organization, id=device_id, organization__in=request.user.organization_set.all())
subnets = set() subnets = Network.objects.filter(organization=device.organization)
wifis = set() wifis = Wifi.objects.filter(organization=device.organization)
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)
if request.POST.get("subnet", ""): 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: if subnet[0] in subnets:
newname = request.POST.get("name", "") newname = request.POST.get("name", "")
if newname != device[0].name: if newname != device.name:
sigRenameDevice(device[0].serial, request.user.username, device[0].name, newname) sigRenameDevice(device.serial, request.user.username, device[0].name, newname)
device[0].name = newname device.name = newname
if subnet[0] != device[0].network: if subnet != device.network:
sigNetDevice(device[0].serial, request.user.username, str(device[0].network), str(subnet[0])) sigNetDevice(device.serial, request.user.username, str(device.network), str(subnet))
device[0].network = subnet[0] device.network = subnet
newreboot = True if request.POST.get("reboot", "0") == "True" else False newreboot = True if request.POST.get("reboot", "0") == "True" else False
if newreboot != device[0].reboot: if newreboot != device.reboot:
sigRebootDevice(device[0].serial, request.user.username, newreboot) sigRebootDevice(device.serial, request.user.username, newreboot)
device[0].reboot = newreboot device.reboot = newreboot
newupdate = True if request.POST.get("update", "0") == "True" else False newupdate = True if request.POST.get("update", "0") == "True" else False
if newupdate != device[0].update: if newupdate != device.update:
sigUpdateDevice(device[0].serial, request.user.username, newupdate) sigUpdateDevice(device.serial, request.user.username, newupdate)
device[0].update = newupdate device.update = newupdate
newwifis = set(request.POST.getlist("wifi", [])) newwifis = set(request.POST.getlist("wifi", []))
oldwifis = set(device[0].wifi.all()) oldwifis = set(device.wifi.all())
if newwifis != oldwifis: if newwifis != oldwifis:
sigWifiDevice(device[0].serial, request.user.username, oldwifis, newwifis) sigWifiDevice(device.serial, request.user.username, oldwifis, newwifis)
device[0].wifi.set(newwifis) device.wifi.set(newwifis)
device[0].changed = timezone.now() device.changed = timezone.now()
device[0].save() device.save()
return redirect("/devices/") return redirect("/devices/")
return render(request, "manager/edit.html", return render(request, "manager/edit.html",
{ {
"title": "Edit Device", "title": "Edit Device",
"device": device[0], "device": device,
"subnets": subnets, "subnets": subnets,
"wifis": wifis, "wifis": wifis,
"curfis": Wifi.objects.filter(device=device[0]) "curfis": Wifi.objects.filter(device=device)
} }
) )