66 lines
2.1 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
2013-09-28 21:32:29 +02:00
from django.views.generic.list import ListView
2013-09-28 22:46:53 +02:00
from django.http import HttpResponse, HttpResponseRedirect
from django.conf import settings
from django.shortcuts import render, get_object_or_404
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-28 22:20:19 +02:00
from main.forms import HostForm
from main.models import Host
2013-09-28 12:06:26 +02:00
2013-09-28 12:06:26 +02:00
class HomeView(TemplateView):
template_name = "base.html"
def get_context_data(self, *args, **kwargs):
context = super(HomeView, self).get_context_data(*args, **kwargs)
context['nav_home'] = True
return context
2013-09-28 22:46:53 +02:00
class OverviewView(CreateView):
model = Host
template_name = "main/overview.html"
form_class = HostForm
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('overview')
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 22:22:42 +02:00
@login_required
def HostView(request,pk=None):
context = {}
context['nav_overview'] = True
context['HostForm'] = HostForm(request.user,instance=get_object_or_404(Host, pk=pk, created_by=request.user))
if request.method == "POST":
print "POST"
form = HostForm(request.user, request.POST,)
print form
if form.is_valid():
print "valid"
host = form.save(request.user)
context['HostForm'] = form
return render(request, "main/host.html", context)
2013-09-28 22:22:42 +02:00