2013-09-28 12:06:26 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-10-03 19:26:39 +02:00
|
|
|
|
2013-10-18 15:30:17 -07:00
|
|
|
from django.db.models import Q
|
2013-09-28 22:46:53 +02:00
|
|
|
from django.views.generic import TemplateView, CreateView
|
2013-09-28 23:45:46 +02:00
|
|
|
from django.views.generic.edit import UpdateView, DeleteView
|
2013-09-29 21:06:11 +02:00
|
|
|
from django.http import HttpResponse, HttpResponseRedirect
|
2013-09-28 21:38:02 +02:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2013-09-28 23:03:34 +02:00
|
|
|
from django.contrib import messages
|
2013-09-28 22:46:53 +02:00
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.core.urlresolvers import reverse
|
2013-09-29 16:03:56 +02:00
|
|
|
from django.core.exceptions import PermissionDenied
|
2013-10-03 19:26:39 +02:00
|
|
|
|
2013-09-29 21:13:20 +02:00
|
|
|
import dnstools
|
2013-09-28 22:46:53 +02:00
|
|
|
|
2013-11-02 11:29:06 +01:00
|
|
|
from .forms import CreateHostForm, EditHostForm, CreateDomainForm, EditDomainForm
|
2013-10-18 15:30:17 -07:00
|
|
|
from .models import Host, Domain
|
2013-09-28 12:06:26 +02:00
|
|
|
|
2013-09-28 22:17:24 +02:00
|
|
|
|
2013-09-29 19:58:08 +02:00
|
|
|
class GenerateSecretView(UpdateView):
|
|
|
|
model = Host
|
|
|
|
template_name = "main/generate_secret.html"
|
|
|
|
|
|
|
|
@method_decorator(login_required)
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
return super(GenerateSecretView, self).dispatch(*args, **kwargs)
|
|
|
|
|
|
|
|
def get_object(self, *args, **kwargs):
|
|
|
|
obj = super(GenerateSecretView, self).get_object(*args, **kwargs)
|
|
|
|
if obj.created_by != self.request.user:
|
|
|
|
raise PermissionDenied() # or Http404
|
|
|
|
return obj
|
|
|
|
|
|
|
|
def get_context_data(self, *args, **kwargs):
|
|
|
|
context = super(GenerateSecretView, self).get_context_data(*args, **kwargs)
|
|
|
|
context['nav_overview'] = True
|
|
|
|
# generate secret, store it hashed and return the plain secret for the context
|
|
|
|
context['update_secret'] = self.object.generate_secret()
|
|
|
|
context['hosts'] = Host.objects.filter(created_by=self.request.user)
|
|
|
|
messages.add_message(self.request, messages.SUCCESS, 'Host secret created.')
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2013-11-02 11:29:06 +01:00
|
|
|
class GenerateNSSecretView(UpdateView):
|
|
|
|
model = Domain
|
|
|
|
template_name = "main/generate_ns_secret.html"
|
|
|
|
|
|
|
|
@method_decorator(login_required)
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
return super(GenerateNSSecretView, self).dispatch(*args, **kwargs)
|
|
|
|
|
|
|
|
def get_object(self, *args, **kwargs):
|
|
|
|
obj = super(GenerateNSSecretView, self).get_object(*args, **kwargs)
|
|
|
|
if obj.created_by != self.request.user:
|
|
|
|
raise PermissionDenied() # or Http404
|
|
|
|
return obj
|
|
|
|
|
|
|
|
def get_context_data(self, *args, **kwargs):
|
|
|
|
context = super(GenerateNSSecretView, self).get_context_data(*args, **kwargs)
|
|
|
|
context['nav_overview'] = True
|
|
|
|
context['shared_secret'] = self.object.generate_ns_secret()
|
|
|
|
context['domains'] = Domain.objects.filter(created_by=self.request.user)
|
|
|
|
messages.add_message(self.request, messages.SUCCESS, 'Nameserver shared secret created.')
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2013-09-29 17:06:39 +02:00
|
|
|
class AboutView(TemplateView):
|
|
|
|
template_name = "main/about.html"
|
|
|
|
|
|
|
|
def get_context_data(self, *args, **kwargs):
|
|
|
|
context = super(AboutView, self).get_context_data(*args, **kwargs)
|
|
|
|
context['nav_about'] = True
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2013-09-28 12:06:26 +02:00
|
|
|
class HomeView(TemplateView):
|
2013-09-29 14:08:22 +02:00
|
|
|
template_name = "main/home.html"
|
2013-09-28 12:06:26 +02:00
|
|
|
|
|
|
|
def get_context_data(self, *args, **kwargs):
|
|
|
|
context = super(HomeView, self).get_context_data(*args, **kwargs)
|
|
|
|
context['nav_home'] = True
|
2013-09-29 14:33:24 +02:00
|
|
|
|
2013-09-29 00:28:31 +02:00
|
|
|
ipaddr = self.request.META['REMOTE_ADDR']
|
2013-10-03 20:39:55 +02:00
|
|
|
key = dnstools.check_ip(ipaddr)
|
|
|
|
s = self.request.session
|
2013-09-29 14:33:24 +02:00
|
|
|
s[key] = ipaddr
|
2013-09-29 15:17:41 +02:00
|
|
|
s.save()
|
2013-09-29 15:37:58 +02:00
|
|
|
|
2013-09-28 12:06:26 +02:00
|
|
|
return context
|
2013-09-28 13:04:21 +02:00
|
|
|
|
2013-09-28 22:17:24 +02:00
|
|
|
|
2013-09-29 16:58:02 +02:00
|
|
|
class HelpView(TemplateView):
|
|
|
|
template_name = "main/help.html"
|
|
|
|
|
|
|
|
def get_context_data(self, *args, **kwargs):
|
|
|
|
context = super(HelpView, self).get_context_data(*args, **kwargs)
|
|
|
|
context['nav_help'] = True
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2013-10-27 11:28:40 +01:00
|
|
|
class ScreenshotsView(TemplateView):
|
|
|
|
template_name = "main/screenshots.html"
|
|
|
|
|
|
|
|
def get_context_data(self, *args, **kwargs):
|
|
|
|
context = super(ScreenshotsView, self).get_context_data(*args, **kwargs)
|
|
|
|
context['nav_help'] = True
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2013-09-28 22:46:53 +02:00
|
|
|
class OverviewView(CreateView):
|
|
|
|
model = Host
|
|
|
|
template_name = "main/overview.html"
|
2013-09-29 19:58:08 +02:00
|
|
|
form_class = CreateHostForm
|
2013-09-28 16:46:33 +02:00
|
|
|
|
2013-09-28 22:46:53 +02:00
|
|
|
@method_decorator(login_required)
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
return super(OverviewView, self).dispatch(*args, **kwargs)
|
|
|
|
|
|
|
|
def get_success_url(self):
|
2013-09-29 19:58:08 +02:00
|
|
|
return reverse('generate_secret_view', args=(self.object.pk,))
|
2013-09-28 22:46:53 +02:00
|
|
|
|
2013-10-18 15:30:17 -07:00
|
|
|
def get_form(self, form_class):
|
|
|
|
form = super(OverviewView, self).get_form(form_class)
|
|
|
|
form.fields['domain'].queryset = Domain.objects.filter(
|
2013-10-27 05:28:30 +01:00
|
|
|
Q(created_by=self.request.user) | Q(public=True))
|
2013-10-18 15:30:17 -07:00
|
|
|
return form
|
|
|
|
|
2013-09-28 22:46:53 +02:00
|
|
|
def form_valid(self, form):
|
|
|
|
self.object = form.save(commit=False)
|
2013-11-01 22:26:06 +01:00
|
|
|
try:
|
|
|
|
dnstools.add(self.object.get_fqdn(), self.request.META['REMOTE_ADDR'], origin=self.object.domain.domain)
|
|
|
|
except dnstools.Timeout:
|
|
|
|
# XXX should be ERROR, but ERROR is white on web ui!?
|
|
|
|
success, level, msg = False, messages.WARNING, 'Timeout - communicating to name server failed.'
|
|
|
|
except dnstools.NameServerNotAvailable:
|
|
|
|
# XXX should be ERROR, but ERROR is white on web ui!?
|
|
|
|
success, level, msg = False, messages.WARNING, 'Name server unavailable.'
|
|
|
|
else:
|
|
|
|
self.object.created_by = self.request.user
|
|
|
|
self.object.save()
|
|
|
|
success, level, msg = True, messages.SUCCESS, 'Host added.'
|
2013-11-02 10:02:51 +01:00
|
|
|
messages.add_message(self.request, level, msg)
|
2013-11-01 22:26:06 +01:00
|
|
|
url = self.get_success_url() if success else reverse('overview')
|
|
|
|
return HttpResponseRedirect(url)
|
2013-09-28 22:46:53 +02:00
|
|
|
|
|
|
|
def get_context_data(self, *args, **kwargs):
|
|
|
|
context = super(OverviewView, self).get_context_data(*args, **kwargs)
|
|
|
|
context['nav_overview'] = True
|
|
|
|
context['hosts'] = Host.objects.filter(created_by=self.request.user)
|
|
|
|
return context
|
2013-09-28 16:46:33 +02:00
|
|
|
|
2013-09-29 01:21:44 +02:00
|
|
|
|
2013-09-28 23:45:46 +02:00
|
|
|
class HostView(UpdateView):
|
|
|
|
model = Host
|
|
|
|
template_name = "main/host.html"
|
2013-09-29 19:58:08 +02:00
|
|
|
form_class = EditHostForm
|
2013-09-28 23:54:37 +02:00
|
|
|
|
2013-09-28 23:45:46 +02:00
|
|
|
@method_decorator(login_required)
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
return super(HostView, self).dispatch(*args, **kwargs)
|
2013-09-29 01:21:44 +02:00
|
|
|
|
2013-09-28 23:45:46 +02:00
|
|
|
def get_success_url(self):
|
|
|
|
return reverse('overview')
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
self.object = form.save(commit=False)
|
|
|
|
self.object.save()
|
|
|
|
messages.add_message(self.request, messages.SUCCESS, 'Host updated.')
|
|
|
|
return HttpResponseRedirect(self.get_success_url())
|
|
|
|
|
2013-09-28 23:54:37 +02:00
|
|
|
def get_object(self, *args, **kwargs):
|
|
|
|
obj = super(HostView, self).get_object(*args, **kwargs)
|
|
|
|
if obj.created_by != self.request.user:
|
2013-09-29 01:21:44 +02:00
|
|
|
raise PermissionDenied() # or Http404
|
2013-09-28 23:54:37 +02:00
|
|
|
return obj
|
|
|
|
|
2013-09-28 23:45:46 +02:00
|
|
|
def get_context_data(self, *args, **kwargs):
|
|
|
|
context = super(HostView, self).get_context_data(*args, **kwargs)
|
|
|
|
context['nav_overview'] = True
|
2013-09-29 21:55:34 +02:00
|
|
|
context['remote_addr'] = self.request.META['REMOTE_ADDR']
|
2013-09-28 23:45:46 +02:00
|
|
|
context['hosts'] = Host.objects.filter(created_by=self.request.user)
|
|
|
|
return context
|
|
|
|
|
2013-09-29 01:21:44 +02:00
|
|
|
|
2013-09-28 23:45:46 +02:00
|
|
|
class DeleteHostView(DeleteView):
|
|
|
|
model = Host
|
2013-10-18 15:30:17 -07:00
|
|
|
template_name = "main/delete_object.html"
|
2013-09-28 23:54:37 +02:00
|
|
|
|
2013-09-28 23:45:46 +02:00
|
|
|
@method_decorator(login_required)
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
return super(DeleteHostView, self).dispatch(*args, **kwargs)
|
2013-09-29 01:21:44 +02:00
|
|
|
|
2013-09-28 23:54:37 +02:00
|
|
|
def get_object(self, *args, **kwargs):
|
|
|
|
obj = super(DeleteHostView, self).get_object(*args, **kwargs)
|
|
|
|
if obj.created_by != self.request.user:
|
2013-09-29 01:21:44 +02:00
|
|
|
raise PermissionDenied() # or Http404
|
2013-09-28 23:54:37 +02:00
|
|
|
return obj
|
|
|
|
|
2013-09-28 23:45:46 +02:00
|
|
|
def get_success_url(self):
|
|
|
|
return reverse('overview')
|
|
|
|
|
|
|
|
def get_context_data(self, *args, **kwargs):
|
|
|
|
context = super(DeleteHostView, self).get_context_data(*args, **kwargs)
|
|
|
|
context['nav_overview'] = True
|
|
|
|
context['hosts'] = Host.objects.filter(created_by=self.request.user)
|
|
|
|
return context
|
2013-09-29 21:06:11 +02:00
|
|
|
|
|
|
|
|
2013-10-18 15:30:17 -07:00
|
|
|
class DomainOverwievView(CreateView):
|
|
|
|
model = Domain
|
|
|
|
template_name = "main/domain_overview.html"
|
|
|
|
form_class = CreateDomainForm
|
|
|
|
|
|
|
|
@method_decorator(login_required)
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
return super(DomainOverwievView, self).dispatch(*args, **kwargs)
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse('domain_overview')
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
self.object = form.save(commit=False)
|
|
|
|
self.object.created_by = self.request.user
|
|
|
|
self.object.save()
|
|
|
|
messages.add_message(self.request, messages.SUCCESS, 'Domain added.')
|
|
|
|
return HttpResponseRedirect(self.get_success_url())
|
|
|
|
|
|
|
|
def get_context_data(self, *args, **kwargs):
|
|
|
|
context = super(
|
|
|
|
DomainOverwievView, self).get_context_data(*args, **kwargs)
|
|
|
|
context['nav_domains'] = True
|
|
|
|
context['domains'] = Domain.objects.filter(
|
|
|
|
created_by=self.request.user)
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2013-11-02 11:29:06 +01:00
|
|
|
class DomainView(UpdateView):
|
|
|
|
model = Domain
|
|
|
|
template_name = "main/domain.html"
|
|
|
|
form_class = EditDomainForm
|
|
|
|
|
|
|
|
@method_decorator(login_required)
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
return super(DomainView, self).dispatch(*args, **kwargs)
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse('domain_overview')
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
self.object = form.save(commit=False)
|
|
|
|
self.object.save()
|
|
|
|
messages.add_message(self.request, messages.SUCCESS, 'Domain updated.')
|
|
|
|
return HttpResponseRedirect(self.get_success_url())
|
|
|
|
|
|
|
|
def get_object(self, *args, **kwargs):
|
|
|
|
obj = super(DomainView, self).get_object(*args, **kwargs)
|
|
|
|
if obj.created_by != self.request.user:
|
|
|
|
raise PermissionDenied() # or Http404
|
|
|
|
return obj
|
|
|
|
|
|
|
|
def get_context_data(self, *args, **kwargs):
|
|
|
|
context = super(DomainView, self).get_context_data(*args, **kwargs)
|
|
|
|
context['nav_overview'] = True
|
|
|
|
context['domains'] = Domain.objects.filter(created_by=self.request.user)
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2013-10-18 15:30:17 -07:00
|
|
|
class DeleteDomainView(DeleteView):
|
|
|
|
model = Domain
|
|
|
|
template_name = "main/delete_object.html"
|
|
|
|
|
|
|
|
@method_decorator(login_required)
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
return super(DeleteDomainView, self).dispatch(*args, **kwargs)
|
|
|
|
|
|
|
|
def get_object(self, *args, **kwargs):
|
|
|
|
obj = super(DeleteDomainView, self).get_object(*args, **kwargs)
|
|
|
|
if obj.created_by != self.request.user:
|
|
|
|
raise PermissionDenied() # or Http404
|
|
|
|
return obj
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse('domain_overview')
|
|
|
|
|
|
|
|
|
2013-09-29 21:06:11 +02:00
|
|
|
def RobotsTxtView(request):
|
|
|
|
"""
|
|
|
|
Dynamically serve robots.txt content.
|
|
|
|
If you like, you can optimize this by statically serving this by your web server.
|
|
|
|
|
|
|
|
:param request: django request object
|
|
|
|
:return: HttpResponse object
|
|
|
|
"""
|
|
|
|
content = """\
|
|
|
|
User-agent: *
|
|
|
|
Crawl-delay: 10
|
2013-10-19 18:20:40 -07:00
|
|
|
Disallow: /account/
|
2013-09-29 21:06:11 +02:00
|
|
|
Disallow: /accounts/
|
2013-10-19 18:20:40 -07:00
|
|
|
Disallow: /admin/
|
2013-09-29 21:06:11 +02:00
|
|
|
Disallow: /myip/
|
|
|
|
Disallow: /nic/update/
|
2013-10-19 18:20:40 -07:00
|
|
|
Disallow: /overview/
|
2013-09-29 21:06:11 +02:00
|
|
|
"""
|
|
|
|
return HttpResponse(content, content_type="text/plain")
|
2013-11-01 01:05:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
def CsrfFailureView(request, reason):
|
|
|
|
"""
|
|
|
|
Django's CSRF middleware's builtin view doesn't tell the user that he needs to have cookies enabled.
|
|
|
|
|
|
|
|
:param request: django request object
|
|
|
|
:return: HttpResponse object
|
|
|
|
"""
|
|
|
|
if reason == "CSRF cookie not set.":
|
2013-11-01 04:22:53 +01:00
|
|
|
content = """\
|
2013-11-01 01:05:12 +01:00
|
|
|
This site needs cookies (for CSRF protection, for keeping your session after login).
|
|
|
|
|
|
|
|
Please enable cookies in your browser (or otherwise make sure the CSRF cookie can be set).
|
|
|
|
""" % dict(reason=reason)
|
|
|
|
status = 200
|
|
|
|
else:
|
|
|
|
content = """\
|
|
|
|
%(reason)s
|
|
|
|
|
|
|
|
CSRF verification failure.
|
|
|
|
|
|
|
|
Either you are trying to access this site in 'unusual' ways (then please stop doing that), or
|
|
|
|
you found an issue in the code (then please file an issue for this and tell how you got here).
|
|
|
|
""" % dict(reason=reason)
|
|
|
|
status = 403
|
|
|
|
return HttpResponse(content, status=status, content_type="text/plain")
|