Commit 28e65ce1 by Kálmán Viktor

request: add resource request

parent d99970c2
...@@ -1270,6 +1270,21 @@ textarea[name="new_members"] { ...@@ -1270,6 +1270,21 @@ textarea[name="new_members"] {
margin-top: 20px; margin-top: 20px;
} }
#vm-renew-request-lease { #vm-renew-request-lease, #vm-request-resource-form {
display: none; display: none;
} }
.label-100 {
display: block;
width: 100%;
}
#modify-the-resources {
font-size: 18px;
display: none;
}
#vm-request-resource-form textarea {
max-width: 500px;
height: 150px;
}
...@@ -228,4 +228,20 @@ $(function() { ...@@ -228,4 +228,20 @@ $(function() {
e.preventDefault(); e.preventDefault();
}); });
$("#vm-request-resource").click(function(e) {
$(".cpu-priority-slider, .cpu-count-slider, .ram-slider").simpleSlider("setDisabled", false);
$(".ram-input, .cpu-count-input, .cpu-priority-input").prop("disabled", false);
$("#vm-details-resources-form").prop("action", $(this).prop("href"));
$("#vm-request-resource-form").show();
$("#modify-the-resources").show();
$(this).hide();
$("html, body").animate({
scrollTop: $("#modify-the-resources").offset().top - 60
});
return e.preventDefault();
});
}); });
...@@ -2,19 +2,39 @@ ...@@ -2,19 +2,39 @@
{% load sizefieldtags %} {% load sizefieldtags %}
{% load crispy_forms_tags %} {% load crispy_forms_tags %}
<div class="label label-info label-100" id="modify-the-resources">
{% trans "Modify the resources" %}
</div>
<form method="POST" action="{{ op.resources_change.get_url }}" id="vm-details-resources-form"> <form method="POST" action="{{ op.resources_change.get_url }}" id="vm-details-resources-form">
{% csrf_token %} {% csrf_token %}
{% include "dashboard/_resources-sliders.html" with field_priority=resources_form.priority field_num_cores=resources_form.num_cores field_ram_size=resources_form.ram_size %} {% include "dashboard/_resources-sliders.html" with field_priority=resources_form.priority field_num_cores=resources_form.num_cores field_ram_size=resources_form.ram_size %}
{% if op.resources_change %} {% if op.resources_change %}
<button type="submit" class="btn btn-success btn-sm change-resources-button" <button type="submit" class="btn btn-success btn-sm change-resources-button"
id="vm-details-resources-save" data-vm="{{ instance.pk }}" id="vm-details-resources-save" data-vm="{{ instance.pk }}"
{% if op.resources_change.disabled %}disabled{% endif %}> {% if op.resources_change.disabled %}disabled{% endif %}>
<i class="fa fa-floppy-o"></i> {% trans "Save resources" %} <i class="fa fa-floppy-o"></i> {% trans "Save resources" %}
</button> </button>
<span class="change-resources-help" <span class="change-resources-help"
{% if not op.resources_change.disabled %}style="display: none;"{% endif %} {% if not op.resources_change.disabled %}style="display: none;"{% endif %}>
>{% trans "Stop your VM to change resources." %}</span> {% trans "Stop your VM to change resources." %}
</span>
{% else %}
<div id="vm-request-resource-form">
<div class="form-group">
<label>{% trans "Reason" %}*</label>
<textarea class="form-control" name="reason"></textarea>
</div>
<input type="submit" class="btn btn-success btn-sm"/>
</div>
<a href="{% url "request.views.request-resource" vm_pk=object.pk %}"
class="btn btn-primary btn-sm" id="vm-request-resource">
<i class="fa fa-tasks"></i>
{% trans "Request more resource" %}
</a>
{% endif %} {% endif %}
</form> </form>
......
...@@ -10,6 +10,7 @@ from crispy_forms.layout import Submit ...@@ -10,6 +10,7 @@ from crispy_forms.layout import Submit
from request.models import ( from request.models import (
LeaseType, TemplateAccessType, TemplateAccessAction, LeaseType, TemplateAccessType, TemplateAccessAction,
) )
from dashboard.forms import VmResourcesForm
class LeaseTypeForm(ModelForm): class LeaseTypeForm(ModelForm):
...@@ -54,3 +55,6 @@ class LeaseRequestForm(Form): ...@@ -54,3 +55,6 @@ class LeaseRequestForm(Form):
lease = ModelChoiceField(LeaseType.objects.all(), lease = ModelChoiceField(LeaseType.objects.all(),
label=_("Lease")) label=_("Lease"))
reason = CharField(widget=forms.Textarea) reason = CharField(widget=forms.Textarea)
class ResourceRequestForm(VmResourcesForm):
reason = CharField(widget=forms.Textarea)
{% extends "dashboard/base.html" %}
{% load i18n %}
{% load crispy_forms_tags %}
{% block content %}
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="no-margin">
<i class="fa fa-tasks"></i> {% trans "Request new resources" %}
</h3>
</div>
<div class="panel-body">
<form action="{% url "request.views.request-resource" vm_pk=vm.pk %}" method="POST">
{% csrf_token %}
<div class="form-group">
<label>{% trans "Virtual machine" %}</label>
<div class="controls">
<a href="{{ vm.get_absolute_url }}">{{ vm.name }}</a>
</div>
</div>
{% include "display-form-errors.html" %}
{% include "dashboard/_resources-sliders.html" with field_priority=form.priority field_num_cores=form.num_cores field_ram_size=form.ram_size %}
{{ form.reason|as_crispy_field }}
<button type="submit" class="btn btn-success">
{% trans "Request new resources" %}
</button>
</form>
</div>
</div>
</div>
</div>
{% endblock %}
...@@ -22,7 +22,7 @@ from .views import ( ...@@ -22,7 +22,7 @@ from .views import (
RequestList, RequestDetail, RequestTypeList, RequestList, RequestDetail, RequestTypeList,
LeaseTypeCreate, LeaseTypeDetail, LeaseTypeCreate, LeaseTypeDetail,
TemplateAccessTypeCreate, TemplateAccessTypeDetail, TemplateAccessTypeCreate, TemplateAccessTypeDetail,
TemplateRequestView, LeaseRequestView, TemplateRequestView, LeaseRequestView, ResourceRequestView,
) )
urlpatterns = patterns( urlpatterns = patterns(
...@@ -49,5 +49,7 @@ urlpatterns = patterns( ...@@ -49,5 +49,7 @@ urlpatterns = patterns(
url(r'template/$', TemplateRequestView.as_view(), url(r'template/$', TemplateRequestView.as_view(),
name="request.views.request-template"), name="request.views.request-template"),
url(r'lease/(?P<vm_pk>\d+)/$', LeaseRequestView.as_view(), url(r'lease/(?P<vm_pk>\d+)/$', LeaseRequestView.as_view(),
name="request.views.request-lease") name="request.views.request-lease"),
url(r'resource/(?P<vm_pk>\d+)/$', ResourceRequestView.as_view(),
name="request.views.request-resource"),
) )
...@@ -11,7 +11,7 @@ from django_tables2 import SingleTableView ...@@ -11,7 +11,7 @@ from django_tables2 import SingleTableView
from request.models import ( from request.models import (
Request, TemplateAccessType, LeaseType, TemplateAccessAction, Request, TemplateAccessType, LeaseType, TemplateAccessAction,
ExtendLeaseAction, ExtendLeaseAction, ResourceChangeAction,
) )
from vm.models import Instance from vm.models import Instance
from request.tables import ( from request.tables import (
...@@ -19,8 +19,9 @@ from request.tables import ( ...@@ -19,8 +19,9 @@ from request.tables import (
) )
from request.forms import ( from request.forms import (
LeaseTypeForm, TemplateAccessTypeForm, TemplateRequestForm, LeaseTypeForm, TemplateAccessTypeForm, TemplateRequestForm,
LeaseRequestForm, LeaseRequestForm, ResourceRequestForm,
) )
from dashboard.forms import VmResourcesForm
class RequestList(LoginRequiredMixin, SuperuserRequiredMixin, SingleTableView): class RequestList(LoginRequiredMixin, SuperuserRequiredMixin, SingleTableView):
...@@ -153,3 +154,60 @@ class LeaseRequestView(FormView): ...@@ -153,3 +154,60 @@ class LeaseRequestView(FormView):
req.save() req.save()
return redirect(vm.get_absolute_url()) return redirect(vm.get_absolute_url())
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())
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