Zwischencommit

This commit is contained in:
Sebastian Lohff 2017-03-22 19:11:19 +01:00
parent 26ff25bfd3
commit 8dbd77dc79
7 changed files with 69 additions and 29 deletions

View File

@ -2,8 +2,9 @@ from django.shortcuts import render
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.db.models import Q from django.db.models import Q
from django.views.generic import CreateView from django.views.generic import CreateView
from django.urls import reverse_lazy from django.urls import reverse_lazy, reverse
from django.contrib import messages from django.contrib import messages
from django.http import HttpResponseRedirect
from whoisdb.models import ASNumber, InetNum from whoisdb.models import ASNumber, InetNum
@ -30,6 +31,9 @@ def dashboard(request):
def index(request): def index(request):
if request.user.is_authenticated():
return HttpResponseRedirect(reverse("dashboard"))
return render(request, "index.html", {}) return render(request, "index.html", {})

View File

@ -62,6 +62,10 @@ class NameserverForm(MntFormMixin, forms.ModelForm):
model = Nameserver model = Nameserver
fields = ['name', 'glueIPv4', 'glueIPv6', 'mnt_by', 'admin_c'] fields = ['name', 'glueIPv4', 'glueIPv6', 'mnt_by', 'admin_c']
help_texts = {
"glueIPv4": "Note: You can only set a glue record if the base domain of this nameserver belongs to you!"
}
def __init__(self, user, *args, **kwargs): def __init__(self, user, *args, **kwargs):
self._user = user self._user = user
@ -78,24 +82,51 @@ class NameserverForm(MntFormMixin, forms.ModelForm):
if name.count(".") <= 2: if name.count(".") <= 2:
raise forms.ValidationError("Nameserver must be inside a domain (e.g. ns1.noot.dn.)") raise forms.ValidationError("Nameserver must be inside a domain (e.g. ns1.noot.dn.)")
zone = ".".join(name.split(".")[-3:])
mnts = self._user.maintainer_set.all() mnts = self._user.maintainer_set.all()
domains = Domain.objects.filter(mnt_by__in=mnts) try:
found = False obj = Nameserver.objects.get(name=name, mnt_by__in=mnts)
for domain in domains: if self._create or not self._create and obj.pk != self.instance.pk:
if domain.name == zone: raise forms.ValidationError("You already have a nameserver with this name under your control")
found = True except Nameserver.DoesNotExist:
break pass
except Nameserver.MultipleObjectsReturned:
pass
if not found: #zone = ".".join(name.split(".")[-3:])
raise forms.ValidationError("This nameserver is not under a domain you control.") #mnts = self._user.maintainer_set.all()
#domains = Domain.objects.filter(mnt_by__in=mnts)
#found = False
#for domain in domains:
# if domain.name == zone:
# found = True
# break
#if not found:
# raise forms.ValidationError("This nameserver is not under a domain you control.")
return name return name
def clean(self): def clean(self):
cleaned_data = super(NameserverForm, self).clean() cleaned_data = super(NameserverForm, self).clean()
if not self.errors:
name = cleaned_data.get("name")
zone = ".".join(name.split(".")[-3:])
ipv4 = cleaned_data.get("glueIPv4")
ipv6 = cleaned_data.get("glueIPv6")
if (ipv4 or ipv6) and self._create or not (self.instance.glueIPv4 == ipv4 and self.instance.glueIPv6 == ipv6):
mnts = self._user.maintainer_set.all()
domains = Domain.objects.filter(mnt_by__in=mnts)
found = False
for domain in domains:
if domain.name == zone:
found = True
break
if not found:
raise forms.ValidationError("This nameserver is not under a domain you control.")
return cleaned_data return cleaned_data

View File

@ -15,21 +15,25 @@ class Nameserver(MntdObject):
# ip address, if glue # ip address, if glue
# ipv4/ipv6 address? # ipv4/ipv6 address?
handle = None handle = None
name = models.CharField(max_length=256, unique=True) name = models.CharField(max_length=256)
glueIPv4 = models.GenericIPAddressField(protocol='IPv4', blank=True, null=True) glueIPv4 = models.GenericIPAddressField(protocol='IPv4', blank=True, null=True)
glueIPv6 = models.GenericIPAddressField(protocol='IPv6', blank=True, null=True) glueIPv6 = models.GenericIPAddressField(protocol='IPv6', blank=True, null=True)
admin_c = models.ManyToManyField(Contact) admin_c = models.ManyToManyField(Contact)
def getPK(self): def getPK(self):
return self.name return self.pk
def get_absolute_url(self): def get_absolute_url(self):
return reverse("domains:nameserver-show", args=(self.name,)) return reverse("domains:nameserver-show", args=(self.pk,))
def __str__(self): def __str__(self):
return self.name return self.name
def getNoDeleteReasons(self):
# nameservers can always be deleted
return []
class Domain(MntdObject): class Domain(MntdObject):
handle = None handle = None
@ -53,6 +57,7 @@ class Domain(MntdObject):
nameservers = Nameserver.objects.filter(name__endswith="." + self.name) nameservers = Nameserver.objects.filter(name__endswith="." + self.name)
for ns in nameservers: for ns in nameservers:
# FIXME: check if the nameserver has a glue record.... and also if it is maintained by us
reasons.append("Nameserver %s depends on this domain" % ns.name) reasons.append("Nameserver %s depends on this domain" % ns.name)
return reasons return reasons

View File

@ -11,9 +11,9 @@ urlpatterns = [
url(r'domain/delete/(?P<domain>[a-z0-9.-]+)/$', domains_views.DomainDelete.as_view(), name='domain-delete'), url(r'domain/delete/(?P<domain>[a-z0-9.-]+)/$', domains_views.DomainDelete.as_view(), name='domain-delete'),
url(r'nameserver/create/$', domains_views.NameserverCreate.as_view(), name='nameserver-create'), url(r'nameserver/create/$', domains_views.NameserverCreate.as_view(), name='nameserver-create'),
url(r'nameserver/show/(?P<domain>[a-z0-9.-]+)/$', domains_views.NameserverDetail.as_view(), name='nameserver-show'), url(r'nameserver/show/(?P<pk>[0-9]+)/$', domains_views.NameserverDetail.as_view(), name='nameserver-show'),
url(r'nameserver/edit/(?P<domain>[a-z0-9.-]+)/$', domains_views.NameserverEdit.as_view(), name='nameserver-edit'), url(r'nameserver/edit/(?P<pk>[0-9]+)/$', domains_views.NameserverEdit.as_view(), name='nameserver-edit'),
url(r'nameserver/delete/(?P<domain>[a-z0-9.-]+)/$', domains_views.NameserverDelete.as_view(), name='nameserver-delete'), url(r'nameserver/delete/(?P<pk>[0-9]+)/$', domains_views.NameserverDelete.as_view(), name='nameserver-delete'),
url(r'reversezone/create/$', domains_views.ReverseZoneCreate.as_view(), name='reversezone-create'), url(r'reversezone/create/$', domains_views.ReverseZoneCreate.as_view(), name='reversezone-create'),
url(r'reversezone/show/(?P<pk>[0-9]+)/$', domains_views.ReverseZoneDetail.as_view(), name='reversezone-show'), url(r'reversezone/show/(?P<pk>[0-9]+)/$', domains_views.ReverseZoneDetail.as_view(), name='reversezone-show'),

View File

@ -77,8 +77,8 @@ class NameserverCreate(LoginRequiredMixin, CreateView):
class NameserverDetail(LoginRequiredMixin, DetailView): class NameserverDetail(LoginRequiredMixin, DetailView):
model = Nameserver model = Nameserver
slug_field = "name" #slug_field = "name"
slug_url_kwarg = "domain" #slug_url_kwarg = "domain"
#context_object_name = "nameserver" #context_object_name = "nameserver"
template_name = "domains/handle_show.html" template_name = "domains/handle_show.html"
@ -86,8 +86,8 @@ class NameserverDetail(LoginRequiredMixin, DetailView):
class NameserverEdit(MntGenericMixin, LoginRequiredMixin, UpdateView): class NameserverEdit(MntGenericMixin, LoginRequiredMixin, UpdateView):
model = Nameserver model = Nameserver
form_class = NameserverForm form_class = NameserverForm
slug_field = "name" #slug_field = "name"
slug_url_kwarg = "domain" #slug_url_kwarg = "domain"
template_name = "domains/obj_edit.html" template_name = "domains/obj_edit.html"
def get_form_kwargs(self, *args, **kwargs): def get_form_kwargs(self, *args, **kwargs):
@ -99,8 +99,8 @@ class NameserverEdit(MntGenericMixin, LoginRequiredMixin, UpdateView):
class NameserverDelete(MntGenericMixin, LoginRequiredMixin, DeleteCheckView): class NameserverDelete(MntGenericMixin, LoginRequiredMixin, DeleteCheckView):
template_name = "domains/obj_delete.html" template_name = "domains/obj_delete.html"
model = Nameserver model = Nameserver
slug_field = "name" #slug_field = "name"
slug_url_kwarg = "domain" #slug_url_kwarg = "domain"
success_url = reverse_lazy("domains:overview") success_url = reverse_lazy("domains:overview")
@ -117,9 +117,9 @@ class ReverseZoneCreate(LoginRequiredMixin, CreateView):
class ReverseZoneDetail(LoginRequiredMixin, DetailView): class ReverseZoneDetail(LoginRequiredMixin, DetailView):
model = ReverseZone model = ReverseZone
slug_field = "name" #slug_field = "name"
slug_url_kwarg = "domain" #slug_url_kwarg = "domain"
context_object_name = "nameserver" #context_object_name = "nameserver"
template_name = "domains/handle_show.html" template_name = "domains/handle_show.html"

View File

@ -19,11 +19,11 @@
</tr> </tr>
{% for nameserver in nameservers %} {% for nameserver in nameservers %}
<tr> <tr>
<td><a href="{% url "domains:nameserver-show" nameserver.name %}">{{ nameserver.name }}</a></td> <td><a href="{% url "domains:nameserver-show" nameserver.getPK %}">{{ nameserver.name }}</a></td>
<td>{{ nameserver.glueIPv4|default:"-" }}</td> <td>{{ nameserver.glueIPv4|default:"-" }}</td>
<td>{{ nameserver.glueIPv6|default:"-" }}</td> <td>{{ nameserver.glueIPv6|default:"-" }}</td>
<td>{{ nameserver.mnt_by.all|linkObjects }}</td> <td>{{ nameserver.mnt_by.all|linkObjects }}</td>
<td><a href="{% url "domains:nameserver-edit" nameserver.name %}">Edit</a> <a href="{% url "domains:nameserver-delete" nameserver.name %}">Delete</a></td> <td><a href="{% url "domains:nameserver-edit" nameserver.getPK %}">Edit</a> <a href="{% url "domains:nameserver-delete" nameserver.getPK %}">Delete</a></td>
</tr> </tr>
{% endfor %} {% endfor %}
</table> </table>

View File

@ -67,7 +67,7 @@ class WhoisObject(models.Model):
return handle % i return handle % i
def getNoDeleteReasons(self): def getNoDeleteReasons(self):
raise NotImplementedError() raise NotImplementedError("Delete reason checking is not implemented for this model")
def canBeDeleted(self): def canBeDeleted(self):
return not bool(self.getNoDeleteReasons()) return not bool(self.getNoDeleteReasons())