Commit 17cc67c3 by Czémán Arnold

dashboard, vm: transform old vm rename solution to RenameOperation

parent a8272957
Pipeline #177 failed with stage
in 0 seconds
......@@ -1528,6 +1528,10 @@ class VmResourcesForm(forms.ModelForm):
fields = ('num_cores', 'priority', 'ram_size', )
class VmRenameForm(forms.Form):
new_name = forms.CharField()
vm_search_choices = (
("owned", _("owned")),
("shared", _("shared")),
......
......@@ -111,9 +111,10 @@ $(function() {
/* rename ajax */
$('.vm-details-rename-submit').click(function() {
var name = $(this).parent("span").prev("input").val();
var url = $("#vm-details-rename-form").attr("action");
$.ajax({
method: 'POST',
url: location.href,
url: url,
data: {'new_name': name},
headers: {"X-CSRFToken": getCookie('csrftoken')},
success: function(data, textStatus, xhr) {
......
......@@ -55,7 +55,7 @@
</div>
<h1>
<div id="vm-details-rename" class="vm-details-home-rename-form-div">
<form action="" method="POST" id="vm-details-rename-form">
<form action="{{ rename_op.get_url }}" method="POST" id="vm-details-rename-form">
{% csrf_token %}
<div class="input-group vm-details-home-name">
<input id="vm-details-rename-name" class="form-control input-sm" name="new_name" type="text" value="{{ instance.name }}"/>
......
......@@ -16,7 +16,7 @@
<small class="vm-details-home-edit-name">{{ instance.name }}</small>
</div>
<div class="js-hidden vm-details-home-rename-form-div" id="vm-details-home-rename">
<form method="POST">
<form action="{{ rename_op.get_url }}" method="POST">
{% csrf_token %}
<div class="input-group">
<input type="text" name="new_name" value="{{ instance.name }}" class="form-control input-sm"/>
......
......@@ -68,6 +68,7 @@ from ..forms import (
VmMigrateForm, VmDeployForm,
VmPortRemoveForm, VmPortAddForm,
VmRemoveInterfaceForm,
VmRenameForm,
)
from request.models import TemplateAccessType, LeaseType
from request.forms import LeaseRequestForm, TemplateRequestForm
......@@ -133,6 +134,7 @@ class VmDetailView(GraphMixin, CheckedDetailView):
kwargs={'pk': self.object.pk}),
'ops': ops,
'op': {i.op: i for i in ops},
'rename_op': self.get_rename_operation(instance),
'connect_commands': user.profile.get_connect_commands(instance),
'hide_tutorial': hide_tutorial,
'fav': instance.favourite_set.filter(user=user).exists(),
......@@ -199,7 +201,6 @@ class VmDetailView(GraphMixin, CheckedDetailView):
def post(self, request, *args, **kwargs):
options = {
'new_name': self.__set_name,
'new_description': self.__set_description,
'new_tag': self.__add_tag,
'to_remove': self.__remove_tag,
......@@ -210,29 +211,6 @@ class VmDetailView(GraphMixin, CheckedDetailView):
return v(request)
raise Http404()
def __set_name(self, request):
self.object = self.get_object()
if not self.object.has_level(request.user, "operator"):
raise PermissionDenied()
new_name = request.POST.get("new_name")
Instance.objects.filter(pk=self.object.pk).update(
**{'name': new_name})
success_message = _("VM successfully renamed.")
if request.is_ajax():
response = {
'message': success_message,
'new_name': new_name,
'vm_pk': self.object.pk
}
return HttpResponse(
json.dumps(response),
content_type="application/json"
)
else:
messages.success(request, success_message)
return redirect(self.object.get_absolute_url())
def __set_description(self, request):
self.object = self.get_object()
if not self.object.has_level(request.user, "operator"):
......@@ -306,6 +284,19 @@ class VmDetailView(GraphMixin, CheckedDetailView):
return HttpResponseRedirect("%s#activity" %
self.object.get_absolute_url())
def get_rename_operation(self, instance):
v = VmRenameView
try:
op = v.get_op_by_object(instance)
op.check_auth(self.request.user)
op.check_precond()
except PermissionDenied as e:
logger.debug('Not showing operation %s for %s: %s',
'rename', instance, unicode(e))
except Exception:
return v.bind_to_object(instance, disabled=True)
return v.bind_to_object(instance)
class VmTraitsUpdate(SuperuserRequiredMixin, UpdateView):
form_class = TraitsForm
......@@ -743,6 +734,32 @@ class VmDeployView(FormOperationMixin, VmOperationView):
return kwargs
class VmRenameView(FormOperationMixin, VmOperationView):
op = 'rename'
icon = 'pencil'
effect = 'success'
show_in_toolbar = False
form_class = VmRenameForm
def post(self, request, extra=None, *args, **kwargs):
if extra is None:
extra = {}
form = self.form_class(self.request.POST, **self.get_form_kwargs())
if form.is_valid():
extra.update(form.cleaned_data)
resp = super(FormOperationMixin, self).post(
request, extra, *args, **kwargs)
new_name = request.POST.get("new_name")
success_message = _("VM successfully renamed.")
if request.is_ajax():
return JsonResponse({'new_name': new_name})
else:
messages.success(request, success_message)
return resp
else:
return self.get(request)
vm_ops = OrderedDict([
('deploy', VmDeployView),
('wake_up', VmOperationView.factory(
......@@ -792,6 +809,7 @@ vm_ops = OrderedDict([
op='install_keys', icon='key', effect='info',
show_in_toolbar=False,
)),
('rename', VmRenameView),
])
......
......@@ -1403,6 +1403,28 @@ class ResourcesOperation(InstanceOperation):
@register_operation
class RenameOperation(InstanceOperation):
id = "rename"
name = _("rename")
description = _("Change the name of virtual machine.")
acl_level = "operator"
required_perms = ("vm.change_resources", )
def _operation(self, user, activity, new_name):
old_name = self.instance.name
self.instance.name = new_name
self.instance.full_clean()
self.instance.save()
return create_readable(ugettext_noop(
"Changed name from '%(old_name)s' to '%(new_name)s'."),
old_name=old_name, new_name=new_name
)
@register_operation
class PasswordResetOperation(RemoteAgentOperation):
id = 'password_reset'
name = _("password reset")
......
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