Commit 5b503e39 by Czémán Arnold

dashboard, vm: rework boot menu toggling

parent 1bb04667
Pipeline #186 failed with stage
in 0 seconds
......@@ -1440,13 +1440,18 @@ class ToggleBootMenuForm(forms.ModelForm):
'data-off': _('Off')})
}
def __init__(self, *args, **kwargs):
disabled = kwargs.pop('disabled')
super(ToggleBootMenuForm, self).__init__(*args, **kwargs)
if disabled:
self.fields['boot_menu'].widget.attrs['disabled'] = 'disabled'
@property
def helper(self):
helper = FormHelper()
helper.form_show_labels = True
helper.form_action =\
reverse_lazy("dashboard.instance.op.toggle_bootmenu",
kwargs={'pk': self.instance.pk})
helper.form_action = (
reverse_lazy("dashboard.instance.op.toggle_boot_menu",
kwargs={'pk': self.instance.pk}))
return helper
......
......@@ -256,4 +256,6 @@ $(function() {
$(this).parents("form").submit();
});
$('#boot-menu-save-btn').hide();
});
......@@ -103,11 +103,18 @@
{% endif %}
{% if toggle_boot_menu_form %}
{% if op.toggle_boot_menu %}
<hr />
<div class="row">
<div class="col-sm-12">
{% crispy toggle_boot_menu_form %}
<form action="{{ op.toggle_boot_menu.get_url }}" method="POST">
{% csrf_token %}
{{ toggle_boot_menu_form.boot_menu|as_crispy_field }}
<input type="submit" id="boot-menu-save-btn"
class="btn btn-success"
value="{% trans "Save" %}"
{% if op.toggle_boot_menu.disabled %}disabled{% endif %} />
</form>
</div>
</div>
{% endif %}
......
......@@ -175,9 +175,10 @@ class VmDetailView(GraphMixin, CheckedDetailView):
context['traits_form'] = TraitsForm(instance=instance)
context['raw_data_form'] = RawDataForm(instance=instance)
if is_owner and user.has_perm("vm.toggle_boot_menu"):
context['toggle_boot_menu_form'] =\
ToggleBootMenuForm(instance=instance)
context['toggle_boot_menu_form'] = ToggleBootMenuForm(
instance=instance,
disabled=hasattr(
context['op']['toggle_boot_menu'], 'disabled'))
# resources change perm
context['can_change_resources'] = self.request.user.has_perm(
......@@ -592,33 +593,20 @@ class VmResourcesChangeView(VmOperationView):
*args, **kwargs)
class VmToggleBootMenuUpdate(VmOperationView):
op = 'toggle_bootmenu'
class VmToggleBootMenuUpdate(FormOperationMixin, VmOperationView):
op = 'toggle_boot_menu'
icon = "wrench"
show_in_toolbar = False
wait_for_result = 0.5
form_class = ToggleBootMenuForm
def post(self, request, extra=None, *args, **kwargs):
instance = get_object_or_404(Instance, pk=kwargs['pk'])
form = ToggleBootMenuForm(request.POST, instance=instance)
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 JsonResponse({'success': False,
'messages': [unicode(m) for m in store]})
else:
return HttpResponseRedirect(instance.get_absolute_url() +
"#resources")
else:
extra = form.cleaned_data
return super(VmToggleBootMenuUpdate, self).post(request, extra,
*args, **kwargs)
def get_form_kwargs(self):
instance = self.get_op().instance
disabled = hasattr(self, 'disabled')
val = super(VmToggleBootMenuUpdate, self).get_form_kwargs()
val['instance'] = instance
val['disabled'] = disabled
return val
class TokenOperationView(OperationView):
......@@ -814,7 +802,7 @@ vm_ops = OrderedDict([
('add_port', VmPortAddView),
('renew', VmRenewView),
('resources_change', VmResourcesChangeView),
('toggle_bootmenu', VmToggleBootMenuUpdate),
('toggle_boot_menu', VmToggleBootMenuUpdate),
('password_reset', VmOperationView.factory(
op='password_reset', icon='unlock', effect='warning',
show_in_toolbar=False, wait_for_result=0.5, with_reload=True)),
......
......@@ -1404,12 +1404,12 @@ class ResourcesOperation(InstanceOperation):
@register_operation
class ToggleBootMenuOperation(InstanceOperation):
id = 'toggle_bootmenu'
id = 'toggle_boot_menu'
name = _("toggle boot menu")
description = _("Turn on/off boot menu.")
acl_level = "owner"
required_perms = ('vm.toggle_boot_menu', )
accept_states = ('STOPPED', 'PENDING', 'RUNNING')
accept_states = ('STOPPED', )
def _operation(self, user, activity, boot_menu):
self.instance.boot_menu = boot_menu
......
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