views.py 8.02 KB
Newer Older
Kálmán Viktor committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Copyright 2014 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE.  If not, see <http://www.gnu.org/licenses/>.
Kálmán Viktor committed
17 18 19 20 21
from __future__ import unicode_literals, absolute_import

from django.views.generic import (
    UpdateView, TemplateView, DetailView, CreateView, FormView,
)
22 23
from django.shortcuts import redirect, get_object_or_404
from django.core.exceptions import PermissionDenied
Kálmán Viktor committed
24 25 26 27

from braces.views import SuperuserRequiredMixin, LoginRequiredMixin
from django_tables2 import SingleTableView

28 29
from request.models import (
    Request, TemplateAccessType, LeaseType, TemplateAccessAction,
30
    ExtendLeaseAction, ResourceChangeAction,
31 32
)
from vm.models import Instance
33
from vm.operations import ResourcesRequestOperation
Kálmán Viktor committed
34 35 36 37
from request.tables import (
    RequestTable, TemplateAccessTypeTable, LeaseTypeTable,
)
from request.forms import (
38
    LeaseTypeForm, TemplateAccessTypeForm, TemplateRequestForm,
39
    LeaseRequestForm, ResourceRequestForm,
Kálmán Viktor committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
)


class RequestList(LoginRequiredMixin, SuperuserRequiredMixin, SingleTableView):
    model = Request
    table_class = RequestTable
    template_name = "request/list.html"

    def get_context_data(self, **kwargs):
        context = super(RequestList, self).get_context_data(**kwargs)
        context['statuses'] = Request.STATUSES
        return context

    def get_table_data(self):
        data = Request.objects.all()
        status = self.request.GET.get("status")
        if status:
            data = data.filter(status=status)

        return data


class RequestDetail(LoginRequiredMixin, SuperuserRequiredMixin, DetailView):
    model = Request
    template_name = "request/detail.html"

66
    def post(self, *args, **kwargs):
67 68 69 70 71 72 73 74
        if self.get_object().status in ["PENDING", "UNSEEN"]:
            user = self.request.user
            accept = self.request.POST.get("accept")
            request = self.get_object()  # not self.request!
            if accept:
                request.accept(user)
            else:
                request.decline(user)
75 76 77

        return redirect(request.get_absolute_url())

78 79 80 81 82
    def get_context_data(self, **kwargs):
        request = self.object
        context = super(RequestDetail, self).get_context_data(**kwargs)

        context['action'] = request.action
83
        context['accept_states'] = ResourcesRequestOperation.accept_states
84 85 86 87 88 89

        if request.status == Request.STATUSES.UNSEEN:
            request.status = Request.STATUSES.PENDING
            request.save()
        return context

Kálmán Viktor committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

class TemplateAccessTypeDetail(LoginRequiredMixin, SuperuserRequiredMixin,
                               UpdateView):
    model = TemplateAccessType
    template_name = "request/template-type-form.html"
    form_class = TemplateAccessTypeForm


class TemplateAccessTypeCreate(LoginRequiredMixin, SuperuserRequiredMixin,
                               CreateView):
    model = TemplateAccessType
    template_name = "request/template-type-form.html"
    form_class = TemplateAccessTypeForm


class LeaseTypeDetail(LoginRequiredMixin, SuperuserRequiredMixin, UpdateView):
    model = LeaseType
    template_name = "request/lease-type-form.html"
    form_class = LeaseTypeForm


class LeaseTypeCreate(LoginRequiredMixin, SuperuserRequiredMixin, CreateView):
    model = LeaseType
    template_name = "request/lease-type-form.html"
    form_class = LeaseTypeForm


class RequestTypeList(LoginRequiredMixin, SuperuserRequiredMixin,
                      TemplateView):
    template_name = "request/type-list.html"

    def get_context_data(self, **kwargs):
        context = super(RequestTypeList, self).get_context_data(**kwargs)

        context['lease_table'] = LeaseTypeTable(
            LeaseType.objects.all(), request=self.request)
        context['template_table'] = TemplateAccessTypeTable(
            TemplateAccessType.objects.all(), request=self.request)

        return context


class TemplateRequestView(FormView):
    form_class = TemplateRequestForm
    template_name = "request/request-template.html"

136 137 138 139 140
    def get_form_kwargs(self):
        kwargs = super(TemplateRequestView, self).get_form_kwargs()
        kwargs['request'] = self.request
        return kwargs

Kálmán Viktor committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    def form_valid(self, form):
        data = form.cleaned_data
        user = self.request.user

        ta = TemplateAccessAction(
            template_type=data['template'],
            level=data['level'],
            user=user,
        )
        ta.save()

        req = Request(
            user=user,
            reason=data['reason'],
            type=Request.TYPES.template,
            action=ta
        )
        req.save()

        return redirect("/")
161 162 163 164 165 166


class LeaseRequestView(FormView):
    form_class = LeaseRequestForm
    template_name = "request/request-lease.html"

167 168 169 170 171
    def get_vm(self):
        return get_object_or_404(Instance, pk=self.kwargs['vm_pk'])

    def dispatch(self, *args, **kwargs):
        vm = self.get_vm()
172 173 174
        user = self.request.user
        if not vm.has_level(user, 'operator'):
            raise PermissionDenied()
175
        return super(LeaseRequestView, self).dispatch(*args, **kwargs)
176

177
    def get_context_data(self, **kwargs):
178
        context = super(LeaseRequestView, self).get_context_data(**kwargs)
179
        context['vm'] = self.get_vm()
180 181
        return context

182 183 184 185 186
    def get_form_kwargs(self):
        kwargs = super(LeaseRequestView, self).get_form_kwargs()
        kwargs['request'] = self.request
        return kwargs

187
    def form_valid(self, form):
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
        data = form.cleaned_data
        user = self.request.user
        vm = self.get_vm()

        el = ExtendLeaseAction(
            lease_type=data['lease'],
            instance=vm,
        )
        el.save()

        req = Request(
            user=user,
            reason=data['reason'],
            type=Request.TYPES.lease,
            action=el
        )
        req.save()

        return redirect(vm.get_absolute_url())
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263


class ResourceRequestView(FormView):
    form_class = ResourceRequestForm
    template_name = "request/request-resource.html"

    def get_vm(self):
        return get_object_or_404(Instance, pk=self.kwargs['vm_pk'])

    def dispatch(self, *args, **kwargs):
        vm = self.get_vm()
        user = self.request.user
        if not vm.has_level(user, "user"):
            raise PermissionDenied()
        return super(ResourceRequestView, self).dispatch(*args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(ResourceRequestView, self).get_context_data(**kwargs)
        context['vm'] = self.get_vm()
        return context

    def get_form_kwargs(self):
        kwargs = super(ResourceRequestView, self).get_form_kwargs()
        kwargs['can_edit'] = True
        kwargs['instance'] = self.get_vm()
        return kwargs

    def get_initial(self):
        vm = self.get_vm()
        initial = super(ResourceRequestView, self).get_initial()
        initial['num_cores'] = vm.num_cores
        initial['priority'] = vm.priority
        initial['ram_size'] = vm.ram_size
        return initial

    def form_valid(self, form):
        vm = self.get_vm()
        data = form.cleaned_data
        user = self.request.user

        rc = ResourceChangeAction(
            instance=vm,
            num_cores=data['num_cores'],
            priority=data['priority'],
            ram_size=data['ram_size'],
        )
        rc.save()

        req = Request(
            user=user,
            reason=data['reason'],
            type=Request.TYPES.resource,
            action=rc
        )
        req.save()

        return redirect(vm.get_absolute_url())