From dd614d193670b1c82e95cdd968965bfebfdc9a98 Mon Sep 17 00:00:00 2001 From: Fabian Faessler Date: Sat, 28 Sep 2013 23:45:46 +0200 Subject: [PATCH] changed the host edit into a classbased view. added host delete --- nsupdate/main/templates/main/delete_host.html | 19 +++++++ nsupdate/main/templates/main/host.html | 6 +- nsupdate/main/templates/main/overview.html | 5 +- nsupdate/main/urls.py | 5 +- nsupdate/main/views.py | 55 ++++++++++++++----- 5 files changed, 71 insertions(+), 19 deletions(-) create mode 100644 nsupdate/main/templates/main/delete_host.html diff --git a/nsupdate/main/templates/main/delete_host.html b/nsupdate/main/templates/main/delete_host.html new file mode 100644 index 0000000..fb2169b --- /dev/null +++ b/nsupdate/main/templates/main/delete_host.html @@ -0,0 +1,19 @@ +{% extends "base.html" %} +{% load bootstrap %} + +{% block content %} + +
+ +
+

Delete Host back to overview

+
+ {% csrf_token %} +

Are you sure you want to delete "{{ object }}"?

+ +
+
+
+ + +{% endblock %} \ No newline at end of file diff --git a/nsupdate/main/templates/main/host.html b/nsupdate/main/templates/main/host.html index 0800fba..c4d8bee 100644 --- a/nsupdate/main/templates/main/host.html +++ b/nsupdate/main/templates/main/host.html @@ -4,14 +4,16 @@ {% block content %}
-

Edit Host

+
+

Edit Host back to overview

{% csrf_token %} - {{ HostForm|bootstrap }} + {{ form|bootstrap }}
+
{% endblock %} \ No newline at end of file diff --git a/nsupdate/main/templates/main/overview.html b/nsupdate/main/templates/main/overview.html index 0ce4882..7f3b79c 100644 --- a/nsupdate/main/templates/main/overview.html +++ b/nsupdate/main/templates/main/overview.html @@ -13,7 +13,10 @@ {{ host.fqdn }} {{ host.last_update|date }} {{ host.comment }} - + + edit | + delete + {% empty %} No hosts yet. diff --git a/nsupdate/main/urls.py b/nsupdate/main/urls.py index 3d13398..f8a7e0a 100644 --- a/nsupdate/main/urls.py +++ b/nsupdate/main/urls.py @@ -1,6 +1,6 @@ from django.conf.urls import patterns, include, url from main.views import ( - HomeView, OverviewView, HostView, + HomeView, OverviewView, HostView, DeleteHostView, ) from api.views import ( MyIpView, UpdateIpView, NicUpdateView @@ -9,7 +9,8 @@ from api.views import ( urlpatterns = patterns('', url(r'^$', HomeView.as_view(), name="home"), url(r'^overview/$', OverviewView.as_view(), name='overview'), - url(r'^host/(?P\d+)/$', HostView, name='host_view'), + url(r'^host/(?P\d+)/$', HostView.as_view(), name='host'), + url(r'^delete_host/(?P\d+)/$', DeleteHostView.as_view(), name='delete_host'), 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 0b50a7f..773d33e 100644 --- a/nsupdate/main/views.py +++ b/nsupdate/main/views.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- from django.views.generic import TemplateView, CreateView from django.views.generic.list import ListView +from django.views.generic.edit import UpdateView, DeleteView from django.http import HttpResponse, HttpResponseRedirect from django.conf import settings from django.shortcuts import render, get_object_or_404 @@ -55,19 +56,45 @@ class OverviewView(CreateView): context['hosts'] = Host.objects.filter(created_by=self.request.user) return context +class HostView(UpdateView): + model = Host + template_name = "main/host.html" + form_class = HostForm + @method_decorator(login_required) + def dispatch(self, *args, **kwargs): + return super(HostView, 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 updated.') + return HttpResponseRedirect(self.get_success_url()) + + def get_context_data(self, *args, **kwargs): + context = super(HostView, self).get_context_data(*args, **kwargs) + context['nav_overview'] = True + context['hosts'] = Host.objects.filter(created_by=self.request.user) + return context + +class DeleteHostView(DeleteView): + model = Host + template_name = "main/delete_host.html" + form_class = HostForm + @method_decorator(login_required) + def dispatch(self, *args, **kwargs): + return super(DeleteHostView, self).dispatch(*args, **kwargs) + + def get_success_url(self): + return reverse('overview') + + def get_context_data(self, *args, **kwargs): + context = super(DeleteHostView, self).get_context_data(*args, **kwargs) + context['nav_overview'] = True + context['hosts'] = Host.objects.filter(created_by=self.request.user) + return context -@login_required -def HostView(request,pk=None): - context = create_context(request) - 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)