Commit 5b503e39 by Czémán Arnold

dashboard, vm: rework boot menu toggling

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