Commit 3b0c7f0a by Kálmán Viktor

dashboard: generic delete view for vm deletion

parent eb728978
{% extends "dashboard/base.html" %}
{% block content %}
<div class="body-content">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="no-margin">
Delete confirmation
<div class="panel-body">
Are you sure you want to delete <strong>{{ object }}</strong>?
<div class="pull-right">
<form action="" method="POST">
{% csrf_token %}
<a class="btn btn-default">Back</a>
<input type="hidden" name="next" value="{{ }}"/>
<button class="btn btn-danger">Yes, delete</button>
{% endblock %}
......@@ -3,7 +3,7 @@ from django.conf.urls import patterns, url
from vm.models import Instance
from .views import (
IndexView, VmDetailView, VmList, VmCreate, TemplateDetail, AclUpdateView,
delete_vm, mass_delete_vm)
VmDelete, mass_delete_vm)
urlpatterns = patterns(
......@@ -17,7 +17,7 @@ urlpatterns = patterns(
url(r'^vm/list/$', VmList.as_view(), name='dashboard.views.vm-list'),
url(r'^vm/create/$', VmCreate.as_view(),
url(r'^vm/delete/(?P<pk>\d+)/$', delete_vm,
url(r'^vm/delete/(?P<pk>\d+)/$', VmDelete.as_view(),
url(r'^vm/mass-delete/', mass_delete_vm,
......@@ -8,11 +8,11 @@ from django.contrib.messages import warning
from django.core.exceptions import PermissionDenied
from django.core import signing
from django.core.urlresolvers import reverse, reverse_lazy
from django.http import HttpResponse
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import redirect
from django.views.decorators.http import require_POST
from django.views.generic.detail import SingleObjectMixin
from django.views.generic import TemplateView, DetailView, View
from django.views.generic import TemplateView, DetailView, View, DeleteView
from django.contrib import messages
from django.utils.translation import ugettext as _
......@@ -239,16 +239,26 @@ class VmCreate(TemplateView):
return redirect(reverse_lazy('dashboard.views.detail', resp))
def delete_vm(request, **kwargs):
vm_pk = kwargs['pk']
class VmDelete(DeleteView):
model = Instance
template_name = "dashboard/confirm/base_delete.html"
def get_context_data(self, **kwargs):
# this is redundant now, but if we wanna add more to print
# we'll need this
context = super(VmDelete, self).get_context_data(**kwargs)
return context
vm = Instance.objects.get(pk=vm_pk)
if not vm.has_level(request.user, 'owner'):
def delete(self, request, *args, **kwargs):
object = self.get_object()
if not object.has_level(request.user, 'owner'):
raise PermissionDenied()
success_url = self.get_success_url()
success_message = _("VM successfully deleted!")
if request.is_ajax():
return HttpResponse(
json.dumps({'message': success_message}),
......@@ -256,8 +266,14 @@ def delete_vm(request, **kwargs):
messages.success(request, success_message)
next = request.GET.get('next')
return redirect(next if next else reverse_lazy('dashboard.index'))
return HttpResponseRedirect(success_url)
def get_success_url(self):
next = self.request.POST.get('next')
if next:
return next
return reverse_lazy('dashboard.index')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment