diff --git a/nsupdate/main/forms.py b/nsupdate/main/forms.py index 256ea52..3b9032c 100644 --- a/nsupdate/main/forms.py +++ b/nsupdate/main/forms.py @@ -1,32 +1,9 @@ # -*- coding: utf-8 -*- - from django import forms -from django.contrib.auth.models import User -from main.models import * +from main.models import Host class HostForm(forms.ModelForm): class Meta: 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 - diff --git a/nsupdate/main/templates/main/overview.html b/nsupdate/main/templates/main/overview.html index e5dc9e5..0ce4882 100644 --- a/nsupdate/main/templates/main/overview.html +++ b/nsupdate/main/templates/main/overview.html @@ -8,12 +8,12 @@

Host List

- {% for host in Hosts %} + {% for host in hosts %} - {% empty %} No hosts yet. @@ -24,7 +24,7 @@
{% csrf_token %} - {{ HostForm|bootstrap }} + {{ form|bootstrap }}
@@ -39,4 +39,4 @@ -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/nsupdate/main/urls.py b/nsupdate/main/urls.py index f74f917..3d13398 100644 --- a/nsupdate/main/urls.py +++ b/nsupdate/main/urls.py @@ -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\w+)$', HostView, name='host-view'), + url(r'^overview/$', OverviewView.as_view(), name='overview'), + url(r'^host/(?P\d+)/$', HostView, name='host_view'), url(r'^myip$', MyIpView), url(r'^updateip$', UpdateIpView), url(r'^nic/update$', NicUpdateView), diff --git a/nsupdate/main/views.py b/nsupdate/main/views.py index 5ca3917..0b50a7f 100644 --- a/nsupdate/main/views.py +++ b/nsupdate/main/views.py @@ -1,11 +1,16 @@ # -*- 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 main.forms import * +from django.contrib import messages +from django.utils.decorators import method_decorator +from django.core.urlresolvers import reverse + +from main.forms import HostForm +from main.models import Host def create_context(request): context = {} @@ -24,23 +29,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 + + @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() + 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 - context['HostForm'] = form - return render(request, "main/overview.html", context) @login_required def HostView(request,pk=None): @@ -55,4 +69,5 @@ def HostView(request,pk=None): print "valid" host = form.save(request.user) context['HostForm'] = form - return render(request, "main/host.html", context) \ No newline at end of file + return render(request, "main/host.html", context) + diff --git a/nsupdate/nsupdate/templates/base.html b/nsupdate/nsupdate/templates/base.html index cc9a7fc..45836cf 100644 --- a/nsupdate/nsupdate/templates/base.html +++ b/nsupdate/nsupdate/templates/base.html @@ -50,8 +50,17 @@ -
+
+ {% if messages %} + {% for message in messages %} +
+ + {{ message }} +
+ {% endfor %} + {% endif %} +
{% block content %}
domainlast updatecommentaction
{{ host.fqdn }} {{ host.last_update|date }} {{ host.comment }} +