155 lines
5.2 KiB
Python
Raw Normal View History

2013-09-28 12:06:26 +02:00
# -*- coding: utf-8 -*-
2013-09-28 22:46:53 +02:00
from django.views.generic import TemplateView, CreateView
from django.views.generic.edit import UpdateView, DeleteView
2013-09-29 16:03:56 +02:00
from django.http import HttpResponseRedirect
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-09-29 00:28:31 +02:00
import dns.inet
2013-09-28 22:46:53 +02:00
from main.forms import CreateHostForm, EditHostForm
2013-09-28 22:20:19 +02:00
from main.models import Host
2013-09-28 12:06:26 +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-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):
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
s = self.request.session
2013-09-29 00:28:31 +02:00
ipaddr = self.request.META['REMOTE_ADDR']
af = dns.inet.af_for_address(ipaddr)
key = 'ipv4' if af == dns.inet.AF_INET else 'ipv6'
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-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-09-28 22:46:53 +02:00
class OverviewView(CreateView):
model = Host
template_name = "main/overview.html"
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):
return reverse('generate_secret_view', args=(self.object.pk,))
2013-09-28 22:46:53 +02:00
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.created_by = self.request.user
self.object.save()
2013-09-28 23:03:34 +02:00
messages.add_message(self.request, messages.SUCCESS, 'Host added.')
2013-09-28 22:46:53 +02:00
return HttpResponseRedirect(self.get_success_url())
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
class HostView(UpdateView):
model = Host
template_name = "main/host.html"
form_class = EditHostForm
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(HostView, self).dispatch(*args, **kwargs)
2013-09-29 01:21:44 +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())
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
return obj
def get_context_data(self, *args, **kwargs):
context = super(HostView, 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 01:21:44 +02:00
class DeleteHostView(DeleteView):
model = Host
template_name = "main/delete_host.html"
form_class = EditHostForm
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(DeleteHostView, self).dispatch(*args, **kwargs)
2013-09-29 01:21:44 +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
return obj
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