Commit 62dd4060 by tarokkk

webui: Fixed template running instance number

parent 150644d6
......@@ -283,6 +283,8 @@ class Template(models.Model):
help_text=(_('Name of operating system in format like "%s".') %
"Ubuntu 12.04 LTS Desktop amd64"))
def running_instances(self):
return self.instance_set.exclude(state='DONE').count()
def os_type(self):
if self.access_type == 'rdp':
return "win"
......@@ -294,7 +296,7 @@ class Template(models.Model):
@transaction.commit_on_success
def safe_delete(self):
if not self.instance_set.exists():
if not self.instance_set.exclude(state='DONE').exists():
self.delete()
return True
else:
......
......@@ -25,7 +25,7 @@
</span></li>
<li class="description">{% trans "Description" %}: <span class="value">{{t.description}}</span><div class="clear"></div></li>
<li class="date" >{% trans "Created at" %}: <span class="value">{{t.created_at}} ({{t.owner.get_profile}})</span><div class="clear"></div></li>
<li class="count">{% trans "Running instances" %}: <span class="value">{{t.instance_set.count}}</span></li>
<li class="count">{% trans "Running instances" %}: <span class="value">{{ t.running_instances }}</span></li>
</ul>
</div>
</li>
......@@ -72,7 +72,7 @@
</span></li>
<li class="description">{% trans "Description" %}: <span class="value">{{t.description}}</span><div class="clear"></div></li>
<li class="date" >{% trans "Created at" %}: <span class="value">{{t.created_at}} ({{t.owner.get_profile}})</span><div class="clear"></div></li>
<li class="count">{% trans "Running instances" %}: <span class="value">{{t.instance_set.count}}</span></li>
<li class="count">{% trans "Running instances" %}: <span class="value">{{t.running_instances}}</span></li>
</ul>
</div>
</li>
......
......@@ -89,15 +89,17 @@ def ajax_template_delete(request):
except:
return HttpResponse(unicode(_("Invalid template ID.")), status=404)
template = get_object_or_404(Template, id=template_id)
if template.instance_set.exists():
if template.running_instances() > 0:
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)
elif template.owner != request.user:
return HttpResponse(unicode(_("You don't have permission to delete this template.")), status=404)
else:
template.safe_delete()
return HttpResponse(unicode(_("Template successfully deleted.")))
if template.safe_delete():
return HttpResponse(unicode(_("Template successfully deleted.")))
else:
return HttpResponse(unicode(_("Unexpected error happened.")), status=404)
def ajax_template_name_unique(request, name):
s = "True"
......@@ -107,12 +109,12 @@ def ajax_template_name_unique(request, name):
@login_required
def vm_credentials(request, iid):
vm = get_object_or_404(Instance, pk=iid)
if vm.owner == request.user:
try:
vm = get_object_or_404(Instance, pk=iid, owner=request.user)
return render_to_response('vm-credentials.html', RequestContext(request, { 'i' : vm }))
else:
return HttpResponse("Stale id.")
except:
return HttpResponse(_("Could not get Virtual Machine credentials."), code=404)
messages.error(request, _('Failed to power off virtual machine.'))
class AjaxTemplateWizard(View):
def get(self, request, *args, **kwargs):
......
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