From f43c71960f0b781ea764f9d70c1d16837872d911 Mon Sep 17 00:00:00 2001 From: Arne Schauf Date: Sat, 28 Sep 2013 22:20:19 +0200 Subject: [PATCH 1/4] better imports --- nsupdate/main/forms.py | 2 +- nsupdate/main/views.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/nsupdate/main/forms.py b/nsupdate/main/forms.py index 5fa95ea..ab602f8 100644 --- a/nsupdate/main/forms.py +++ b/nsupdate/main/forms.py @@ -2,7 +2,7 @@ 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: diff --git a/nsupdate/main/views.py b/nsupdate/main/views.py index d270986..2ca7884 100644 --- a/nsupdate/main/views.py +++ b/nsupdate/main/views.py @@ -5,7 +5,8 @@ from django.http import HttpResponse from django.conf import settings from django.shortcuts import render from django.contrib.auth.decorators import login_required -from main.forms import * +from main.forms import HostForm +from main.models import Host class HomeView(TemplateView): template_name = "base.html" @@ -34,4 +35,4 @@ def OverviewView(request): host.save() context['HostForm'] = form - return render(request, "main/overview.html", context) \ No newline at end of file + return render(request, "main/overview.html", context) From 3af1d100145d5b5f60e140856463b2cae5f120aa Mon Sep 17 00:00:00 2001 From: Arne Schauf Date: Sat, 28 Sep 2013 22:22:42 +0200 Subject: [PATCH 2/4] oups - pushed broken things --- nsupdate/main/views.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/nsupdate/main/views.py b/nsupdate/main/views.py index ea13a33..8aaeb2b 100644 --- a/nsupdate/main/views.py +++ b/nsupdate/main/views.py @@ -41,8 +41,7 @@ def OverviewView(request): context['HostForm'] = form return render(request, "main/overview.html", context) -<<<<<<< HEAD -======= + @login_required def HostView(request,pk=None): @@ -58,4 +57,4 @@ def HostView(request,pk=None): host = form.save(request.user) context['HostForm'] = form return render(request, "main/host.html", context) ->>>>>>> 3b2fa9008fe6aa3d20e7b3ae9504fadedb4165c4 + From eb8f548f957ee07f87a4f1c0f173968cdbfbfb8a Mon Sep 17 00:00:00 2001 From: Arne Schauf Date: Sat, 28 Sep 2013 22:46:53 +0200 Subject: [PATCH 3/4] overview as class based view --- nsupdate/main/forms.py | 23 ----------- nsupdate/main/templates/main/overview.html | 8 ++-- nsupdate/main/urls.py | 4 +- nsupdate/main/views.py | 46 ++++++++++++++-------- 4 files changed, 35 insertions(+), 46 deletions(-) diff --git a/nsupdate/main/forms.py b/nsupdate/main/forms.py index baf274d..3b9032c 100644 --- a/nsupdate/main/forms.py +++ b/nsupdate/main/forms.py @@ -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 - 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 8aaeb2b..87745e0 100644 --- a/nsupdate/main/views.py +++ b/nsupdate/main/views.py @@ -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 From 202de726c1c4e529a8f0940a640b12b0385fd1ca Mon Sep 17 00:00:00 2001 From: Arne Schauf Date: Sat, 28 Sep 2013 23:03:34 +0200 Subject: [PATCH 4/4] messages --- nsupdate/main/views.py | 4 ++-- nsupdate/nsupdate/templates/base.html | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/nsupdate/main/views.py b/nsupdate/main/views.py index 87745e0..62afcb9 100644 --- a/nsupdate/main/views.py +++ b/nsupdate/main/views.py @@ -5,6 +5,7 @@ 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.contrib import messages from django.utils.decorators import method_decorator from django.core.urlresolvers import reverse @@ -44,8 +45,7 @@ class OverviewView(CreateView): 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.')) + messages.add_message(self.request, messages.SUCCESS, 'Host added.') return HttpResponseRedirect(self.get_success_url()) def get_context_data(self, *args, **kwargs): diff --git a/nsupdate/nsupdate/templates/base.html b/nsupdate/nsupdate/templates/base.html index 3a06a02..ae0e5b3 100644 --- a/nsupdate/nsupdate/templates/base.html +++ b/nsupdate/nsupdate/templates/base.html @@ -50,6 +50,16 @@
+
+ {% if messages %} + {% for message in messages %} +
+ + {{ message }} +
+ {% endfor %} + {% endif %} +
{% block content %}

Heyho!

Welcome to the base template. Extend this template in all other

domainlast updatecommentaction
{{ host.fqdn }} {{ host.last_update|date }} {{ host.comment }} +