2013-09-28 12:06:26 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-12-15 17:09:22 +01:00
|
|
|
"""
|
|
|
|
views for the interactive web user interface
|
|
|
|
"""
|
2013-10-03 19:26:39 +02:00
|
|
|
|
2014-05-28 15:19:21 +02:00
|
|
|
import socket
|
2013-11-03 04:29:14 +01:00
|
|
|
from datetime import timedelta
|
|
|
|
|
2013-10-18 15:30:17 -07:00
|
|
|
from django.db.models import Q
|
2013-11-05 00:24:02 +01:00
|
|
|
from django.views.generic import View, 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-11-24 09:37:47 +01:00
|
|
|
from django.contrib.auth import get_user_model
|
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
|
2014-12-28 14:07:06 +01:00
|
|
|
from django.http import Http404
|
2013-11-03 04:29:14 +01:00
|
|
|
from django.utils.timezone import now
|
2013-10-03 19:26:39 +02:00
|
|
|
|
2013-12-14 00:35:29 +01:00
|
|
|
from . import dnstools
|
2014-09-29 06:16:52 +02:00
|
|
|
from .iptools import normalize_ip
|
2013-09-28 22:46:53 +02:00
|
|
|
|
2014-09-25 20:15:18 +02:00
|
|
|
from .forms import (CreateHostForm, EditHostForm, CreateRelatedHostForm, EditRelatedHostForm,
|
|
|
|
CreateDomainForm, EditDomainForm, CreateUpdaterHostConfigForm, EditUpdaterHostConfigForm)
|
|
|
|
from .models import Host, RelatedHost, Domain, ServiceUpdaterHostConfig
|
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:
|
2014-12-28 14:07:06 +01:00
|
|
|
raise Http404
|
2013-09-29 19:58:08 +02:00
|
|
|
return obj
|
|
|
|
|
2014-11-17 22:09:39 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(GenerateSecretView, self).get_context_data(**kwargs)
|
2014-08-31 01:23:38 +02:00
|
|
|
context['nav_overview'] = True
|
2013-09-29 19:58:08 +02:00
|
|
|
# generate secret, store it hashed and return the plain secret for the context
|
|
|
|
context['update_secret'] = self.object.generate_secret()
|
|
|
|
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:
|
2014-12-28 14:07:06 +01:00
|
|
|
raise Http404
|
2013-11-02 11:29:06 +01:00
|
|
|
return obj
|
|
|
|
|
2014-11-17 22:09:39 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(GenerateNSSecretView, self).get_context_data(**kwargs)
|
2014-09-03 17:16:12 +02:00
|
|
|
context['nav_overview'] = True
|
2013-11-02 11:29:06 +01:00
|
|
|
context['shared_secret'] = self.object.generate_ns_secret()
|
|
|
|
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"
|
|
|
|
|
2014-11-17 22:09:39 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(AboutView, self).get_context_data(**kwargs)
|
2013-09-29 17:06:39 +02:00
|
|
|
context['nav_about'] = True
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2014-01-21 23:48:51 +01:00
|
|
|
class CustomTemplateView(TemplateView):
|
|
|
|
# template_name is set in dispatch method
|
|
|
|
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
self.template_name = 'main/custom/%s' % kwargs.get('template')
|
|
|
|
return super(CustomTemplateView, self).dispatch(*args, **kwargs)
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2014-11-17 22:09:39 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(HomeView, self).get_context_data(**kwargs)
|
2013-09-28 12:06:26 +02:00
|
|
|
context['nav_home'] = True
|
|
|
|
return context
|
2013-09-28 13:04:21 +02:00
|
|
|
|
2013-09-28 22:17:24 +02:00
|
|
|
|
2013-11-03 04:29:14 +01:00
|
|
|
class StatusView(TemplateView):
|
|
|
|
template_name = "main/status.html"
|
|
|
|
|
2013-11-21 04:07:27 +01:00
|
|
|
@method_decorator(login_required)
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
return super(StatusView, self).dispatch(*args, **kwargs)
|
|
|
|
|
2014-11-17 22:09:39 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
2013-11-03 04:29:14 +01:00
|
|
|
context = super(
|
2014-11-17 22:09:39 +01:00
|
|
|
StatusView, self).get_context_data(**kwargs)
|
2013-11-03 04:29:14 +01:00
|
|
|
context['nav_status'] = True
|
|
|
|
context['domains_total'] = Domain.objects.count()
|
|
|
|
context['domains_unavailable'] = Domain.objects.filter(available=False).count()
|
|
|
|
context['domains_public'] = Domain.objects.filter(public=True).count()
|
|
|
|
context['hosts_total'] = Host.objects.count()
|
2013-12-01 14:06:45 +01:00
|
|
|
context['hosts_unavailable'] = Host.objects.filter(available=False).count()
|
|
|
|
context['hosts_abuse'] = Host.objects.filter(abuse=True).count()
|
|
|
|
context['hosts_abuse_blocked'] = Host.objects.filter(abuse_blocked=True).count()
|
2013-11-03 04:29:14 +01:00
|
|
|
t_now = now()
|
|
|
|
before_2d = t_now - timedelta(hours=48)
|
|
|
|
before_2w = t_now - timedelta(days=14)
|
|
|
|
before_2m = t_now - timedelta(days=61)
|
|
|
|
before_2y = t_now - timedelta(days=730)
|
|
|
|
context['hosts_ipv4_2d'] = Host.objects.filter(last_update_ipv4__gt=before_2d).count()
|
|
|
|
context['hosts_ipv6_2d'] = Host.objects.filter(last_update_ipv6__gt=before_2d).count()
|
2014-05-30 02:03:48 +02:00
|
|
|
context['hosts_ipv4_tls_2d'] = Host.objects.filter(last_update_ipv4__gt=before_2d, tls_update_ipv4=True).count()
|
|
|
|
context['hosts_ipv6_tls_2d'] = Host.objects.filter(last_update_ipv6__gt=before_2d, tls_update_ipv6=True).count()
|
2013-11-03 04:29:14 +01:00
|
|
|
context['hosts_ipv4_2w'] = Host.objects.filter(last_update_ipv4__gt=before_2w).count()
|
|
|
|
context['hosts_ipv6_2w'] = Host.objects.filter(last_update_ipv6__gt=before_2w).count()
|
2014-05-30 02:03:48 +02:00
|
|
|
context['hosts_ipv4_tls_2w'] = Host.objects.filter(last_update_ipv4__gt=before_2w, tls_update_ipv4=True).count()
|
|
|
|
context['hosts_ipv6_tls_2w'] = Host.objects.filter(last_update_ipv6__gt=before_2w, tls_update_ipv6=True).count()
|
2013-11-03 04:29:14 +01:00
|
|
|
context['hosts_ipv4_2m'] = Host.objects.filter(last_update_ipv4__gt=before_2m).count()
|
|
|
|
context['hosts_ipv6_2m'] = Host.objects.filter(last_update_ipv6__gt=before_2m).count()
|
2014-05-30 02:03:48 +02:00
|
|
|
context['hosts_ipv4_tls_2m'] = Host.objects.filter(last_update_ipv4__gt=before_2m, tls_update_ipv4=True).count()
|
|
|
|
context['hosts_ipv6_tls_2m'] = Host.objects.filter(last_update_ipv6__gt=before_2m, tls_update_ipv6=True).count()
|
2013-11-03 04:29:14 +01:00
|
|
|
context['hosts_ipv4_2y'] = Host.objects.filter(last_update_ipv4__gt=before_2y).count()
|
|
|
|
context['hosts_ipv6_2y'] = Host.objects.filter(last_update_ipv6__gt=before_2y).count()
|
2014-05-30 02:03:48 +02:00
|
|
|
context['hosts_ipv4_tls_2y'] = Host.objects.filter(last_update_ipv4__gt=before_2y, tls_update_ipv4=True).count()
|
|
|
|
context['hosts_ipv6_tls_2y'] = Host.objects.filter(last_update_ipv6__gt=before_2y, tls_update_ipv6=True).count()
|
2013-11-24 09:37:47 +01:00
|
|
|
user_model = get_user_model()
|
|
|
|
context['users_total'] = user_model.objects.count()
|
|
|
|
context['users_active'] = user_model.objects.filter(is_active=True).count()
|
|
|
|
context['users_created_2d'] = user_model.objects.filter(date_joined__gt=before_2d).count()
|
|
|
|
context['users_loggedin_2d'] = user_model.objects.filter(last_login__gt=before_2d).count()
|
|
|
|
context['users_created_2w'] = user_model.objects.filter(date_joined__gt=before_2w).count()
|
|
|
|
context['users_loggedin_2w'] = user_model.objects.filter(last_login__gt=before_2w).count()
|
|
|
|
context['users_created_2m'] = user_model.objects.filter(date_joined__gt=before_2m).count()
|
|
|
|
context['users_loggedin_2m'] = user_model.objects.filter(last_login__gt=before_2m).count()
|
|
|
|
context['users_created_2y'] = user_model.objects.filter(date_joined__gt=before_2y).count()
|
|
|
|
context['users_loggedin_2y'] = user_model.objects.filter(last_login__gt=before_2y).count()
|
2013-11-03 04:29:14 +01:00
|
|
|
return context
|
|
|
|
|
|
|
|
|
2013-12-06 13:59:26 +01:00
|
|
|
from nsupdate.api.views import basic_challenge, basic_authenticate
|
|
|
|
|
|
|
|
|
2013-12-06 20:44:50 +01:00
|
|
|
class JsUpdateView(TemplateView):
|
2013-12-06 13:59:26 +01:00
|
|
|
template_name = "main/update.html"
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
auth = request.META.get('HTTP_AUTHORIZATION')
|
|
|
|
if auth is None:
|
|
|
|
return basic_challenge("authenticate to update DNS", 'badauth')
|
|
|
|
username, password = basic_authenticate(auth)
|
|
|
|
self.hostname = username
|
|
|
|
self.secret = password
|
2013-12-06 20:44:50 +01:00
|
|
|
return super(JsUpdateView, self).get(request, *args, **kwargs)
|
2013-12-06 13:59:26 +01:00
|
|
|
|
2014-11-17 22:09:39 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(JsUpdateView, self).get_context_data(**kwargs)
|
2013-12-06 13:59:26 +01:00
|
|
|
context['hostname'] = self.hostname
|
|
|
|
context['secret'] = self.secret
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2014-08-31 01:03:19 +02:00
|
|
|
class OverviewView(TemplateView):
|
2014-08-31 01:23:38 +02:00
|
|
|
template_name = "main/overview.html"
|
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)
|
|
|
|
|
2014-11-17 22:09:39 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(OverviewView, self).get_context_data(**kwargs)
|
2014-08-31 01:23:38 +02:00
|
|
|
context['nav_overview'] = True
|
2014-08-31 01:03:19 +02:00
|
|
|
context['hosts'] = Host.objects.filter(created_by=self.request.user)
|
2014-08-31 01:23:38 +02:00
|
|
|
context['your_domains'] = Domain.objects.filter(
|
|
|
|
created_by=self.request.user)
|
|
|
|
context['public_domains'] = Domain.objects.filter(
|
|
|
|
public=True).exclude(created_by=self.request.user)
|
2014-08-31 01:03:19 +02:00
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class AddHostView(CreateView):
|
|
|
|
template_name = "main/host_add.html"
|
|
|
|
model = Host
|
|
|
|
form_class = CreateHostForm
|
|
|
|
|
|
|
|
@method_decorator(login_required)
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
return super(AddHostView, self).dispatch(*args, **kwargs)
|
|
|
|
|
2013-09-28 22:46:53 +02:00
|
|
|
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):
|
2014-08-31 01:03:19 +02:00
|
|
|
form = super(AddHostView, self).get_form(form_class)
|
2013-10-18 15:30:17 -07:00
|
|
|
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:
|
2014-09-29 06:16:52 +02:00
|
|
|
dnstools.add(self.object.get_fqdn(), normalize_ip(self.request.META['REMOTE_ADDR']))
|
2013-11-01 22:26:06 +01:00
|
|
|
except dnstools.Timeout:
|
2013-11-06 01:30:06 +01:00
|
|
|
success, level, msg = False, messages.ERROR, 'Timeout - communicating to name server failed.'
|
2013-11-01 22:26:06 +01:00
|
|
|
except dnstools.NameServerNotAvailable:
|
2013-11-06 01:30:06 +01:00
|
|
|
success, level, msg = False, messages.ERROR, 'Name server unavailable.'
|
2014-05-29 17:27:11 +02:00
|
|
|
except dnstools.NoNameservers:
|
|
|
|
success, level, msg = False, messages.ERROR, 'Resolving failed: No name servers.'
|
2014-04-28 00:47:28 +02:00
|
|
|
except dnstools.DnsUpdateError as e:
|
|
|
|
success, level, msg = False, messages.ERROR, 'DNS update error [%s].' % str(e)
|
2013-12-27 13:03:48 +01:00
|
|
|
except Domain.DoesNotExist:
|
|
|
|
# should not happen: POST data had invalid (base)domain
|
|
|
|
success, level, msg = False, messages.ERROR, 'Base domain does not exist.'
|
2014-05-28 15:19:21 +02:00
|
|
|
except socket.error as err:
|
|
|
|
success, level, msg = False, messages.ERROR, 'Communication to name server failed [%s]' % str(err)
|
2013-11-01 22:26:06 +01:00
|
|
|
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
|
|
|
|
2014-11-17 22:09:39 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(AddHostView, self).get_context_data(**kwargs)
|
2014-08-31 01:23:38 +02:00
|
|
|
context['nav_overview'] = True
|
2013-09-28 22:46:53 +02:00
|
|
|
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:
|
2014-12-28 14:07:06 +01:00
|
|
|
raise Http404
|
2013-09-28 23:54:37 +02:00
|
|
|
return obj
|
|
|
|
|
2014-11-17 22:09:39 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(HostView, self).get_context_data(**kwargs)
|
2014-08-31 01:23:38 +02:00
|
|
|
context['nav_overview'] = True
|
2014-09-29 06:16:52 +02:00
|
|
|
context['remote_addr'] = normalize_ip(self.request.META['REMOTE_ADDR'])
|
2013-09-28 23:45:46 +02:00
|
|
|
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)
|
2014-11-01 19:25:49 +01:00
|
|
|
if obj.created_by != self.request.user or obj.abuse_blocked:
|
2014-09-12 20:53:08 +02:00
|
|
|
# disallow deletion if abuse_blocked is set, otherwise the
|
2014-09-12 21:11:55 +02:00
|
|
|
# abuser can just delete and recreate the host
|
2014-12-28 14:07:06 +01:00
|
|
|
raise 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')
|
|
|
|
|
2014-11-17 22:09:39 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(DeleteHostView, self).get_context_data(**kwargs)
|
2014-08-31 01:23:38 +02:00
|
|
|
context['nav_overview'] = True
|
2013-09-28 23:45:46 +02:00
|
|
|
return context
|
2013-09-29 21:06:11 +02:00
|
|
|
|
|
|
|
|
2014-09-25 20:15:18 +02:00
|
|
|
class RelatedHostOverviewView(TemplateView):
|
|
|
|
template_name = "main/related_host_overview.html"
|
|
|
|
|
|
|
|
@method_decorator(login_required)
|
|
|
|
def dispatch(self, *args, **kwargs):
|
2014-11-08 17:14:46 +01:00
|
|
|
try:
|
|
|
|
self.__main_host = Host.objects.get(pk=kwargs.pop('mpk', None), created_by=self.request.user)
|
|
|
|
except Host.DoesNotExist:
|
2014-12-28 14:07:06 +01:00
|
|
|
raise Http404
|
2014-09-25 20:15:18 +02:00
|
|
|
return super(RelatedHostOverviewView, self).dispatch(*args, **kwargs)
|
|
|
|
|
2014-11-17 22:09:39 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(RelatedHostOverviewView, self).get_context_data(**kwargs)
|
2014-09-25 20:15:18 +02:00
|
|
|
context['nav_overview'] = True
|
2014-11-08 17:14:46 +01:00
|
|
|
context['main_host'] = self.__main_host
|
|
|
|
context['related_hosts'] = RelatedHost.objects.filter(main_host=self.__main_host)
|
2014-09-25 20:15:18 +02:00
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class AddRelatedHostView(CreateView):
|
|
|
|
template_name = "main/related_host_add.html"
|
|
|
|
model = RelatedHost
|
|
|
|
form_class = CreateRelatedHostForm
|
|
|
|
|
|
|
|
@method_decorator(login_required)
|
|
|
|
def dispatch(self, *args, **kwargs):
|
2014-11-08 17:14:46 +01:00
|
|
|
try:
|
|
|
|
self.__main_host = Host.objects.get(pk=kwargs.pop('mpk', None), created_by=self.request.user)
|
|
|
|
except Host.DoesNotExist:
|
2014-12-28 14:07:06 +01:00
|
|
|
raise Http404
|
2014-09-25 20:15:18 +02:00
|
|
|
return super(AddRelatedHostView, self).dispatch(*args, **kwargs)
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse('related_host_overview', args=(self.object.main_host.pk, ))
|
|
|
|
|
|
|
|
def get_form(self, form_class):
|
|
|
|
form = super(AddRelatedHostView, self).get_form(form_class)
|
|
|
|
return form
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
self.object = form.save(commit=False)
|
2014-11-08 17:14:46 +01:00
|
|
|
self.object.main_host = self.__main_host
|
2014-09-25 20:15:18 +02:00
|
|
|
self.object.save()
|
|
|
|
success, level, msg = True, messages.SUCCESS, 'Related host added.'
|
|
|
|
messages.add_message(self.request, level, msg)
|
|
|
|
url = self.get_success_url()
|
|
|
|
return HttpResponseRedirect(url)
|
|
|
|
|
2014-11-17 22:09:39 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(AddRelatedHostView, self).get_context_data(**kwargs)
|
2014-09-25 20:15:18 +02:00
|
|
|
context['nav_overview'] = True
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class RelatedHostView(UpdateView):
|
|
|
|
model = RelatedHost
|
|
|
|
template_name = "main/related_host.html"
|
|
|
|
form_class = EditRelatedHostForm
|
|
|
|
|
|
|
|
@method_decorator(login_required)
|
|
|
|
def dispatch(self, *args, **kwargs):
|
2015-01-02 21:21:37 +01:00
|
|
|
try:
|
|
|
|
self.__main_host = Host.objects.get(pk=kwargs.pop('mpk', None), created_by=self.request.user)
|
|
|
|
except Host.DoesNotExist:
|
|
|
|
raise Http404
|
2014-09-25 20:15:18 +02:00
|
|
|
return super(RelatedHostView, self).dispatch(*args, **kwargs)
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse('related_host_overview', args=(self.object.main_host.pk, ))
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
self.object = form.save(commit=False)
|
|
|
|
self.object.save()
|
|
|
|
messages.add_message(self.request, messages.SUCCESS, 'Related host updated.')
|
|
|
|
return HttpResponseRedirect(self.get_success_url())
|
|
|
|
|
|
|
|
def get_object(self, *args, **kwargs):
|
|
|
|
obj = super(RelatedHostView, self).get_object(*args, **kwargs)
|
2015-01-02 21:21:37 +01:00
|
|
|
if obj.main_host.created_by != self.request.user or obj.main_host != self.__main_host:
|
2014-12-28 14:07:06 +01:00
|
|
|
raise Http404
|
2014-09-25 20:15:18 +02:00
|
|
|
return obj
|
|
|
|
|
2014-11-17 22:09:39 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(RelatedHostView, self).get_context_data(**kwargs)
|
2014-09-25 20:15:18 +02:00
|
|
|
context['nav_overview'] = True
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class DeleteRelatedHostView(DeleteView):
|
|
|
|
model = RelatedHost
|
|
|
|
template_name = "main/delete_object.html"
|
|
|
|
|
|
|
|
@method_decorator(login_required)
|
|
|
|
def dispatch(self, *args, **kwargs):
|
2015-01-02 21:21:37 +01:00
|
|
|
try:
|
|
|
|
self.__main_host = Host.objects.get(pk=kwargs.pop('mpk', None), created_by=self.request.user)
|
|
|
|
except Host.DoesNotExist:
|
|
|
|
raise Http404
|
2014-09-25 20:15:18 +02:00
|
|
|
return super(DeleteRelatedHostView, self).dispatch(*args, **kwargs)
|
|
|
|
|
|
|
|
def get_object(self, *args, **kwargs):
|
|
|
|
obj = super(DeleteRelatedHostView, self).get_object(*args, **kwargs)
|
2015-01-02 21:21:37 +01:00
|
|
|
if obj.main_host.created_by != self.request.user or obj.main_host != self.__main_host:
|
2014-12-28 14:07:06 +01:00
|
|
|
raise Http404
|
2014-09-25 20:15:18 +02:00
|
|
|
return obj
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse('related_host_overview', args=(self.object.main_host.pk, ))
|
|
|
|
|
2014-11-17 22:09:39 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(DeleteRelatedHostView, self).get_context_data(**kwargs)
|
2014-09-25 20:15:18 +02:00
|
|
|
context['nav_overview'] = True
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
2014-08-31 01:03:19 +02:00
|
|
|
class AddDomainView(CreateView):
|
|
|
|
template_name = "main/domain_add.html"
|
|
|
|
model = Domain
|
|
|
|
form_class = CreateDomainForm
|
|
|
|
|
|
|
|
@method_decorator(login_required)
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
return super(AddDomainView, self).dispatch(*args, **kwargs)
|
|
|
|
|
2013-10-18 15:30:17 -07:00
|
|
|
def get_success_url(self):
|
2013-11-24 04:14:31 +01:00
|
|
|
return reverse('generate_ns_secret_view', args=(self.object.pk,))
|
2013-10-18 15:30:17 -07:00
|
|
|
|
|
|
|
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())
|
|
|
|
|
2014-11-17 22:09:39 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(AddDomainView, self).get_context_data(**kwargs)
|
2014-09-03 17:16:12 +02:00
|
|
|
context['nav_overview'] = True
|
2013-10-18 15:30:17 -07:00
|
|
|
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):
|
2014-08-31 01:23:38 +02:00
|
|
|
return reverse('overview')
|
2013-11-02 11:29:06 +01:00
|
|
|
|
|
|
|
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:
|
2014-12-28 14:07:06 +01:00
|
|
|
raise Http404
|
2013-11-02 11:29:06 +01:00
|
|
|
return obj
|
|
|
|
|
2014-11-17 22:09:39 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(DomainView, self).get_context_data(**kwargs)
|
2014-09-03 17:16:12 +02:00
|
|
|
context['nav_overview'] = True
|
2013-11-02 11:29:06 +01:00
|
|
|
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:
|
2014-12-28 14:07:06 +01:00
|
|
|
raise Http404
|
2013-10-18 15:30:17 -07:00
|
|
|
return obj
|
|
|
|
|
|
|
|
def get_success_url(self):
|
2014-08-31 01:23:38 +02:00
|
|
|
return reverse('overview')
|
2013-10-18 15:30:17 -07:00
|
|
|
|
2014-11-17 22:09:39 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(DeleteDomainView, self).get_context_data(**kwargs)
|
2014-09-03 17:16:12 +02:00
|
|
|
context['nav_overview'] = True
|
2013-11-02 12:12:13 +01:00
|
|
|
return context
|
|
|
|
|
2013-10-18 15:30:17 -07:00
|
|
|
|
2013-11-29 10:43:18 +01:00
|
|
|
class UpdaterHostConfigOverviewView(CreateView):
|
|
|
|
model = ServiceUpdaterHostConfig
|
|
|
|
template_name = "main/updater_hostconfig_overview.html"
|
|
|
|
form_class = CreateUpdaterHostConfigForm
|
|
|
|
|
|
|
|
@method_decorator(login_required)
|
|
|
|
def dispatch(self, *args, **kwargs):
|
2014-11-08 17:14:46 +01:00
|
|
|
try:
|
|
|
|
self.__host = Host.objects.get(pk=kwargs.pop('pk', None), created_by=self.request.user)
|
|
|
|
except Host.DoesNotExist:
|
2014-12-28 14:07:06 +01:00
|
|
|
raise Http404
|
2013-11-29 10:43:18 +01:00
|
|
|
return super(UpdaterHostConfigOverviewView, self).dispatch(*args, **kwargs)
|
|
|
|
|
|
|
|
def get_success_url(self):
|
2014-11-08 17:14:46 +01:00
|
|
|
return reverse('updater_hostconfig_overview', args=(self.__host.pk, ))
|
2013-11-29 10:43:18 +01:00
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
self.object = form.save(commit=False)
|
2014-11-08 17:14:46 +01:00
|
|
|
self.object.host = self.__host
|
2013-11-29 10:43:18 +01:00
|
|
|
self.object.created_by = self.request.user
|
|
|
|
self.object.save()
|
|
|
|
messages.add_message(self.request, messages.SUCCESS, 'Service Updater Host Configuration added.')
|
|
|
|
return HttpResponseRedirect(self.get_success_url())
|
|
|
|
|
2014-11-17 22:09:39 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
2013-11-29 10:43:18 +01:00
|
|
|
context = super(
|
2014-11-17 22:09:39 +01:00
|
|
|
UpdaterHostConfigOverviewView, self).get_context_data(**kwargs)
|
2014-11-08 17:14:46 +01:00
|
|
|
context['updater_configs'] = ServiceUpdaterHostConfig.objects.filter(host=self.__host)
|
2013-11-29 10:43:18 +01:00
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class UpdaterHostConfigView(UpdateView):
|
|
|
|
model = ServiceUpdaterHostConfig
|
|
|
|
template_name = "main/updater_hostconfig.html"
|
|
|
|
form_class = EditUpdaterHostConfigForm
|
|
|
|
|
|
|
|
@method_decorator(login_required)
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
return super(UpdaterHostConfigView, self).dispatch(*args, **kwargs)
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
host_pk = self.object.host.pk
|
|
|
|
return reverse('updater_hostconfig_overview', args=(host_pk,))
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
self.object = form.save(commit=False)
|
|
|
|
self.object.save()
|
|
|
|
messages.add_message(self.request, messages.SUCCESS, 'Service Updater Host Configuration updated.')
|
|
|
|
return HttpResponseRedirect(self.get_success_url())
|
|
|
|
|
|
|
|
def get_object(self, *args, **kwargs):
|
|
|
|
obj = super(UpdaterHostConfigView, self).get_object(*args, **kwargs)
|
|
|
|
if obj.created_by != self.request.user:
|
2014-12-28 14:07:06 +01:00
|
|
|
raise Http404
|
2013-11-29 10:43:18 +01:00
|
|
|
return obj
|
|
|
|
|
2014-11-17 22:09:39 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(UpdaterHostConfigView, self).get_context_data(**kwargs)
|
2013-11-29 10:43:18 +01:00
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class DeleteUpdaterHostConfigView(DeleteView):
|
|
|
|
model = ServiceUpdaterHostConfig
|
|
|
|
template_name = "main/delete_object.html"
|
|
|
|
|
|
|
|
@method_decorator(login_required)
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
return super(DeleteUpdaterHostConfigView, self).dispatch(*args, **kwargs)
|
|
|
|
|
|
|
|
def get_object(self, *args, **kwargs):
|
|
|
|
obj = super(DeleteUpdaterHostConfigView, self).get_object(*args, **kwargs)
|
|
|
|
if obj.created_by != self.request.user:
|
2014-12-28 14:07:06 +01:00
|
|
|
raise Http404
|
2013-11-29 10:43:18 +01:00
|
|
|
return obj
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
host_pk = self.object.host.pk
|
|
|
|
return reverse('updater_hostconfig_overview', args=(host_pk,))
|
|
|
|
|
2014-11-17 22:09:39 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(DeleteUpdaterHostConfigView, self).get_context_data(**kwargs)
|
2013-11-29 10:43:18 +01:00
|
|
|
return context
|
|
|
|
|
|
|
|
|
2013-11-05 00:24:02 +01:00
|
|
|
class RobotsTxtView(View):
|
2013-09-29 21:06:11 +02:00
|
|
|
"""
|
|
|
|
Dynamically serve robots.txt content.
|
|
|
|
If you like, you can optimize this by statically serving this by your web server.
|
|
|
|
"""
|
2013-11-05 00:24:02 +01:00
|
|
|
def get(self, request):
|
|
|
|
content = """\
|
2013-09-29 21:06:11 +02:00
|
|
|
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-11-02 13:24:44 +01:00
|
|
|
Disallow: /login/
|
2013-10-19 18:20:40 -07:00
|
|
|
Disallow: /admin/
|
2013-12-15 17:36:38 +01:00
|
|
|
Disallow: /status/
|
2013-09-29 21:06:11 +02:00
|
|
|
Disallow: /myip/
|
2013-12-15 17:36:38 +01:00
|
|
|
Disallow: /detect_ip/
|
|
|
|
Disallow: /ajax_get_ips/
|
2013-09-29 21:06:11 +02:00
|
|
|
Disallow: /nic/update/
|
2013-12-15 17:36:38 +01:00
|
|
|
Disallow: /nic/update_authorized/
|
|
|
|
Disallow: /update/
|
2013-11-02 13:24:44 +01:00
|
|
|
Disallow: /host/
|
2013-12-15 17:36:38 +01:00
|
|
|
Disallow: /overview/
|
2013-11-02 13:24:44 +01:00
|
|
|
Disallow: /domain/
|
2013-12-15 17:36:38 +01:00
|
|
|
Disallow: /updater_hostconfig/
|
|
|
|
Disallow: /updater_hostconfig_overview/
|
2013-09-29 21:06:11 +02:00
|
|
|
"""
|
2013-11-05 00:24:02 +01:00
|
|
|
return HttpResponse(content, content_type="text/plain")
|
2013-11-01 01:05:12 +01:00
|
|
|
|
|
|
|
|
2013-12-18 03:50:41 +01:00
|
|
|
def csrf_failure_view(request, reason): # pragma: no cover (hard to test)
|
2013-11-01 01:05:12 +01:00
|
|
|
"""
|
|
|
|
Django's CSRF middleware's builtin view doesn't tell the user that he needs to have cookies enabled.
|
|
|
|
|
|
|
|
:param request: django request object
|
2013-12-15 18:05:19 +01:00
|
|
|
:param reason: why the csrf check failed
|
2013-11-01 01:05:12 +01:00
|
|
|
: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")
|