From d4158a33ee283742f884316cc78bccf589611600 Mon Sep 17 00:00:00 2001 From: "Ing. Jan KRCMAR" Date: Fri, 15 Sep 2017 12:40:37 +0200 Subject: [PATCH] instance owner view: add owner button, delete owner button --- .../templates/add_instance_owner_block.html | 36 +++++++++++++++++++ instances/templates/instance.html | 29 ++++++++++++--- instances/views.py | 29 +++++++++++++++ 3 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 instances/templates/add_instance_owner_block.html diff --git a/instances/templates/add_instance_owner_block.html b/instances/templates/add_instance_owner_block.html new file mode 100644 index 0000000..b7e6df9 --- /dev/null +++ b/instances/templates/add_instance_owner_block.html @@ -0,0 +1,36 @@ +{% load i18n %} +{% if request.user.is_superuser %} + + + + + + +{% endif %} diff --git a/instances/templates/instance.html b/instances/templates/instance.html index 7a8140f..8e26148 100644 --- a/instances/templates/instance.html +++ b/instances/templates/instance.html @@ -995,10 +995,31 @@
-

{% trans "Instance owners" %}

- {% for userinstance in userinstances %} -

{{ userinstance.user }}

- {% endfor %} +
+

+ {% trans "Instance owners" %} + {% include 'add_instance_owner_block.html' %} +

+
+
+ + + {% for userinstance in userinstances %} + + + + + {% endfor %} + +
{{ userinstance.user }} +
{% csrf_token %} + + +
+
+
{% endif %} diff --git a/instances/views.py b/instances/views.py index d6d5c61..14b4eae 100644 --- a/instances/views.py +++ b/instances/views.py @@ -14,6 +14,7 @@ from django.utils.translation import ugettext_lazy as _ from django.contrib.auth.decorators import login_required from computes.models import Compute from instances.models import Instance +from django.contrib.auth.models import User from accounts.models import UserInstance, UserSSHKey from vrtManager.hostdetails import wvmHostDetails from vrtManager.instance import wvmInstance, wvmInstances @@ -182,6 +183,7 @@ def instance(request, compute_id, vname): compute = get_object_or_404(Compute, pk=compute_id) computes = Compute.objects.all() computes_count = len(computes) + users = User.objects.all().order_by('username') publickeys = UserSSHKey.objects.filter(user_id=request.user.id) keymaps = QEMU_KEYMAPS console_types = QEMU_CONSOLE_TYPES @@ -663,6 +665,33 @@ def instance(request, compute_id, vname): addlogmsg(request.user.username, instance.name, msg) return HttpResponseRedirect(request.get_full_path() + '#options') + if 'add_owner' in request.POST: + user_id = int(request.POST.get('user_id', '')) + + if settings.ALLOW_INSTANCE_MULTIPLE_OWNER: + check_inst = UserInstance.objects.filter(instance=instance, user_id=user_id) + else: + check_inst = UserInstance.objects.filter(instance=instance) + + if check_inst: + msg = _("Owner already added") + error_messages.append(msg) + else: + add_user_inst = UserInstance(instance=instance, user_id=user_id) + add_user_inst.save() + msg = _("Added owner %d" % user_id) + addlogmsg(request.user.username, instance.name, msg) + return HttpResponseRedirect(request.get_full_path() + '#users') + + if 'del_owner' in request.POST: + userinstance_id = int(request.POST.get('userinstance', '')) + userinstance = UserInstance.objects.get(pk=userinstance_id) + userinstance.delete() + msg = _("Deleted owner %d" % userinstance_id) + addlogmsg(request.user.username, instance.name, msg) + return HttpResponseRedirect(request.get_full_path() + '#users') + + if request.user.is_superuser or request.user.userattributes.can_clone_instances: if 'clone' in request.POST: clone_data = {}