dnmgmt/whoisdb/views.py

369 lines
12 KiB
Python
Raw Normal View History

2018-01-17 00:23:00 +01:00
# This file is part of dnmgmt, a number resource management system
# Licensed under GNU General Public License v3 or later
# Written by Sebastian Lohff (seba@someserver.de)
2017-03-20 00:16:33 +01:00
from django.shortcuts import render, get_object_or_404
2017-02-28 19:44:15 +01:00
from django.db.models import Q
2017-02-23 22:28:06 +01:00
from django.contrib.auth.decorators import login_required
2017-03-20 00:16:33 +01:00
from django.http import HttpResponseRedirect, Http404
2017-02-28 19:44:15 +01:00
from django.urls import reverse, reverse_lazy
from django.views.generic import DetailView, CreateView, UpdateView
from django.contrib.auth.mixins import LoginRequiredMixin
2017-03-25 04:11:37 +01:00
from django.contrib import messages
from formtools.wizard.views import SessionWizardView
2017-02-23 22:28:06 +01:00
2017-02-28 19:44:15 +01:00
from .models import Maintainer, Contact, InetNum, ASBlock, ASNumber
from .forms import MntForm, MntInitialForm, ContactForm, ContactInitialForm, InetNumForm, ASBlockForm, ASNumberForm
from .generic import DeleteCheckView, MntGenericMixin
2017-03-27 12:18:02 +02:00
from .helpers import findInDatabase
2017-02-24 03:43:59 +01:00
2017-03-22 04:06:24 +01:00
2017-03-20 00:16:33 +01:00
@login_required
def createObjectOverview(request):
2019-05-30 22:10:55 +02:00
mnts = request.user.maintainer_set.filter().all()
netblocks = InetNum.objects.filter(Q(mnt_by__in=mnts) | Q(mnt_lower__in=mnts)).distinct()
asblocks = ASBlock.objects.filter(Q(mnt_by__in=mnts) | Q(mnt_lower__in=mnts)).distinct()
2017-03-20 00:16:33 +01:00
2019-05-30 22:10:55 +02:00
return render(request, "whoisdb/create_overview.html", {"netblocks": netblocks, "asblocks": asblocks})
2017-03-01 03:21:03 +01:00
2017-03-22 04:06:24 +01:00
2017-02-23 22:28:06 +01:00
@login_required
def dbDashboard(request):
2019-05-30 22:10:55 +02:00
mnts = request.user.maintainer_set.filter(rir=False, lir=False).all()
if request.GET.get("delegated", None) or mnts.count() == 0:
# if user wants to see rir/lir objects or only has rir/lir mnts, use all available mnts
mnts = request.user.maintainer_set.all()
showDelegations = True
else:
showDelegations = False
hasDelegations = request.user.maintainer_set.filter(Q(rir=True) | Q(lir=True)).count() > 0
contacts = Contact.objects.filter(mnt_by__in=mnts)
netblocks = InetNum.objects.filter(Q(mnt_by__in=mnts) | Q(mnt_lower__in=mnts)).distinct()
asblocks = ASBlock.objects.filter(Q(mnt_by__in=mnts) | Q(mnt_lower__in=mnts)).distinct()
asnumbers = ASNumber.objects.filter(Q(mnt_by__in=mnts) | Q(mnt_lower__in=mnts)).distinct()
mntForm = contactForm = None
if mnts.count() == 0:
mntForm = contactForm = None
if request.method == "POST":
mntForm = MntInitialForm(user=request.user, data=request.POST, prefix="mnt")
contactForm = ContactInitialForm(user=request.user, data=request.POST, prefix="contact")
if mntForm.is_valid() and contactForm.is_valid():
mnt = mntForm.save(commit=False)
mnt.handleAuto(request.user.username)
mnt.save()
contact = contactForm.save(commit=False)
contact.handleAuto()
contact.type = Contact.TYPE_PERSON
contact.save()
contact.mnt_by.add(mnt.id)
contact.save()
mnt.auth.add(request.user.id)
mnt.admin_c.add(contact.id)
mnt.save()
return HttpResponseRedirect(reverse("whoisdb:dashboard"))
else:
mntForm = MntInitialForm(user=request.user, prefix="mnt", initial={'handle': 'AUTO', 'description': 'Primary maintainer of %s' % request.user.username})
contactForm = ContactInitialForm(user=request.user, initial={'handle': 'AUTO', 'name': request.user.username.capitalize()}, prefix='contact')
return render(request, "whoisdb/overview.html", {"mnts": mnts, "contacts": contacts, "mntForm": mntForm, "contactForm": contactForm, "netblocks": netblocks, "asblocks": asblocks, "asnumbers": asnumbers, 'hasDelegations': hasDelegations, "showDelegations": showDelegations})
2017-02-28 19:44:15 +01:00
2017-03-01 03:21:03 +01:00
2017-03-20 00:16:33 +01:00
def showHandle(request, handle):
2019-05-30 22:10:55 +02:00
# a) find handle
models = [Contact, Maintainer, ASBlock, ASNumber, InetNum]
obj = None
2017-03-20 00:16:33 +01:00
2019-05-30 22:10:55 +02:00
for model in models:
if handle.endswith(model.handleSuffix):
obj = get_object_or_404(model, handle=handle)
break
2017-03-20 00:16:33 +01:00
2019-05-30 22:10:55 +02:00
if not obj:
raise Http404("Handle object not found")
2017-03-20 00:16:33 +01:00
2019-05-30 22:10:55 +02:00
return render(request, "whoisdb/handle_show.html", {"object": obj})
2017-03-20 00:16:33 +01:00
2017-03-27 12:18:02 +02:00
def searchObject(request):
2019-05-30 22:10:55 +02:00
results = None
term = request.GET.get("q", None)
if term:
results = findInDatabase(term)
return render(request, "whoisdb/search.html", {"results": results, "term": term})
2017-03-27 12:18:02 +02:00
2017-02-28 19:44:15 +01:00
class MaintainerCreate(LoginRequiredMixin, CreateView):
2019-05-30 22:10:55 +02:00
template_name = "whoisdb/obj_create.html"
form_class = MntForm
2017-02-28 19:44:15 +01:00
2019-05-30 22:10:55 +02:00
def get_form_kwargs(self, *args, **kwargs):
kwargs = super(MaintainerCreate, self).get_form_kwargs(*args, **kwargs)
kwargs["user"] = self.request.user
kwargs["initial"] = {
"handle": "AUTO",
#"auth": self.request.user.username,
}
2017-02-28 19:44:15 +01:00
2019-05-30 22:10:55 +02:00
return kwargs
2017-02-28 19:44:15 +01:00
2019-05-30 22:10:55 +02:00
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.handleAuto(self.request.user.username)
self.object.save()
self.object.auth.add(self.request.user)
self.object.save()
2017-02-28 19:44:15 +01:00
2019-05-30 22:10:55 +02:00
return super(MaintainerCreate, self).form_valid(form)
2017-02-28 19:44:15 +01:00
2017-03-01 03:21:03 +01:00
2017-02-28 19:44:15 +01:00
class MaintainerEdit(LoginRequiredMixin, UpdateView):
2019-05-30 22:10:55 +02:00
template_name = "whoisdb/obj_edit.html"
model = Maintainer
form_class = MntForm
slug_field = "handle"
slug_url_kwarg = "handle"
2017-02-28 19:44:15 +01:00
2019-05-30 22:10:55 +02:00
def get_form_kwargs(self, *args, **kwargs):
kwargs = super(MaintainerEdit, self).get_form_kwargs(*args, **kwargs)
kwargs["user"] = self.request.user
return kwargs
2017-02-28 19:44:15 +01:00
2019-05-30 22:10:55 +02:00
def get_queryset(self):
return self.model.objects.filter(auth=self.request.user)
2017-02-28 19:44:15 +01:00
2017-03-01 03:21:03 +01:00
2017-02-28 19:44:15 +01:00
class MaintainerDelete(LoginRequiredMixin, DeleteCheckView):
2019-05-30 22:10:55 +02:00
template_name = "whoisdb/obj_delete.html"
model = Maintainer
slug_field = "handle"
slug_url_kwarg = "handle"
success_url = reverse_lazy("whoisdb:dashboard")
2017-02-28 19:44:15 +01:00
2019-05-30 22:10:55 +02:00
def get_queryset(self):
return self.model.objects.filter(auth=self.request.user)
2017-03-01 03:21:03 +01:00
2017-02-28 19:44:15 +01:00
class MaintainerDetail(LoginRequiredMixin, DetailView):
2019-05-30 22:10:55 +02:00
template_name = "whoisdb/handle_show.html"
model = Maintainer
slug_field = "handle"
slug_url_kwarg = "handle"
#context_object_name = "mnt"
2017-02-28 19:44:15 +01:00
2017-03-01 03:21:03 +01:00
2017-02-28 19:44:15 +01:00
class ContactDetail(DetailView):
2019-05-30 22:10:55 +02:00
model = Contact
slug_field = "handle"
slug_url_kwarg = "handle"
context_object_name = "contact"
2017-02-28 19:44:15 +01:00
2017-03-01 03:21:03 +01:00
class ContactEdit(MntGenericMixin, LoginRequiredMixin, UpdateView):
2019-05-30 22:10:55 +02:00
template_name = "whoisdb/obj_edit.html"
model = Contact
form_class = ContactForm
slug_field = "handle"
slug_url_kwarg = "handle"
2017-02-28 19:44:15 +01:00
2019-05-30 22:10:55 +02:00
def get_form_kwargs(self, *args, **kwargs):
kwargs = super(ContactEdit, self).get_form_kwargs(*args, **kwargs)
kwargs["user"] = self.request.user
return kwargs
2017-02-28 19:44:15 +01:00
2017-03-01 03:21:03 +01:00
2017-02-28 19:44:15 +01:00
class ContactCreate(LoginRequiredMixin, CreateView):
2019-05-30 22:10:55 +02:00
template_name = "whoisdb/obj_create.html"
form_class = ContactForm
2017-02-28 19:44:15 +01:00
2019-05-30 22:10:55 +02:00
def get_form_kwargs(self, *args, **kwargs):
kwargs = super(ContactCreate, self).get_form_kwargs(*args, **kwargs)
kwargs["user"] = self.request.user
kwargs["initial"] = {
"handle": "AUTO",
"type": Contact.TYPE_PERSON
}
return kwargs
2017-02-28 19:44:15 +01:00
2017-03-01 03:21:03 +01:00
class ContactDelete(MntGenericMixin, LoginRequiredMixin, DeleteCheckView):
2019-05-30 22:10:55 +02:00
template_name = "whoisdb/obj_delete.html"
model = Contact
slug_field = "handle"
slug_url_kwarg = "handle"
success_url = reverse_lazy("whoisdb:dashboard")
2017-02-28 19:44:15 +01:00
2017-03-01 03:21:03 +01:00
2017-02-28 19:44:15 +01:00
# InetNum
class InetNumCreate(LoginRequiredMixin, CreateView):
2019-05-30 22:10:55 +02:00
template_name = "whoisdb/obj_create.html"
form_class = InetNumForm
2017-02-23 22:28:06 +01:00
2019-05-30 22:10:55 +02:00
def get_form_kwargs(self, *args, **kwargs):
print("NOOOOOOOOOOOOOOOOOOOOOOOOOOOOT", args, kwargs)
kwargs = super(InetNumCreate, self).get_form_kwargs(*args, **kwargs)
kwargs["user"] = self.request.user
kwargs["initial"] = {
"handle": "AUTO",
}
2017-03-01 03:21:03 +01:00
2019-05-30 22:10:55 +02:00
return kwargs
2017-02-24 03:43:59 +01:00
2017-03-01 03:21:03 +01:00
class InetNumDetail(DetailView):
2019-05-30 22:10:55 +02:00
model = InetNum
slug_field = "handle"
slug_url_kwarg = "handle"
context_object_name = "inetnum"
2017-03-01 03:21:03 +01:00
class InetNumEdit(MntGenericMixin, LoginRequiredMixin, UpdateView):
2019-05-30 22:10:55 +02:00
template_name = "whoisdb/obj_edit.html"
model = InetNum
form_class = InetNumForm
slug_field = "handle"
slug_url_kwarg = "handle"
2017-03-01 03:21:03 +01:00
2019-05-30 22:10:55 +02:00
def get_form_kwargs(self, *args, **kwargs):
kwargs = super(InetNumEdit, self).get_form_kwargs(*args, **kwargs)
kwargs["user"] = self.request.user
kwargs["initial"] = {'prefix': str(self.object.getNetwork())}
2017-03-01 03:21:03 +01:00
2019-05-30 22:10:55 +02:00
return kwargs
2017-03-01 03:21:03 +01:00
class InetNumDelete(MntGenericMixin, LoginRequiredMixin, DeleteCheckView):
2019-05-30 22:10:55 +02:00
template_name = "whoisdb/obj_delete.html"
model = InetNum
slug_field = "handle"
slug_url_kwarg = "handle"
success_url = reverse_lazy("whoisdb:dashboard")
# asblock
class ASBlockCreate(LoginRequiredMixin, CreateView):
2019-05-30 22:10:55 +02:00
template_name = "whoisdb/obj_create.html"
form_class = ASBlockForm
2019-05-30 22:10:55 +02:00
def get_form_kwargs(self, *args, **kwargs):
kwargs = super(ASBlockCreate, self).get_form_kwargs(*args, **kwargs)
kwargs["user"] = self.request.user
kwargs["initial"] = {
"handle": "AUTO",
}
2017-03-01 03:21:03 +01:00
2019-05-30 22:10:55 +02:00
return kwargs
2017-03-01 03:21:03 +01:00
class ASBlockDetail(DetailView):
2019-05-30 22:10:55 +02:00
model = ASBlock
slug_field = "handle"
slug_url_kwarg = "handle"
context_object_name = "asblock"
2017-03-22 04:06:24 +01:00
class ASBlockEdit(MntGenericMixin, LoginRequiredMixin, UpdateView):
2019-05-30 22:10:55 +02:00
template_name = "whoisdb/obj_edit.html"
model = ASBlock
form_class = ASBlockForm
slug_field = "handle"
slug_url_kwarg = "handle"
2019-05-30 22:10:55 +02:00
def get_form_kwargs(self, *args, **kwargs):
kwargs = super(ASBlockEdit, self).get_form_kwargs(*args, **kwargs)
kwargs["user"] = self.request.user
#kwargs["initial"] = {'prefix': str(self.object.getNetwork())}
2019-05-30 22:10:55 +02:00
return kwargs
2017-03-01 03:21:03 +01:00
class ASBlockDelete(MntGenericMixin, LoginRequiredMixin, DeleteCheckView):
2019-05-30 22:10:55 +02:00
template_name = "whoisdb/obj_delete.html"
model = ASBlock
slug_field = "handle"
slug_url_kwarg = "handle"
success_url = reverse_lazy("whoisdb:dashboard")
2017-03-22 04:06:24 +01:00
# asnumber
class ASNumberCreate(LoginRequiredMixin, CreateView):
2019-05-30 22:10:55 +02:00
template_name = "whoisdb/obj_create.html"
form_class = ASNumberForm
2019-05-30 22:10:55 +02:00
def get_form_kwargs(self, *args, **kwargs):
kwargs = super(ASNumberCreate, self).get_form_kwargs(*args, **kwargs)
kwargs["user"] = self.request.user
kwargs["initial"] = {
"handle": "AUTO",
}
2019-05-30 22:10:55 +02:00
return kwargs
class ASNumberDetail(DetailView):
2019-05-30 22:10:55 +02:00
model = ASNumber
slug_field = "handle"
slug_url_kwarg = "handle"
context_object_name = "asnumber"
class ASNumberEdit(MntGenericMixin, LoginRequiredMixin, UpdateView):
2019-05-30 22:10:55 +02:00
template_name = "whoisdb/obj_edit.html"
model = ASNumber
form_class = ASNumberForm
slug_field = "handle"
slug_url_kwarg = "handle"
2019-05-30 22:10:55 +02:00
def get_form_kwargs(self, *args, **kwargs):
kwargs = super(ASNumberEdit, self).get_form_kwargs(*args, **kwargs)
kwargs["user"] = self.request.user
2019-05-30 22:10:55 +02:00
return kwargs
class ASNumberDelete(MntGenericMixin, LoginRequiredMixin, DeleteCheckView):
2019-05-30 22:10:55 +02:00
template_name = "whoisdb/obj_delete.html"
model = ASNumber
slug_field = "handle"
slug_url_kwarg = "handle"
success_url = reverse_lazy("whoisdb:dashboard")
2017-03-25 04:11:37 +01:00
class ASAndSubnetWizard(LoginRequiredMixin, SessionWizardView):
2019-05-30 22:10:55 +02:00
form_list = [ASNumberForm, InetNumForm]
template_name = "whoisdb/wizard.html"
2017-03-25 04:11:37 +01:00
2019-05-30 22:10:55 +02:00
def get_form_initial(self, step):
return {"handle": "AUTO"}
2017-03-25 04:11:37 +01:00
2019-05-30 22:10:55 +02:00
def done(self, form_list, **kwargs):
fl = list(form_list)
asNum = fl[0].save()
net = fl[1].save()
2017-03-25 04:11:37 +01:00
2019-05-30 22:10:55 +02:00
messages.info(self.request, "The following objects have been created: AS %s %s, Subnet %s %s" % (asNum.handle, asNum.number, net.handle, net.getNetwork()))
2017-03-25 04:11:37 +01:00
2019-05-30 22:10:55 +02:00
return HttpResponseRedirect(reverse("whoisdb:dashboard"))
2017-03-25 04:11:37 +01:00
2019-05-30 22:10:55 +02:00
def get_form_kwargs(self, *args, **kwargs):
kwargs = super(ASAndSubnetWizard, self).get_form_kwargs(*args, **kwargs)
kwargs["user"] = self.request.user
2017-03-25 04:11:37 +01:00
2019-05-30 22:10:55 +02:00
return kwargs
2017-03-25 04:11:37 +01:00
2019-05-30 22:10:55 +02:00
def get_context_data(self, *args, **kwargs):
d = super(ASAndSubnetWizard, self).get_context_data(*args, **kwargs)
2017-03-25 04:11:37 +01:00
2019-05-30 22:10:55 +02:00
step = d["wizard"]["steps"].step1
if step == 1:
d["message"] = "Create an AS object"
elif step == 2:
d["message"] = "Create a Subnet"
2017-03-25 04:11:37 +01:00
2019-05-30 22:10:55 +02:00
return d