vpnmanager/manager/views.py
2018-11-25 22:02:16 +01:00

74 lines
2.5 KiB
Python

from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse
from django.contrib.auth.forms import AuthenticationForm
from django.db.models import Q
from django.views.decorators.csrf import csrf_exempt
from django.utils import timezone
from .models import Device, Organization, Network
def index(request):
if request.user.is_authenticated:
return redirect("/devices")
else:
return redirect("/accounts/login")
@csrf_exempt
def heartbeat(request):
device = get_object_or_404(Device, secret=request.POST.get("secret", ""))
device.curip = request.POST.get("ip", "")
device.lasttime = timezone.now()
device.save()
return HttpResponse("")
def hosts(request):
device = get_object_or_404(Device, secret=request.POST.get("secret", ""))
return render(request, "manager/hosts", {"device": device})
def devices(request):
if request.user.is_authenticated:
user = request.user
organization = Organization.objects.filter(users__username=request.user.username)[0]
devices = Device.objects.filter(organization=organization.id)
return render(request, "manager/index.html",
{
"title": "Device Administration",
"user": user,
"organization": organization,
"devices": devices
}
)
else:
return redirect("/")
def editdevice(request, device_id):
if request.user.is_authenticated:
device = None
subnets = set()
for organization in Organization.objects.filter(users__id=request.user.id):
device = device or Device.objects.filter(id=device_id, organization=organization.id)
for subnet in Network.objects.filter(organizations=organization.id):
subnets.add(subnet)
if not device:
return redirect("/")
if request.POST.get("subnet", ""):
subnet = Network.objects.filter(intip=request.POST.get("subnet", device[0].network.intip))
if subnet[0] in subnets:
device[0].name = request.POST.get("name", "")
device[0].network = subnet[0]
device[0].save()
return redirect("/")
return render(request, "manager/edit.html",
{
"title": "Edit Device",
"device": device[0],
"subnets": subnets
}
)
else:
return redirect("/")