Commit 0d345297 by tarokkk

webui: Added template delete support

parent 1981fc01
......@@ -33,6 +33,7 @@ urlpatterns = patterns('',
url(r'^store/gui/$', 'store.views.gui', name='store_gui'),
url(r'^store/top/$', 'store.views.toplist', name='store_top'),
url(r'^ajax/templateWizard$', 'one.views.ajax_template_wizard', name='ajax_template_wizard'),
url(r'^ajax/template/delete/$', 'one.views.ajax_template_delete', name='ajax_template_delete'),
url(r'^ajax/template_name_unique/(?P<name>.*)$', 'one.views.ajax_template_name_unique', name='ajax_template_name_unique'),
url(r'^ajax/store/list$', 'store.views.ajax_listfolder', name='store_ajax_listfolder'),
url(r'^ajax/store/download$', 'store.views.ajax_download', name='store_ajax_download'),
......
......@@ -14,9 +14,12 @@ from school.models import Person, Group
from datetime import timedelta as td
from django.db.models.signals import post_delete, pre_delete
from store.api import StoreApi
from django.db import transaction
import logging
import subprocess, tempfile, os, stat, re, base64, struct
logger = logging.getLogger(__name__)
pwgen = User.objects.make_random_password
"""
......@@ -289,6 +292,15 @@ class Template(models.Model):
def __unicode__(self):
return self.name
@transaction.commit_on_success
def safe_delete(self):
if not self.instance_set.exists():
self.delete()
return True
else:
logger.info("Could not delete template. Instances still running!")
return False
class Meta:
verbose_name = _('template')
verbose_name_plural = _('templates')
......
......@@ -36,6 +36,9 @@ $(function() {
$(this).next('.details').slideDown(700);
}
}
$('.delete-template-button').click(function() {
delete_template_confirm($(this).data('id'), $(this).data('name'));
});
$('.wm .summary').unbind('click').click(toggleDetails);
$('.stop-vm-button').click(function() {
stop_vm($(this).data('id'), $(this).data('name'));
......@@ -172,6 +175,37 @@ $(function() {
}
})
}
/**
* Template delete
*/
function delete_template_confirm(id, name) {
confirm_message = interpolate(gettext("Are you sure deleting this %s template?"), [name])
vm_confirm_popup(confirm_message, gettext("Delete"), function() {
delete_template(id)
})
}
/**
* Template delete
*/
function delete_template(id) {
$.ajax({
type: 'POST',
url: '/ajax/template/delete/',
data: 'id=' + id,
dataType: 'json',
statusCode: {
404: function(data) {
alert(data['responseText']);
},
200: function(data) {
$("#t"+id).remove()
},
}
})
}
$('#new-member').click(function() {
$('#new-member-form').toggle();
});
......
......@@ -4,13 +4,13 @@
<h2>{% trans "My templates" %}</h2>
<ul class="wm-list">
{% for t in mytemplates %}
<li class="wm">
<li class="wm" id="t{{t.id }}">
<div class="summary">
<div class="name">{{t.name}}</div>
<div class="status">{{t.state}}</div>
<div class="actions">
<a href="#" title="{% trans "Edit" %}"><img src="/static/icons/pencil.png" alt="{% trans "Edit" %}" /></a>
<a href="#" title="{% trans "Remove" %}"><img src="/static/icons/minus-circle.png" alt="{% trans "Remove" %}" /></a>
<a href="#" class="delete-template-button" data-id="{{ t.id }}" data-name="{{ t.name }}" title="{% trans "Remove" %}"><img src="/static/icons/minus-circle.png" alt="{% trans "Remove" %}" /></a>
<a href="#" title="{% trans "Share" %}"><img src="static/icons/user-share.png" alt="{% trans "Share" %}" /></a>
</div>
<div class="clear"></div>
......
......@@ -81,6 +81,21 @@ def home(request):
'userdetails': UserCloudDetails.objects.get(user=request.user),
}))
@login_required
def ajax_template_delete(request):
try:
template_id = request.POST['id']
except:
return HttpResponse(unicode(_("Invalid template ID.")), status=404)
template = get_object_or_404(Template, id=template_id)
if template.instance_set.exists():
return HttpResponse(unicode(_("There are running instances of this template.")), status=404)
elif template.share_set.exists():
return HttpResponse(unicode(_("Template is still shared.")), status=404)
else:
template.safe_delete()
return HttpResponse(unicode(_("Template succesfully deleted.")))
def ajax_template_name_unique(request, name):
s = "True"
if Template.objects.filter(name=name).exists():
......
......@@ -20,7 +20,7 @@ def estabilish_store_user(request, user):
for key in request.user.sshkey_set.all():
key_list.append(key.key)
except:
return HttpResponse('Can not acces to django database!', status_code=404)
return HttpResponse('Can not acces to django database!', status=404)
#Create user
if not StoreApi.createuser(user, password, key_list, str(quota)):
return HttpResponse('User does not exist on store! And could not create!')
......@@ -40,7 +40,7 @@ def index(request):
for key in request.user.sshkey_set.all():
key_list.append(key.key)
except:
return HttpResponse('Can not acces to django database!', status_code=404)
return HttpResponse('Can not acces to django database!', status=404)
if not StoreApi.updateauthorizationinfo(user, password, key_list):
return HttpResponse('Can not update authorization information!')
except:
......@@ -114,7 +114,7 @@ def ajax_download(request):
return HttpResponse(json.dumps({'url':StoreApi.requestdownload(user,dl)}))
except:
pass
return HttpResponse('File not found!', status_code=404)
return HttpResponse('File not found!', status=404)
@login_required
def ajax_upload(request):
......@@ -125,7 +125,7 @@ def ajax_upload(request):
return HttpResponse(json.dumps({'url':url}))
except:
pass
return HttpResponse('Error!', status_code=404)
return HttpResponse('Error!', status=404)
@login_required
def ajax_delete(request):
......@@ -135,7 +135,7 @@ def ajax_delete(request):
return HttpResponse(json.dumps({'success':StoreApi.requestremove(user,rm)}))
except:
pass
return HttpResponse('File not found!', status_code=404)
return HttpResponse('File not found!', status=404)
@login_required
def ajax_new_folder(request):
......@@ -147,7 +147,7 @@ def ajax_new_folder(request):
return HttpResponse(json.dumps({'success':success}))
except:
pass
return HttpResponse('Error!', status_code=404)
return HttpResponse('Error!', status=404)
@login_required
def ajax_rename(request):
......@@ -159,7 +159,7 @@ def ajax_rename(request):
return HttpResponse(json.dumps({'success':success}))
except:
pass
return HttpResponse('Error!', status_code=404)
return HttpResponse('Error!', status=404)
@login_required
def toplist(request):
......@@ -181,7 +181,7 @@ def gui(request):
for key in request.user.sshkey_set.all():
key_list.append(key.key)
except:
return HttpResponse('Can not acces to django database!', status_code=404)
return HttpResponse('Can not acces to django database!', status=404)
try:
lab_key_decoded = base64.b64decode(request.POST['KEY'])
key_list.append(lab_key_decoded)
......@@ -195,7 +195,7 @@ def gui(request):
else:
return HttpResponse('Can not update authorization information!')
else:
return HttpResponse('Method not found!', status_code=404)
return HttpResponse('Method not found!', status=404)
def logout(request):
auth.logout(request)
......
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