dnmgmt/whoisdb/views.py

307 lines
9.0 KiB
Python

from django.shortcuts import render, get_object_or_404
from django.db.models import Q
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect, Http404
from django.urls import reverse, reverse_lazy
from django.views.generic import DetailView, CreateView, UpdateView
from django.contrib.auth.mixins import LoginRequiredMixin
from .models import Maintainer, Contact, InetNum, ASBlock, ASNumber
from .forms import MntForm, MntInitialForm, ContactForm, ContactInitialForm, InetNumForm, ASBlockForm, ASNumberForm
from .generic import DeleteCheckView, MntGenericMixin
@login_required
def createObjectOverview(request):
mnts = request.user.maintainer_set.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()
return render(request, "whoisdb/create_overview.html", {"netblocks": netblocks, "asblocks": asblocks})
@login_required
def dbDashboard(request):
mnts = request.user.maintainer_set.all()
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, person=True, 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, person=True, 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})
def showHandle(request, handle):
# a) find handle
models = [Contact, Maintainer, ASBlock, ASNumber, InetNum]
obj = None
for model in models:
if handle.endswith(model.handleSuffix):
obj = get_object_or_404(model, handle=handle)
break
if not obj:
raise Http404("Handle object not found")
return render(request, "whoisdb/handle_show.html", {"object": obj})
class MaintainerCreate(LoginRequiredMixin, CreateView):
template_name = "whoisdb/obj_create.html"
form_class = MntForm
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",
}
return kwargs
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()
return super(MaintainerCreate, self).form_valid(form)
class MaintainerEdit(LoginRequiredMixin, UpdateView):
template_name = "whoisdb/maintainer_edit.html"
model = Maintainer
form_class = MntForm
slug_field = "handle"
slug_url_kwarg = "handle"
def get_form_kwargs(self, *args, **kwargs):
kwargs = super(MaintainerEdit, self).get_form_kwargs(*args, **kwargs)
kwargs["user"] = self.request.user
return kwargs
def get_queryset(self):
return self.model.objects.filter(auth=self.request.user)
class MaintainerDelete(LoginRequiredMixin, DeleteCheckView):
template_name = "whoisdb/obj_delete.html"
model = Maintainer
slug_field = "handle"
slug_url_kwarg = "handle"
success_url = reverse_lazy("whoisdb:dashboard")
def get_queryset(self):
return self.model.objects.filter(auth=self.request.user)
class MaintainerDetail(LoginRequiredMixin, DetailView):
template_name = "whoisdb/handle_show.html"
model = Maintainer
slug_field = "handle"
slug_url_kwarg = "handle"
#context_object_name = "mnt"
class ContactDetail(DetailView):
model = Contact
slug_field = "handle"
slug_url_kwarg = "handle"
context_object_name = "contact"
class ContactEdit(MntGenericMixin, LoginRequiredMixin, UpdateView):
template_name = "whoisdb/obj_edit.html"
model = Contact
form_class = ContactForm
slug_field = "handle"
slug_url_kwarg = "handle"
def get_form_kwargs(self, *args, **kwargs):
kwargs = super(ContactEdit, self).get_form_kwargs(*args, **kwargs)
kwargs["user"] = self.request.user
return kwargs
class ContactCreate(LoginRequiredMixin, CreateView):
template_name = "whoisdb/obj_create.html"
form_class = ContactForm
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
class ContactDelete(MntGenericMixin, LoginRequiredMixin, DeleteCheckView):
template_name = "whoisdb/obj_delete.html"
model = Contact
slug_field = "handle"
slug_url_kwarg = "handle"
success_url = reverse_lazy("whoisdb:dashboard")
# InetNum
class InetNumCreate(LoginRequiredMixin, CreateView):
template_name = "whoisdb/obj_create.html"
form_class = InetNumForm
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",
}
return kwargs
class InetNumDetail(DetailView):
model = InetNum
slug_field = "handle"
slug_url_kwarg = "handle"
context_object_name = "inetnum"
class InetNumEdit(MntGenericMixin, LoginRequiredMixin, UpdateView):
template_name = "whoisdb/obj_edit.html"
model = InetNum
form_class = InetNumForm
slug_field = "handle"
slug_url_kwarg = "handle"
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())}
return kwargs
class InetNumDelete(MntGenericMixin, LoginRequiredMixin, DeleteCheckView):
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):
template_name = "whoisdb/obj_create.html"
form_class = ASBlockForm
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",
}
return kwargs
class ASBlockDetail(DetailView):
model = ASBlock
slug_field = "handle"
slug_url_kwarg = "handle"
context_object_name = "asblock"
class ASBlockEdit(MntGenericMixin, LoginRequiredMixin, UpdateView):
template_name = "whoisdb/obj_edit.html"
model = ASBlock
form_class = ASBlockForm
slug_field = "handle"
slug_url_kwarg = "handle"
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())}
return kwargs
class ASBlockDelete(MntGenericMixin, LoginRequiredMixin, DeleteCheckView):
template_name = "whoisdb/obj_delete.html"
model = ASBlock
slug_field = "handle"
slug_url_kwarg = "handle"
success_url = reverse_lazy("whoisdb:dashboard")
# asnumber
class ASNumberCreate(LoginRequiredMixin, CreateView):
template_name = "whoisdb/obj_create.html"
form_class = ASNumberForm
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",
}
return kwargs
class ASNumberDetail(DetailView):
model = ASNumber
slug_field = "handle"
slug_url_kwarg = "handle"
context_object_name = "asnumber"
class ASNumberEdit(MntGenericMixin, LoginRequiredMixin, UpdateView):
template_name = "whoisdb/obj_edit.html"
model = ASNumber
form_class = ASNumberForm
slug_field = "handle"
slug_url_kwarg = "handle"
def get_form_kwargs(self, *args, **kwargs):
kwargs = super(ASNumberEdit, self).get_form_kwargs(*args, **kwargs)
kwargs["user"] = self.request.user
return kwargs
class ASNumberDelete(MntGenericMixin, LoginRequiredMixin, DeleteCheckView):
template_name = "whoisdb/obj_delete.html"
model = ASNumber
slug_field = "handle"
slug_url_kwarg = "handle"
success_url = reverse_lazy("whoisdb:dashboard")