Commit c766e690 by Őry Máté

Merge branch 'issue-267' into 'master'

Fix vm customization dialog's error handling

Closes #267
parents 06018f2d f4539967
...@@ -143,6 +143,8 @@ class VmCustomizeForm(forms.Form): ...@@ -143,6 +143,8 @@ class VmCustomizeForm(forms.Form):
self.template = kwargs.pop("template", None) self.template = kwargs.pop("template", None)
super(VmCustomizeForm, self).__init__(*args, **kwargs) super(VmCustomizeForm, self).__init__(*args, **kwargs)
if self.user.has_perm("vm_set_resouces"):
self.allowed_fields = tuple(self.fields.keys())
# set displayed disk and network list # set displayed disk and network list
self.fields['disks'].queryset = self.template.disks.all() self.fields['disks'].queryset = self.template.disks.all()
self.fields['networks'].queryset = Vlan.get_objects_with_level( self.fields['networks'].queryset = Vlan.get_objects_with_level(
...@@ -158,10 +160,29 @@ class VmCustomizeForm(forms.Form): ...@@ -158,10 +160,29 @@ class VmCustomizeForm(forms.Form):
self.initial['cpu_count'] = self.template.num_cores self.initial['cpu_count'] = self.template.num_cores
self.initial['ram_size'] = self.template.ram_size self.initial['ram_size'] = self.template.ram_size
else:
self.allowed_fields = ("name", "template", "customized", )
# initial name and template pk # initial name and template pk
self.initial['name'] = self.template.name self.initial['name'] = self.template.name
self.initial['template'] = self.template.pk self.initial['template'] = self.template.pk
self.initial['customized'] = self.template.pk self.initial['customized'] = True
def _clean_fields(self):
for name, field in self.fields.items():
if name in self.allowed_fields:
value = field.widget.value_from_datadict(
self.data, self.files, self.add_prefix(name))
try:
value = field.clean(value)
self.cleaned_data[name] = value
if hasattr(self, 'clean_%s' % name):
value = getattr(self, 'clean_%s' % name)()
self.cleaned_data[name] = value
except ValidationError as e:
self._errors[name] = self.error_class(e.messages)
if name in self.cleaned_data:
del self.cleaned_data[name]
class GroupCreateForm(forms.ModelForm): class GroupCreateForm(forms.ModelForm):
......
...@@ -222,6 +222,8 @@ function vmCustomizeLoaded() { ...@@ -222,6 +222,8 @@ function vmCustomizeLoaded() {
$(this).find("i").prop("class", "fa fa-spinner fa-spin"); $(this).find("i").prop("class", "fa fa-spinner fa-spin");
if($("#create-modal")) return true;
$.ajax({ $.ajax({
url: '/dashboard/vm/create/', url: '/dashboard/vm/create/',
headers: {"X-CSRFToken": getCookie('csrftoken')}, headers: {"X-CSRFToken": getCookie('csrftoken')},
......
...@@ -3,10 +3,11 @@ ...@@ -3,10 +3,11 @@
{% load sizefieldtags %} {% load sizefieldtags %}
{% include "display-form-errors.html" with form=vm_create_form %} {% include "display-form-errors.html" with form=vm_create_form %}
<form method="POST"> <form method="POST" action="{% url "dashboard.views.vm-create" %}">
{% csrf_token %} {% csrf_token %}
{{ vm_create_form.template }} {{ vm_create_form.template }}
{{ vm_create_form.customized }}
<div class="row"> <div class="row">
<div class="col-sm-12"> <div class="col-sm-12">
...@@ -23,7 +24,6 @@ ...@@ -23,7 +24,6 @@
</div> </div>
{% if perms.vm.set_resources %} {% if perms.vm.set_resources %}
{{ vm_create_form.customized }}
<div class="row"> <div class="row">
<div class="col-sm-10"> <div class="col-sm-10">
<div class="form-group"> <div class="form-group">
......
...@@ -2157,8 +2157,6 @@ class VmCreate(LoginRequiredMixin, TemplateView): ...@@ -2157,8 +2157,6 @@ class VmCreate(LoginRequiredMixin, TemplateView):
raise PermissionDenied() raise PermissionDenied()
args = {"template": template, "owner": user} args = {"template": template, "owner": user}
if "name" in request.POST:
args["name"] = request.POST.get("name")
instances = [Instance.create_from_template(**args)] instances = [Instance.create_from_template(**args)]
return self.__deploy(request, instances) return self.__deploy(request, instances)
...@@ -2179,30 +2177,31 @@ class VmCreate(LoginRequiredMixin, TemplateView): ...@@ -2179,30 +2177,31 @@ class VmCreate(LoginRequiredMixin, TemplateView):
if not template.has_level(user, 'user'): if not template.has_level(user, 'user'):
raise PermissionDenied() raise PermissionDenied()
if request.user.has_perm('vm.set_resources'):
ikwargs = { ikwargs = {
'name': post['name'], 'name': post['name'],
'num_cores': post['cpu_count'], 'template': template,
'ram_size': post['ram_size'], 'owner': user,
'priority': post['cpu_priority'],
'max_ram_size': post['ram_size'],
} }
amount = post.get("amount", 1)
if request.user.has_perm('vm.set_resources'):
networks = [InterfaceTemplate(vlan=l, managed=l.managed) networks = [InterfaceTemplate(vlan=l, managed=l.managed)
for l in post['networks']] for l in post['networks']]
ikwargs.update({ ikwargs.update({
'template': template, 'num_cores': post['cpu_count'],
'owner': user, 'ram_size': post['ram_size'],
'priority': post['cpu_priority'],
'max_ram_size': post['ram_size'],
'networks': networks, 'networks': networks,
'disks': list(template.disks.all()), 'disks': list(template.disks.all()),
}) })
amount = post['amount'] else:
pass
instances = Instance.mass_create_from_template(amount=amount, instances = Instance.mass_create_from_template(amount=amount,
**ikwargs) **ikwargs)
return self.__deploy(request, instances) return self.__deploy(request, instances)
else:
raise PermissionDenied()
def __deploy(self, request, instances, *args, **kwargs): def __deploy(self, request, instances, *args, **kwargs):
for i in instances: for i in instances:
......
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