host list and create new host form

This commit is contained in:
Fabian Faessler 2013-09-28 21:32:29 +02:00
parent a96c7b4608
commit 99a3347042
4 changed files with 60 additions and 30 deletions

View File

@ -9,10 +9,16 @@ class HostForm(forms.ModelForm):
model = Host
fields = ['fqdn', 'comment', ]
def create_host(self):
def __init__(self, user, *args, **kwargs):
super(HostForm, self).__init__(*args, **kwargs)
self.created_by = user
def create_host(self, user):
self.clean()
host = Host(fqdn=self.cleaned_data['fqdn'],
comment=self.cleaned_data['comment'])
comment=self.cleaned_data['comment'],
created_by=user)
host.save()
# TODO: Update NS with self.cleaned_data['ipv4addr']
return host

View File

@ -1,17 +1,37 @@
{% extends "base.html" %}
{% load bootstrap %}
{% block content %}
<h1>Overview</h1>
<div class="row">
<div class="col-lg-8">
<h1>Host List</h1>
<table class="table">
<tr><th>domain</th><th>last update</th><th>comment</th></tr>
{% for host in Hosts %}
<tr><td><b>{{ host.fqdn }}</b>.nsupdate.info</td> <td>{{ host.last_update|date }}</td> <td>{{ host.comment }}</td></tr>
{% empty %}
<tr>No hosts yet.</tr>
{% endfor %}
</table>
IPv4: <input type='text' class="form-control ipv4adr" placeholder="can't find IP" value="{{session.ipv4}}"> </br >
IPv6: <input type='text' class="form-control ipv6adr" placeholder="can't find IP" value="{{session.ipv6}}">
<div style="display: none">
<img src="//{{ WWW_IPV4_HOST }}/updateip" />
<img src="//{{ WWW_IPV6_HOST }}/updateip" />
</div>
{{ HostForm }}
<div style="display: none">
<img src="//{{ WWW_IPV4_HOST }}/updateip" />
<img src="//{{ WWW_IPV6_HOST }}/updateip" />
</div>
</div>
<h1>New host</h1>
<div class="col-lg-4">
<form method="post" action="">
{% csrf_token %}
{{ HostForm|bootstrap }}
<label>IPv4</label>
<input type='text' name='ipv4addr' class="form-control ipv4adr" placeholder="can't find IP" value="{{session.ipv4}}"> </br >
<label>IPv6</label>
<input type='text' name='ipv6addr' class="form-control ipv6adr" placeholder="can't find IP" value="{{session.ipv6}}"> </br>
<button type="submit" class="btn btn-primary">Create</button>
</form>
</div>
{% endblock %}

View File

@ -1,6 +1,6 @@
from django.conf.urls import patterns, include, url
from main.views import (
HomeView, OverviewView
HomeView, OverviewView,
)
from api.views import (
MyIpView, UpdateIpView
@ -8,7 +8,7 @@ from api.views import (
urlpatterns = patterns('',
url(r'^$', HomeView.as_view(), name="home"),
url(r'^overview/', OverviewView.as_view(), name="overview"),
url(r'^overview/$', OverviewView, name='overview'),
url(r'^myip$', MyIpView),
url(r'^updateip$', UpdateIpView),
)

View File

@ -1,7 +1,9 @@
# -*- coding: utf-8 -*-
from django.views.generic import TemplateView
from django.views.generic.list import ListView
from django.http import HttpResponse
from django.conf import settings
from django.shortcuts import render
from main.forms import *
class HomeView(TemplateView):
@ -12,20 +14,22 @@ class HomeView(TemplateView):
context['nav_home'] = True
return context
def OverviewView(request):
context = {}
context['nav_overview'] = True
context['WWW_IPV4_HOST'] = settings.WWW_IPV4_HOST
context['WWW_IPV6_HOST'] = settings.WWW_IPV6_HOST
context['session'] = request.session
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 = form.create_host(request.user)
host.save()
class OverviewView(TemplateView):
template_name = "main/overview.html"
def get_context_data(self, *args, **kwargs):
context = super(OverviewView, self).get_context_data(*args, **kwargs)
context['nav_overview'] = True
context['WWW_IPV4_HOST'] = settings.WWW_IPV4_HOST
context['WWW_IPV6_HOST'] = settings.WWW_IPV6_HOST
context['session'] = self.request.session
context['HostForm'] = HostForm(self.request.POST)
if self.request.method == "POST":
form = HostForm(self.request.POST)
if form.is_valid():
host = form.create_host()
host.save()
return context
context['HostForm'] = form
return render(request, "main/overview.html", context)