Commit 65fb81e6 by Kálmán Viktor

dashboard: delete templates and leases

parent 67bf3744
$(function() {
/* for template removes buttons */
$('.template-delete').click(function() {
var template_pk = $(this).data('template-pk');
addModalConfirmation(deleteTemplate,
{ 'url': '/dashboard/template/delete/' + template_pk + '/',
'data': [],
'template_pk': template_pk,
});
return false;
});
/* for lease removes buttons */
$('.lease-delete').click(function() {
var lease_pk = $(this).data('lease-pk');
addModalConfirmation(deleteLease,
{ 'url': '/dashboard/lease/delete/' + lease_pk + '/',
'data': [],
'lease_pk': lease_pk,
});
return false;
});
});
// send POST request then delete the row in table
function deleteTemplate(data) {
$.ajax({
type: 'POST',
url: data['url'],
headers: {"X-CSRFToken": getCookie('csrftoken')},
success: function(re, textStatus, xhr) {
addMessage(re['message'], 'success');
$('a[data-template-pk="' + data['template_pk'] + '"]').closest('tr').fadeOut(function() {
$(this).remove();
});
},
error: function(xhr, textStatus, error) {
addMessage('Uh oh :(', 'danger')
}
});
}
// send POST request then delete the row in table
function deleteLease(data) {
$.ajax({
type: 'POST',
url: data['url'],
headers: {"X-CSRFToken": getCookie('csrftoken')},
success: function(re, textStatus, xhr) {
addMessage(re['message'], 'success');
$('a[data-lease-pk="' + data['lease_pk'] + '"]').closest('tr').fadeOut(function() {
$(this).remove();
});
},
error: function(xhr, textStatus, error) {
addMessage('Uh oh :(', 'danger')
}
});
}
...@@ -67,3 +67,7 @@ ...@@ -67,3 +67,7 @@
} }
</style> </style>
{% endblock %} {% endblock %}
{% block extra_js %}
<script src="{{ STATIC_URL}}dashboard/template-list.js"></script>
{% endblock %}
...@@ -2,6 +2,6 @@ ...@@ -2,6 +2,6 @@
<a href="{% url "dashboard.views.lease-detail" pk=record.pk %}" id="template-list-edit-button" class="btn btn-default btn-xs" title data-original-title="{% trans "Edit" %}"> <a href="{% url "dashboard.views.lease-detail" pk=record.pk %}" id="template-list-edit-button" class="btn btn-default btn-xs" title data-original-title="{% trans "Edit" %}">
<i class="icon-edit"></i> <i class="icon-edit"></i>
</a> </a>
<a href="#" class="btn btn-danger btn-xs" title data-original-title="{% trans "Delete" %}"> <a data-lease-pk="{{ record.pk }}" href="{% url "dashboard.views.lease-delete" pk=record.pk %}" class="btn btn-danger btn-xs lease-delete" title="{% trans "Delete" %}">
<i class="icon-remove"></i> <i class="icon-remove"></i>
</a> </a>
...@@ -5,6 +5,6 @@ ...@@ -5,6 +5,6 @@
<a href="{% url "dashboard.views.template-detail" pk=record.pk%}" id="template-list-edit-button" class="btn btn-default btn-xs" title data-original-title="{% trans "Edit" %}"> <a href="{% url "dashboard.views.template-detail" pk=record.pk%}" id="template-list-edit-button" class="btn btn-default btn-xs" title data-original-title="{% trans "Edit" %}">
<i class="icon-edit"></i> <i class="icon-edit"></i>
</a> </a>
<a href="#" class="btn btn-danger btn-xs" title data-original-title="{% trans "Delete" %}"> <a data-template-pk="{{ record.pk }}" href="{% url "dashboard.views.template-delete" pk=record.pk %}" class="btn btn-danger btn-xs template-delete" title="{% trans "Delete" %}">
<i class="icon-remove"></i> <i class="icon-remove"></i>
</a> </a>
...@@ -6,7 +6,7 @@ from .views import ( ...@@ -6,7 +6,7 @@ from .views import (
VmDelete, VmMassDelete, vm_activity, NodeList, NodeDetailView, PortDelete, VmDelete, VmMassDelete, vm_activity, NodeList, NodeDetailView, PortDelete,
TransferOwnershipView, TransferOwnershipConfirmView, NodeDelete, TransferOwnershipView, TransferOwnershipConfirmView, NodeDelete,
TemplateList, LeaseDetail, NodeCreate, LeaseCreate, TemplateCreate, TemplateList, LeaseDetail, NodeCreate, LeaseCreate, TemplateCreate,
FavouriteView, NodeStatus, GroupList, FavouriteView, NodeStatus, GroupList, TemplateDelete, LeaseDelete,
) )
urlpatterns = patterns( urlpatterns = patterns(
...@@ -16,12 +16,16 @@ urlpatterns = patterns( ...@@ -16,12 +16,16 @@ urlpatterns = patterns(
name="dashboard.views.lease-detail"), name="dashboard.views.lease-detail"),
url(r'^lease/create/$', LeaseCreate.as_view(), url(r'^lease/create/$', LeaseCreate.as_view(),
name="dashboard.views.lease-create"), name="dashboard.views.lease-create"),
url(r'^lease/delete/(?P<pk>\d+)/$', LeaseDelete.as_view(),
name="dashboard.views.lease-delete"),
url(r'^template/create/$', TemplateCreate.as_view(), url(r'^template/create/$', TemplateCreate.as_view(),
name="dashboard.views.template-create"), name="dashboard.views.template-create"),
url(r'^template/(?P<pk>\d+)/$', TemplateDetail.as_view(), url(r'^template/(?P<pk>\d+)/$', TemplateDetail.as_view(),
name='dashboard.views.template-detail'), name='dashboard.views.template-detail'),
url(r"^template/list/$", TemplateList.as_view(), url(r"^template/list/$", TemplateList.as_view(),
name="dashboard.views.template-list"), name="dashboard.views.template-list"),
url(r"^template/delete/(?P<pk>\d+)/$", TemplateDelete.as_view(),
name="dashboard.views.template-delete"),
url(r'^vm/(?P<pk>\d+)/remove_port/(?P<rule>\d+)/$', PortDelete.as_view(), url(r'^vm/(?P<pk>\d+)/remove_port/(?P<rule>\d+)/$', PortDelete.as_view(),
name='dashboard.views.remove-port'), name='dashboard.views.remove-port'),
url(r'^vm/(?P<pk>\d+)/$', VmDetailView.as_view(), url(r'^vm/(?P<pk>\d+)/$', VmDetailView.as_view(),
......
...@@ -534,6 +534,37 @@ class TemplateList(LoginRequiredMixin, SingleTableView): ...@@ -534,6 +534,37 @@ class TemplateList(LoginRequiredMixin, SingleTableView):
'user', self.request.user).all() 'user', self.request.user).all()
class TemplateDelete(LoginRequiredMixin, DeleteView):
model = InstanceTemplate
def get_success_url(self):
return reverse("dashboard.views.template-list")
def get_template_names(self):
if self.request.is_ajax():
return ['dashboard/confirm/ajax-delete.html']
else:
return ['dashboard/confirm/base-delete.html']
def delete(self, request, *args, **kwargs):
object = self.get_object()
if not object.has_level(request.user, 'owner'):
raise PermissionDenied()
object.delete()
success_url = self.get_success_url()
success_message = _("Template successfully deleted!")
if request.is_ajax():
return HttpResponse(
json.dumps({'message': success_message}),
content_type="application/json",
)
else:
messages.success(request, success_message)
return HttpResponseRedirect(success_url)
class VmList(LoginRequiredMixin, SingleTableView): class VmList(LoginRequiredMixin, SingleTableView):
template_name = "dashboard/vm-list.html" template_name = "dashboard/vm-list.html"
table_class = VmListTable table_class = VmListTable
...@@ -958,6 +989,35 @@ class LeaseDetail(LoginRequiredMixin, SuccessMessageMixin, UpdateView): ...@@ -958,6 +989,35 @@ class LeaseDetail(LoginRequiredMixin, SuccessMessageMixin, UpdateView):
return reverse_lazy("dashboard.views.lease-detail", kwargs=self.kwargs) return reverse_lazy("dashboard.views.lease-detail", kwargs=self.kwargs)
class LeaseDelete(LoginRequiredMixin, SuperuserRequiredMixin, DeleteView):
model = Lease
def get_success_url(self):
return reverse("dashboard.views.template-list")
def get_template_names(self):
if self.request.is_ajax():
return ['dashboard/confirm/ajax-delete.html']
else:
return ['dashboard/confirm/base-delete.html']
def delete(self, request, *args, **kwargs):
object = self.get_object()
object.delete()
success_url = self.get_success_url()
success_message = _("Lease successfully deleted!")
if request.is_ajax():
return HttpResponse(
json.dumps({'message': success_message}),
content_type="application/json",
)
else:
messages.success(request, success_message)
return HttpResponseRedirect(success_url)
@require_POST @require_POST
def vm_activity(request, pk): def vm_activity(request, pk):
object = Instance.objects.get(pk=pk) object = Instance.objects.get(pk=pk)
......
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