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
</h3>
</div>
<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="{{ request.GET.next }}"/>
<button class="btn btn-danger">Yes, delete</button>
</form>
</div>
</div>
</div>
{% endblock %}
...@@ -3,7 +3,7 @@ from django.conf.urls import patterns, url ...@@ -3,7 +3,7 @@ from django.conf.urls import patterns, url
from vm.models import Instance from vm.models import Instance
from .views import ( from .views import (
IndexView, VmDetailView, VmList, VmCreate, TemplateDetail, AclUpdateView, IndexView, VmDetailView, VmList, VmCreate, TemplateDetail, AclUpdateView,
delete_vm, mass_delete_vm) VmDelete, mass_delete_vm)
urlpatterns = patterns( urlpatterns = patterns(
'', '',
...@@ -17,7 +17,7 @@ urlpatterns = patterns( ...@@ -17,7 +17,7 @@ urlpatterns = patterns(
url(r'^vm/list/$', VmList.as_view(), name='dashboard.views.vm-list'), url(r'^vm/list/$', VmList.as_view(), name='dashboard.views.vm-list'),
url(r'^vm/create/$', VmCreate.as_view(), url(r'^vm/create/$', VmCreate.as_view(),
name='dashboard.views.vm-create'), name='dashboard.views.vm-create'),
url(r'^vm/delete/(?P<pk>\d+)/$', delete_vm, url(r'^vm/delete/(?P<pk>\d+)/$', VmDelete.as_view(),
name="dashboard.views.delete-vm"), name="dashboard.views.delete-vm"),
url(r'^vm/mass-delete/', mass_delete_vm, url(r'^vm/mass-delete/', mass_delete_vm,
name='dashboard.view.mass-delete-vm') name='dashboard.view.mass-delete-vm')
......
...@@ -8,11 +8,11 @@ from django.contrib.messages import warning ...@@ -8,11 +8,11 @@ from django.contrib.messages import warning
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
from django.core import signing from django.core import signing
from django.core.urlresolvers import reverse, reverse_lazy 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.shortcuts import redirect
from django.views.decorators.http import require_POST from django.views.decorators.http import require_POST
from django.views.generic.detail import SingleObjectMixin 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.contrib import messages
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
...@@ -239,16 +239,26 @@ class VmCreate(TemplateView): ...@@ -239,16 +239,26 @@ class VmCreate(TemplateView):
return redirect(reverse_lazy('dashboard.views.detail', resp)) return redirect(reverse_lazy('dashboard.views.detail', resp))
@require_POST class VmDelete(DeleteView):
def delete_vm(request, **kwargs): model = Instance
vm_pk = kwargs['pk'] 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) # github.com/django/django/blob/master/django/views/generic/edit.py#L245
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() raise PermissionDenied()
vm.destroy_async()
object.destroy_async()
success_url = self.get_success_url()
success_message = _("VM successfully deleted!") success_message = _("VM successfully deleted!")
if request.is_ajax(): if request.is_ajax():
return HttpResponse( return HttpResponse(
json.dumps({'message': success_message}), json.dumps({'message': success_message}),
...@@ -256,8 +266,14 @@ def delete_vm(request, **kwargs): ...@@ -256,8 +266,14 @@ def delete_vm(request, **kwargs):
) )
else: else:
messages.success(request, success_message) messages.success(request, success_message)
next = request.GET.get('next') return HttpResponseRedirect(success_url)
return redirect(next if next else reverse_lazy('dashboard.index'))
def get_success_url(self):
next = self.request.POST.get('next')
if next:
return next
else:
return reverse_lazy('dashboard.index')
@require_POST @require_POST
......
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