overview as class based view

This commit is contained in:
Arne Schauf 2013-09-28 22:46:53 +02:00
parent 3af1d10014
commit eb8f548f95
4 changed files with 35 additions and 46 deletions

View File

@ -1,7 +1,5 @@
# -*- coding: utf-8 -*-
from django import forms
from django.contrib.auth.models import User
from main.models import Host
class HostForm(forms.ModelForm):
@ -9,24 +7,3 @@ class HostForm(forms.ModelForm):
model = Host
fields = ['fqdn', 'comment', 'update_secret']
def __init__(self, user, *args, **kwargs):
super(HostForm, self).__init__(*args, **kwargs)
self.created_by = user
def save(self, user, commit=True):
instance = super(HostForm, self).save(commit=False)
instance.created_by = user
if commit:
instance.save()
return instance
def create_host(self, user):
self.clean()
host = Host(fqdn=self.cleaned_data['fqdn'],
comment=self.cleaned_data['comment'],
update_secret=self.cleaned_data['update_secret'],
created_by=user)
host.save()
# TODO: Update NS with self.cleaned_data['ipv4addr']
return host

View File

@ -8,12 +8,12 @@
<h3>Host List</h3>
<table class="table">
<tr><th>domain</th><th>last update</th><th>comment</th><th>action</th></tr>
{% for host in Hosts %}
{% for host in hosts %}
<tr>
<td><b>{{ host.fqdn }}</b></td>
<td>{{ host.last_update|date }}</td>
<td>{{ host.comment }}</td>
<td><a href="{% url 'host-view' host.pk %}"><span class="glyphicon glyphicon-edit"></span></a>
<td><a href="{% url 'host_view' host.pk %}"><span class="glyphicon glyphicon-edit"></span></a>
</tr>
{% empty %}
<tr>No hosts yet.</tr>
@ -24,7 +24,7 @@
<div class="col-lg-4">
<form method="post" action="">
{% csrf_token %}
{{ HostForm|bootstrap }}
{{ form|bootstrap }}
<button type="submit" class="btn btn-primary">Create</button>
</form>
</div>
@ -39,4 +39,4 @@
<img src="//{{ WWW_IPV6_HOST }}/updateip" />
</div>
{% endblock %}
{% endblock %}

View File

@ -8,8 +8,8 @@ from api.views import (
urlpatterns = patterns('',
url(r'^$', HomeView.as_view(), name="home"),
url(r'^overview/$', OverviewView, name='overview'),
url(r'^host/(?P<pk>\w+)$', HostView, name='host-view'),
url(r'^overview/$', OverviewView.as_view(), name='overview'),
url(r'^host/(?P<pk>\d+)/$', HostView, name='host_view'),
url(r'^myip$', MyIpView),
url(r'^updateip$', UpdateIpView),
url(r'^nic/update$', NicUpdateView),

View File

@ -1,10 +1,13 @@
# -*- coding: utf-8 -*-
from django.views.generic import TemplateView
from django.views.generic import TemplateView, CreateView
from django.views.generic.list import ListView
from django.http import HttpResponse
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
from django.utils.decorators import method_decorator
from django.core.urlresolvers import reverse
from main.forms import HostForm
from main.models import Host
@ -24,23 +27,32 @@ class HomeView(TemplateView):
context['nav_home'] = True
return context
@login_required
def OverviewView(request):
context = create_context(request)
context['nav_overview'] = True
context['HostForm'] = HostForm(request.user)
context['Hosts'] = Host.objects.filter(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.save()
class OverviewView(CreateView):
model = Host
template_name = "main/overview.html"
form_class = HostForm
context['HostForm'] = form
return render(request, "main/overview.html", context)
@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()
#FIXME: add messages to base.html
#messages.add_message(self.request, messages.SUCCESS, 'Host added.'))
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
@login_required