changed the host edit into a classbased view. added host delete

This commit is contained in:
Fabian Faessler 2013-09-28 23:45:46 +02:00
parent 155b1966d5
commit dd614d1936
5 changed files with 71 additions and 19 deletions

View File

@ -0,0 +1,19 @@
{% extends "base.html" %}
{% load bootstrap %}
{% block content %}
<div class="row">
<div class="col-lg-4">
<h3>Delete Host <small><a href="{% url 'overview' %}"><i class="icon-double-angle-left"></i> back to overview</a></small></h3>
<form action="" method="post">
{% csrf_token %}
<p>Are you sure you want to delete "{{ object }}"?</p>
<button type="submit" class="btn btn-primary">Confirm</button>
</form>
</div>
</div>
{% endblock %}

View File

@ -4,14 +4,16 @@
{% block content %} {% block content %}
<div class="row"> <div class="row">
<h3>Edit Host</h3>
<div class="col-lg-4"> <div class="col-lg-4">
<h3>Edit Host <small><a href="{% url 'overview' %}"><i class="icon-double-angle-left"></i> back to overview</a></small></h3>
<form method="post" action=""> <form method="post" action="">
{% csrf_token %} {% csrf_token %}
{{ HostForm|bootstrap }} {{ form|bootstrap }}
<button type="submit" class="btn btn-primary">Update</button> <button type="submit" class="btn btn-primary">Update</button>
</form> </form>
</div> </div>
</div>
{% endblock %} {% endblock %}

View File

@ -13,7 +13,10 @@
<td><b>{{ host.fqdn }}</b></td> <td><b>{{ host.fqdn }}</b></td>
<td>{{ host.last_update|date }}</td> <td>{{ host.last_update|date }}</td>
<td>{{ host.comment }}</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' host.pk %}"><i class="icon icon-pencil"></i> edit</a> |
<a href="{% url 'delete_host' host.pk %}"><i class="icon icon-remove"></i> delete</a>
</td>
</tr> </tr>
{% empty %} {% empty %}
<tr>No hosts yet.</tr> <tr>No hosts yet.</tr>

View File

@ -1,6 +1,6 @@
from django.conf.urls import patterns, include, url from django.conf.urls import patterns, include, url
from main.views import ( from main.views import (
HomeView, OverviewView, HostView, HomeView, OverviewView, HostView, DeleteHostView,
) )
from api.views import ( from api.views import (
MyIpView, UpdateIpView, NicUpdateView MyIpView, UpdateIpView, NicUpdateView
@ -9,7 +9,8 @@ from api.views import (
urlpatterns = patterns('', urlpatterns = patterns('',
url(r'^$', HomeView.as_view(), name="home"), url(r'^$', HomeView.as_view(), name="home"),
url(r'^overview/$', OverviewView.as_view(), name='overview'), url(r'^overview/$', OverviewView.as_view(), name='overview'),
url(r'^host/(?P<pk>\d+)/$', HostView, name='host_view'), url(r'^host/(?P<pk>\d+)/$', HostView.as_view(), name='host'),
url(r'^delete_host/(?P<pk>\d+)/$', DeleteHostView.as_view(), name='delete_host'),
url(r'^myip$', MyIpView), url(r'^myip$', MyIpView),
url(r'^updateip$', UpdateIpView), url(r'^updateip$', UpdateIpView),
url(r'^nic/update$', NicUpdateView), url(r'^nic/update$', NicUpdateView),

View File

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from django.views.generic import TemplateView, CreateView from django.views.generic import TemplateView, CreateView
from django.views.generic.list import ListView from django.views.generic.list import ListView
from django.views.generic.edit import UpdateView, DeleteView
from django.http import HttpResponse, HttpResponseRedirect from django.http import HttpResponse, HttpResponseRedirect
from django.conf import settings from django.conf import settings
from django.shortcuts import render, get_object_or_404 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) context['hosts'] = Host.objects.filter(created_by=self.request.user)
return context 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)