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() {
$('#confirm-cancel').on('click', '.btn-ok', function(e) {
var url = $('#virtual_machine_cancel_form').attr('action');
var $container = $('#terminate-VM');
var $btn = $container.find('.btn');
var text = $container.find('.vm-item-lg').text();
var altText = $container.attr('data-alt');
$container.find('.alert-danger').addClass('hide');
$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');
VMTerminateActive($container, altText);
$.post(url)
.done(function(data) {
console.log("success", data);
if (data.status == true) {
$container.addClass('terminate-success')
.find('.vm-item-lg').text(data.text);
$btn.remove();
VMTerminateSuccess($container, data);
window.location = data.redirect;
} else {
$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');
if ('text' in data) {
VMTerminateFail($container, data, text);
} else {
VMTerminateStatus($container, url);
}
}
})
.fail(function(data) {
$container.addClass('terminate-fail')
.find('.vm-item-lg').text(text);
$container.find('.btn').prop('disabled', false);
$container.find('.alert-danger').removeClass('hide');
console.log(data)
if (data.status==504) {
VMTerminateStatus($container, url);
} else {
VMTerminateFail($container, data, text);
}
})
.always(function(data) {
$container.removeClass('processing');
});
});
var hash = window.location.hash;

View file

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