Commit 65f3c289 by Máhonfai Bálint

Check template instance limit of user when creating VM

parent 9cfa0f18
Pipeline #1358 failed with stage
in 0 seconds
...@@ -1166,24 +1166,28 @@ class VmCreate(LoginRequiredMixin, TemplateView): ...@@ -1166,24 +1166,28 @@ class VmCreate(LoginRequiredMixin, TemplateView):
# limit chekcs # limit chekcs
try: try:
limit = user.profile.instance_limit instance_limit = user.profile.instance_limit
template_instance_limit = user.profile.template_instance_limit
except Exception as e: except Exception as e:
logger.debug('No profile or instance limit: %s', e) logger.debug('No profile or instance limit: %s', e)
else: else:
try: try:
amount = int(request.POST.get("amount", 1)) amount = int(request.POST.get("amount", 1))
except: except:
amount = limit # TODO this should definitely use a Form # TODO this should definitely use a Form
current = Instance.active.filter(owner=user).count() amount = instance_limit
logger.debug('current use: %d, limit: %d', current, limit) instances = Instance.active.filter(owner=user).count()
if current + amount > limit: template_instances = template.get_user_instances(user).count()
messages.error(request,
_('Instance limit (%d) exceeded.') % limit) logger.debug('current instance use: %d, limit: %d',
if request.is_ajax(): instances, instance_limit)
return HttpResponse(json.dumps({'redirect': '/'}), logger.debug('current template instance use: %d, limit: %d',
content_type="application/json") template_instances, template_instance_limit)
else:
return redirect('/') if instances + amount > instance_limit:
return self._limit_exceeded(instance_limit, request)
if template_instances + amount > template_instance_limit:
return self._limit_exceeded(template_instance_limit, request)
create_func = (self.__create_normal if create_func = (self.__create_normal if
request.POST.get("customized") is None else request.POST.get("customized") is None else
...@@ -1191,6 +1195,16 @@ class VmCreate(LoginRequiredMixin, TemplateView): ...@@ -1191,6 +1195,16 @@ class VmCreate(LoginRequiredMixin, TemplateView):
return create_func(request, template, *args, **kwargs) return create_func(request, template, *args, **kwargs)
def _limit_exceeded(self, limit, request):
messages.error(request,
_('Instance limit (%d) exceeded.')
% limit)
if request.is_ajax():
return HttpResponse(json.dumps({'redirect': '/'}),
content_type="application/json")
else:
return redirect('/')
@require_GET @require_GET
def get_vm_screenshot(request, pk): def get_vm_screenshot(request, pk):
......
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