dnmgmt/domains/views.py

170 lines
5.0 KiB
Python
Raw Normal View History

2017-03-14 20:18:57 +01:00
from django.shortcuts import render
2017-05-01 06:41:29 +02:00
from django.urls import reverse_lazy, reverse
2017-03-14 20:18:57 +01:00
from django.contrib.auth.decorators import login_required
from django.views.generic import DetailView, CreateView, UpdateView
from django.contrib.auth.mixins import LoginRequiredMixin
2017-03-21 02:36:07 +01:00
from django.db.models import Q
2017-03-14 20:18:57 +01:00
2017-03-15 02:22:08 +01:00
from whoisdb.generic import MntGenericMixin, DeleteCheckView
2017-03-14 20:18:57 +01:00
2017-03-21 02:36:07 +01:00
from .models import Domain, Nameserver, ReverseZone
from .forms import DomainForm, NameserverForm, ReverseZoneForm
2017-03-14 20:18:57 +01:00
2017-03-15 02:22:08 +01:00
2017-03-14 20:18:57 +01:00
@login_required
def overview(request):
mnts = request.user.maintainer_set.all()
# get all domains and nameservers
2017-03-21 02:36:07 +01:00
domains = Domain.objects.filter(mnt_by__in=mnts).distinct()
nameservers = Nameserver.objects.filter(mnt_by__in=mnts).distinct()
2017-03-22 04:06:24 +01:00
reversezones = ReverseZone.objects.filter(Q(parentNet__mnt_by__in=mnts) | Q(parentNet__mnt_lower__in=mnts)).distinct()
2017-03-14 20:18:57 +01:00
return render(request, "domains/overview.html", {"mnts": mnts, "domains": domains, "nameservers": nameservers, 'reversezones': reversezones})
2017-03-14 20:18:57 +01:00
2017-03-15 02:22:08 +01:00
2017-03-14 20:18:57 +01:00
class DomainCreate(LoginRequiredMixin, CreateView):
template_name = "domains/obj_create.html"
form_class = DomainForm
def get_form_kwargs(self, *args, **kwargs):
kwargs = super(DomainCreate, self).get_form_kwargs(*args, **kwargs)
kwargs["user"] = self.request.user
return kwargs
2017-03-15 02:22:08 +01:00
2017-05-01 06:11:54 +02:00
class DomainCheck(LoginRequiredMixin, DetailView):
model = Domain
slug_field = "name"
slug_url_kwarg = "domain"
context_object_name = "domain"
2017-05-01 06:41:29 +02:00
template_name = "domains/dns_check.html"
def get_context_data(self, **kwargs):
ctx = super(DomainCheck, self).get_context_data(**kwargs)
ctx["key"] = self.object.name
ctx["apiUrl"] = reverse("api:domain-check")
return ctx
2017-05-01 06:11:54 +02:00
2017-03-14 20:18:57 +01:00
class DomainDetail(LoginRequiredMixin, DetailView):
model = Domain
slug_field = "name"
slug_url_kwarg = "domain"
2017-03-15 02:22:08 +01:00
context_object_name = "domain"
2017-03-28 01:46:47 +02:00
template_name = "whoisdb/handle_show.html"
2017-03-21 02:36:07 +01:00
2017-03-14 20:18:57 +01:00
class DomainEdit(MntGenericMixin, LoginRequiredMixin, UpdateView):
2017-03-15 02:22:08 +01:00
model = Domain
form_class = DomainForm
slug_field = "name"
slug_url_kwarg = "domain"
template_name = "domains/obj_edit.html"
def get_form_kwargs(self, *args, **kwargs):
kwargs = super(DomainEdit, self).get_form_kwargs(*args, **kwargs)
kwargs["user"] = self.request.user
return kwargs
class DomainDelete(MntGenericMixin, LoginRequiredMixin, DeleteCheckView):
template_name = "domains/obj_delete.html"
model = Domain
slug_field = "name"
slug_url_kwarg = "domain"
success_url = reverse_lazy("domains:overview")
2017-03-14 20:18:57 +01:00
class NameserverCreate(LoginRequiredMixin, CreateView):
template_name = "domains/obj_create.html"
form_class = NameserverForm
def get_form_kwargs(self, *args, **kwargs):
kwargs = super(NameserverCreate, self).get_form_kwargs(*args, **kwargs)
kwargs["user"] = self.request.user
return kwargs
2017-03-15 02:22:08 +01:00
2017-03-14 20:18:57 +01:00
class NameserverDetail(LoginRequiredMixin, DetailView):
model = Nameserver
2017-03-28 01:46:47 +02:00
template_name = "whoisdb/handle_show.html"
2017-03-15 02:22:08 +01:00
class NameserverEdit(MntGenericMixin, LoginRequiredMixin, UpdateView):
model = Nameserver
form_class = NameserverForm
template_name = "domains/obj_edit.html"
2017-03-14 20:18:57 +01:00
2017-03-15 02:22:08 +01:00
def get_form_kwargs(self, *args, **kwargs):
kwargs = super(NameserverEdit, self).get_form_kwargs(*args, **kwargs)
kwargs["user"] = self.request.user
return kwargs
class NameserverDelete(MntGenericMixin, LoginRequiredMixin, DeleteCheckView):
template_name = "domains/obj_delete.html"
model = Nameserver
success_url = reverse_lazy("domains:overview")
2017-03-21 02:36:07 +01:00
class ReverseZoneCreate(LoginRequiredMixin, CreateView):
template_name = "domains/obj_create.html"
form_class = ReverseZoneForm
def get_form_kwargs(self, *args, **kwargs):
kwargs = super(ReverseZoneCreate, self).get_form_kwargs(*args, **kwargs)
kwargs["user"] = self.request.user
return kwargs
2017-05-01 06:41:29 +02:00
class ReverseZoneCheck(LoginRequiredMixin, DetailView):
model = ReverseZone
template_name = "domains/dns_check.html"
def get_context_data(self, **kwargs):
ctx = super(ReverseZoneCheck, self).get_context_data(**kwargs)
ctx["key"] = self.object.id
ctx["apiUrl"] = reverse("api:reversezone-check")
return ctx
2017-03-21 02:36:07 +01:00
class ReverseZoneDetail(LoginRequiredMixin, DetailView):
model = ReverseZone
2017-03-28 01:46:47 +02:00
template_name = "whoisdb/handle_show.html"
2017-03-21 02:36:07 +01:00
class ReverseZoneEdit(MntGenericMixin, LoginRequiredMixin, UpdateView):
model = ReverseZone
form_class = ReverseZoneForm
template_name = "domains/obj_edit.html"
def get_form_kwargs(self, *args, **kwargs):
kwargs = super(ReverseZoneEdit, self).get_form_kwargs(*args, **kwargs)
kwargs["user"] = self.request.user
2017-03-22 04:06:24 +01:00
if "initial" not in kwargs:
2017-03-21 02:36:07 +01:00
kwargs["initial"] = {}
kwargs["initial"]["prefix"] = self.object.prefix()
return kwargs
def get_queryset(self):
mnts = self.request.user.maintainer_set.all()
return ReverseZone.objects.filter(Q(parentNet__mnt_by__in=mnts) | Q(parentNet__mnt_lower__in=mnts)).distinct()
class ReverseZoneDelete(LoginRequiredMixin, DeleteCheckView):
template_name = "domains/obj_delete.html"
model = ReverseZone
success_url = reverse_lazy("domains:overview")
def get_queryset(self):
mnts = self.request.user.maintainer_set.all()
return ReverseZone.objects.filter(Q(parentNet__mnt_by__in=mnts) | Q(parentNet__mnt_lower__in=mnts)).distinct()