Commit 62dd4060 by tarokkk

webui: Fixed template running instance number

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