Commit 3d052db9 by Kálmán Viktor

dashboard: template wizard draft

parent 381c61f1
......@@ -9,6 +9,8 @@ from crispy_forms.helper import FormHelper
from crispy_forms.layout import (
Layout, Div, BaseInput, Field, HTML, Submit, Fieldset, TEMPLATE_PACK
)
from django.shortcuts import get_object_or_404
from crispy_forms.utils import render_field
from django import forms
from django.forms.widgets import TextInput
......@@ -456,8 +458,8 @@ class TemplateForm(forms.ModelForm):
data['owner'] = self.user.pk
self.data = data
if parent is not None:
template = InstanceTemplate.objects.get(pk=parent)
if parent is not None and parent.isdigit():
template = get_object_or_404(InstanceTemplate, pk=parent)
parent = template.__dict__
fields = ["system", "name", "num_cores", "boot_menu", "ram_size",
"priority", "access_method", "raw_data",
......@@ -497,11 +499,16 @@ class TemplateForm(forms.ModelForm):
def save(self, commit=True):
data = self.cleaned_data
self.instance.max_ram_size = data.get('ram_size')
is_new = self.instance.pk is None
instance = super(TemplateForm, self).save(commit=False)
if commit:
instance.save()
if is_new:
self.instance.disks = InstanceTemplate.objects.get(
pk=self.instance.parent.pk).disks.all()
# create and/or delete InterfaceTemplates
networks = InterfaceTemplate.objects.filter(
template=self.instance).values_list("vlan", flat=True)
......
......@@ -423,3 +423,25 @@ footer a, footer a:hover, footer a:visited {
#node-info-pane {
margin-bottom: 20px;
}
.template-choose-list {
max-width: 600px;
}
.template-choose-list-element {
cursor: pointer;
}
.template-choose-list-element small {
display: none;
float: right;
padding-right: 50px;
}
.template-choose-list-element {
padding: 15px;
}
.template-choose-list input[type="radio"] {
float: right;
}
{% load i18n %}
<form action="{% url "dashboard.views.template-create" %}">
<div class="template-choose-list">
{% for t in templates %}
<div class="panel panel-default template-choose-list-element">
<input type="radio" name="parent" value="{{ t.pk }}"/>
{{ t.name }} - {{ t.system }}
<small>Cores: {{ t.num_cores }} RAM: {{ t.ram_size }}</small>
<div class="clearfix"></div>
</div>
{% endfor %}
<div class="panel panel-default template-choose-list-element">
<input type="radio" name="parent" value="base_vm"/>
{% trans "Create a new base VM without disk" %}
</div>
<button type="submit" class="btn btn-success pull-right">{% trans "Next" %}</button>
<div class="clearfix"></div>
</div>
</form>
<script>
$(function() {
$(".template-choose-list-element").click(function() {
$("input", $(this)).prop("checked", true);
});
$(".template-choose-list-element").hover(
function() {
$("small", $(this)).stop().fadeIn(200);
},
function() {
$("small", $(this)).stop().fadeOut(200);
}
);
});
</script>
{% extends "dashboard/base.html" %}
{% load i18n %}
{% load crispy_forms_tags %}
{% block title-page %}{% trans "Create base VM" %}{% endblock %}
{% block content %}
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<a class="pull-right btn btn-default btn-xs" href="{% url "dashboard.views.template-list" %}">{% trans "Back" %}</a>
<h3 class="no-margin"><i class="icon-desktop"></i> {% trans "Create base VM" %}</h3>
</div>
<div class="panel-body">
{% with form=form %}
{% include "display-form-errors.html" %}
{% endwith %}
{% crispy form %}
</div>
</div>
</div>
</div>
{% with form=form %}
{% include "display-form-errors.html" %}
{% endwith %}
{% crispy form %}
<style>
fieldset {
......@@ -35,5 +19,3 @@
$("#hint_id_num_cores, #hint_id_priority, #hint_id_ram_size").hide();
});
</script>
{% endblock %}
......@@ -18,15 +18,16 @@
<p>
{% trans "You don't have any templates, however you can still start virtual machines and even save them as new templates!" %}
</p>
<p>
{% trans "The new button below creates a new base vm, please use only if necessary!" %}
</p>
</div>
{% endfor %}
<div href="#" class="list-group-item list-group-footer text-right">
<p>
<a href="{% url "dashboard.views.template-list" %}" class="btn btn-primary btn-xs"><i class="icon-chevron-sign-right"></i> {% trans "show all" %}</a>
<a href="{% url "dashboard.views.template-create" %}" class="btn btn-success btn-xs"><i class="icon-plus-sign"></i> {% trans "new" %}</a>
<a href="{% url "dashboard.views.template-list" %}" class="btn btn-primary btn-xs">
<i class="icon-chevron-sign-right"></i> {% trans "show all" %}
</a>
<a href="{% url "dashboard.views.template-choose" %}" class="btn btn-success btn-xs">
<i class="icon-plus-sign"></i> {% trans "new" %}
</a>
</p>
</div>
</div>
......
......@@ -11,7 +11,7 @@ from .views import (
TransferOwnershipConfirmView, TransferOwnershipView, vm_activity, VmCreate,
VmDelete, VmDetailView, VmDetailVncTokenView, VmGraphView, VmList,
VmMassDelete, VmMigrateView, VmRenewView, DiskRemoveView,
get_disk_download_status,
get_disk_download_status, TemplateChoose,
)
urlpatterns = patterns(
......@@ -26,6 +26,8 @@ urlpatterns = patterns(
url(r'^template/create/$', TemplateCreate.as_view(),
name="dashboard.views.template-create"),
url(r'^template/choose/$', TemplateChoose.as_view(),
name="dashboard.views.template-choose"),
url(r'template/(?P<pk>\d+)/acl/$', TemplateAclUpdateView.as_view(),
name='dashboard.views.template-acl'),
url(r'^template/(?P<pk>\d+)/$', TemplateDetail.as_view(),
......
......@@ -741,12 +741,51 @@ class GroupAclUpdateView(AclUpdateView):
kwargs=self.kwargs))
class TemplateChoose(TemplateView):
def get_template_names(self):
if self.request.is_ajax():
return ['dashboard/modal-wrapper.html']
else:
return ['dashboard/nojs-wrapper.html']
def get_context_data(self, *args, **kwargs):
context = super(TemplateChoose, self).get_context_data(*args, **kwargs)
templates = InstanceTemplate.get_objects_with_level("user",
self.request.user)
context.update({
'box_title': _('Choose template'),
'ajax_title': False,
'template': "dashboard/_template-create-1.html",
'templates': templates.all(),
})
return context
class TemplateCreate(SuccessMessageMixin, CreateView):
model = InstanceTemplate
form_class = TemplateForm
template_name = "dashboard/template-create.html"
success_message = _("Successfully created a new template!")
def get_template_names(self):
if self.request.is_ajax():
return ['dashboard/modal-wrapper.html']
else:
return ['dashboard/nojs-wrapper.html']
def get_context_data(self, *args, **kwargs):
context = super(TemplateCreate, self).get_context_data(*args, **kwargs)
context.update({
'box_title': (_('Clone a template')
if self.request.GET.get("parent").isdigit()
else _("Create a new base VM")),
'ajax_title': False,
'template': "dashboard/_template-create-2.html",
})
return context
def get(self, *args, **kwargs):
if not self.request.user.has_perm('vm.create_template'):
raise PermissionDenied()
......@@ -767,7 +806,7 @@ class TemplateCreate(SuccessMessageMixin, CreateView):
form = self.form_class(request.POST, user=request.user)
if not form.is_valid():
return self.get(request, form, *args, **kwargs)
else:
elif form.cleaned_data.get("parent") is None:
post = form.cleaned_data
networks = self.__create_networks(post.pop("networks"))
......
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