edge cases handled

This commit is contained in:
Arvind Tiwari 2017-09-15 16:46:56 +05:30
parent 852c1228ba
commit 6136a48961
2 changed files with 62 additions and 31 deletions

View file

@ -1,42 +1,72 @@
function VMTerminateStatus($container, url) {
$.get(url)
.done(function(data) {
setTimeout(function(){
VMTerminateStatus($container, url);
}, 2000);
})
.fail(function(data) {
VMTerminateSuccess($container, data)
window.location.reload(true);
});
}
function VMTerminateActive($container, altText) {
$container.find('.alert-danger').addClass('hide');
$container.addClass('processing')
.find('.vm-item-lg').attr('class', '')
.addClass('vm-item-lg vm-color-failed')
.text(altText);
$container.find('.btn').prop('disabled', true);
$('#confirm-cancel').modal('hide');
}
function VMTerminateSuccess($container, data) {
$container.addClass('terminate-success')
.find('.vm-item-lg').text(data.text);
$container.find('.btn').remove();
}
function VMTerminateFail($container, data, text) {
$container.addClass('terminate-fail')
.find('.vm-item-lg').text(text);
$container.find('.btn').prop('disabled', false);
$container.find('.alert-danger').text(data.text).removeClass('hide');
$container.removeClass('processing');
}
$(document).ready(function() { $(document).ready(function() {
$('#confirm-cancel').on('click', '.btn-ok', function(e) { $('#confirm-cancel').on('click', '.btn-ok', function(e) {
var url = $('#virtual_machine_cancel_form').attr('action'); var url = $('#virtual_machine_cancel_form').attr('action');
var $container = $('#terminate-VM'); var $container = $('#terminate-VM');
var $btn = $container.find('.btn');
var text = $container.find('.vm-item-lg').text(); var text = $container.find('.vm-item-lg').text();
var altText = $container.attr('data-alt'); var altText = $container.attr('data-alt');
$container.find('.alert-danger').addClass('hide'); VMTerminateActive($container, altText);
$container.addClass('processing')
.find('.vm-item-lg').attr('class', '')
.addClass('vm-item-lg vm-color-failed')
.text(altText);
$btn.prop('disabled', true);
$('#confirm-cancel').modal('hide');
$.post(url) $.post(url)
.done(function(data) { .done(function(data) {
console.log("success", data); console.log("success", data);
if (data.status == true) { if (data.status == true) {
$container.addClass('terminate-success') VMTerminateSuccess($container, data);
.find('.vm-item-lg').text(data.text);
$btn.remove();
window.location = data.redirect; window.location = data.redirect;
} else { } else {
$container.addClass('terminate-fail') if ('text' in data) {
.find('.vm-item-lg').text(text); VMTerminateFail($container, data, text);
$container.find('.btn').prop('disabled', false); } else {
$container.find('.alert-danger').text(data.text).removeClass('hide'); VMTerminateStatus($container, url);
}
} }
}) })
.fail(function(data) { .fail(function(data) {
$container.addClass('terminate-fail') console.log(data)
.find('.vm-item-lg').text(text); if (data.status==504) {
$container.find('.btn').prop('disabled', false); VMTerminateStatus($container, url);
$container.find('.alert-danger').removeClass('hide'); } else {
VMTerminateFail($container, data, text);
}
}) })
.always(function(data) {
$container.removeClass('processing');
});
}); });
var hash = window.location.hash; var hash = window.location.hash;

View file

@ -923,7 +923,10 @@ class VirtualMachineView(LoginRequiredMixin, View):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
vm = self.get_object() vm = self.get_object()
if vm is None: if vm is None:
return redirect(reverse('hosting:virtual_machines')) if self.request.is_ajax():
raise Http404()
else:
return redirect(reverse('hosting:virtual_machines'))
try: try:
serializer = VirtualMachineSerializer(vm) serializer = VirtualMachineSerializer(vm)
context = { context = {
@ -938,7 +941,7 @@ class VirtualMachineView(LoginRequiredMixin, View):
return render(request, self.template_name, context) return render(request, self.template_name, context)
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
response = {} response = {'status': False}
owner = self.request.user owner = self.request.user
vm = self.get_object() vm = self.get_object()
@ -948,23 +951,25 @@ class VirtualMachineView(LoginRequiredMixin, View):
email=owner.email, email=owner.email,
password=owner.password password=owner.password
) )
vm_data = VirtualMachineSerializer(manager.get_vm(vm.id)).data vm_data = VirtualMachineSerializer(manager.get_vm(vm.id)).data
terminated = manager.delete_vm(vm.id) terminated = manager.delete_vm(vm.id)
if not terminated: if not terminated:
response['status'] = False
response['text'] = ugettext( response['text'] = ugettext(
'Error terminating VM') + opennebula_vm_id 'Error terminating VM') + opennebula_vm_id
else: else:
for t in range(50): for t in range(15):
try: try:
manager.get_vm(self.kwargs.get('pk')) manager.get_vm(self.kwargs.get('pk'))
except BaseException: except BaseException:
response['status'] = True
response['redirect'] = self.get_success_url()
response['text'] = ugettext('Terminated')
break break
else: else:
sleep(2) sleep(2)
context = { context = {
'vm': vm_data, 'vm': vm_data,
'base_url': "{0}://{1}".format(self.request.scheme, 'base_url': "{0}://{1}".format(self.request.scheme,
@ -981,10 +986,6 @@ class VirtualMachineView(LoginRequiredMixin, View):
} }
email = BaseEmail(**email_data) email = BaseEmail(**email_data)
email.send() email.send()
response['status'] = True
response['redirect'] = self.get_success_url()
response['text'] = ugettext('Terminated')
return HttpResponse( return HttpResponse(
json.dumps(response), json.dumps(response),
content_type="application/json" content_type="application/json"