Commit 50e5a41d by Kálmán Viktor

dashboard: validate user input for resources

parent c44710dd
...@@ -85,21 +85,25 @@ class VmCustomizeForm(forms.Form): ...@@ -85,21 +85,25 @@ class VmCustomizeForm(forms.Form):
'required': "", 'required': "",
})) }))
cpu_count = forms.CharField(widget=forms.NumberInput(attrs={ cpu_count = forms.IntegerField(widget=forms.NumberInput(attrs={
'class': "form-control input-tags cpu-count-input", 'class': "form-control input-tags cpu-count-input",
'min': 1, 'min': 1,
'max': 10, 'max': 10,
'required': "", 'required': "",
})) }),
min_value=1, max_value=10,
)
ram_size = forms.CharField(widget=forms.TextInput(attrs={ ram_size = forms.IntegerField(widget=forms.TextInput(attrs={
'class': "form-control input-tags ram-input", 'class': "form-control input-tags ram-input",
'min': 128, 'min': 128,
'pattern': "\d+", 'pattern': "\d+",
'max': MAX_NODE_RAM, 'max': MAX_NODE_RAM,
'step': 128, 'step': 128,
'required': "", 'required': "",
})) }),
min_value=128, max_value=MAX_NODE_RAM,
)
cpu_priority = forms.ChoiceField( cpu_priority = forms.ChoiceField(
priority_choices, widget=forms.Select(attrs={ priority_choices, widget=forms.Select(attrs={
...@@ -1134,20 +1138,24 @@ class GroupPermissionForm(forms.ModelForm): ...@@ -1134,20 +1138,24 @@ class GroupPermissionForm(forms.ModelForm):
class VmResourcesForm(forms.ModelForm): class VmResourcesForm(forms.ModelForm):
num_cores = forms.CharField(widget=forms.NumberInput(attrs={ num_cores = forms.IntegerField(widget=forms.NumberInput(attrs={
'class': "form-control input-tags cpu-count-input", 'class': "form-control input-tags cpu-count-input",
'min': 1, 'min': 1,
'max': 10, 'max': 10,
'required': "", 'required': "",
})) }),
min_value=1, max_value=10,
)
ram_size = forms.CharField(widget=forms.NumberInput(attrs={ ram_size = forms.IntegerField(widget=forms.NumberInput(attrs={
'class': "form-control input-tags ram-input", 'class': "form-control input-tags ram-input",
'min': 128, 'min': 128,
'max': MAX_NODE_RAM, 'max': MAX_NODE_RAM,
'step': 128, 'step': 128,
'required': "", 'required': "",
})) }),
min_value=128, max_value=MAX_NODE_RAM,
)
priority = forms.ChoiceField(priority_choices, widget=forms.Select(attrs={ priority = forms.ChoiceField(priority_choices, widget=forms.Select(attrs={
'class': "form-control input-tags cpu-priority-input", 'class': "form-control input-tags cpu-priority-input",
......
...@@ -43,8 +43,12 @@ $(function() { ...@@ -43,8 +43,12 @@ $(function() {
url: "/dashboard/vm/" + vm + "/op/resources_change/", url: "/dashboard/vm/" + vm + "/op/resources_change/",
data: $('#vm-details-resources-form').serialize(), data: $('#vm-details-resources-form').serialize(),
success: function(data, textStatus, xhr) { success: function(data, textStatus, xhr) {
if(data.success) {
$('a[href="#activity"]').trigger("click");
} else {
addMessage(data.messages.join("<br />"), "danger");
}
$("#vm-details-resources-save i").removeClass('fa-refresh fa-spin').addClass("fa-floppy-o"); $("#vm-details-resources-save i").removeClass('fa-refresh fa-spin').addClass("fa-floppy-o");
$('a[href="#activity"]').trigger("click");
}, },
error: function(xhr, textStatus, error) { error: function(xhr, textStatus, error) {
$("#vm-details-resources-save i").removeClass('fa-refresh fa-spin').addClass("fa-floppy-o"); $("#vm-details-resources-save i").removeClass('fa-refresh fa-spin').addClass("fa-floppy-o");
......
...@@ -765,17 +765,35 @@ class VmResourcesChangeView(VmOperationView): ...@@ -765,17 +765,35 @@ class VmResourcesChangeView(VmOperationView):
if extra is None: if extra is None:
extra = {} extra = {}
resources = { instance = get_object_or_404(Instance, pk=kwargs['pk'])
'num_cores': "num_cores",
'priority': "priority",
'ram_size': "ram_size",
"max_ram_size": "ram_size", # TODO
}
for k, v in resources.iteritems():
extra[k] = request.POST.get(v)
return super(VmResourcesChangeView, self).post(request, extra, form = VmResourcesForm(request.POST, instance=instance)
*args, **kwargs) if not form.is_valid():
for f in form.errors:
messages.error(request, "<strong>%s</strong>: %s" % (
f, form.errors[f].as_text()
))
if request.is_ajax(): # this is not too nice
store = messages.get_messages(request)
store.used = True
return HttpResponse(
json.dumps({'success': False,
'messages': [unicode(m) for m in store]}),
content_type="application=json"
)
else:
return redirect(instance.get_absolute_url() + "#resources")
else:
resources = {
'num_cores': "num_cores",
'priority': "priority",
'ram_size': "ram_size",
'max_ram_size': "ram_size", # TODO
}
for k, v in resources.iteritems():
extra[k] = request.POST.get(v)
return super(VmResourcesChangeView, self).post(request, extra,
*args, **kwargs)
class TokenOperationView(OperationView): class TokenOperationView(OperationView):
......
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