Commit f42dd1df by Kálmán Viktor

dashboard: fix base vm creation

parent 1768643d
......@@ -30,7 +30,6 @@ 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
......@@ -479,10 +478,8 @@ class NodeForm(forms.ModelForm):
class TemplateForm(forms.ModelForm):
networks = forms.ModelMultipleChoiceField(
queryset=None, required=False, label=_("Networks"))
parent_type = forms.CharField(required=False)
def __init__(self, *args, **kwargs):
self.parent = kwargs.pop("parent", None)
self.user = kwargs.pop("user", None)
super(TemplateForm, self).__init__(*args, **kwargs)
......@@ -492,25 +489,9 @@ class TemplateForm(forms.ModelForm):
data = self.data.copy()
data['owner'] = self.user.pk
self.data = data
self.initial['parent_type'] = self.parent
if self.parent is not None and self.parent.isdigit():
template = get_object_or_404(InstanceTemplate, pk=self.parent)
parent = template.__dict__
fields = ["system", "name", "num_cores", "boot_menu", "ram_size",
"priority", "access_method", "raw_data",
"arch", "description"]
for f in fields:
self.initial[f] = parent[f]
self.initial['lease'] = parent['lease_id']
self.initial['parent'] = template
self.initial['name'] = "Clone of %s" % self.initial['name']
self.for_networks = template
else:
self.for_networks = self.instance
if self.instance.pk or self.parent is not None:
n = self.for_networks.interface_set.values_list("vlan", flat=True)
if self.instance.pk:
n = self.instance.interface_set.values_list("vlan", flat=True)
self.initial['networks'] = n
if not self.instance.pk and len(self.errors) < 1:
......@@ -561,7 +542,6 @@ class TemplateForm(forms.ModelForm):
helper = FormHelper()
helper.layout = Layout(
Field("parent_type", type="hidden"),
Field("name"),
Fieldset(
_("Resource configuration"),
......
......@@ -841,7 +841,7 @@ class TemplateChoose(TemplateView):
context.update({
'box_title': _('Choose template'),
'ajax_title': False,
'template': "dashboard/_template-create-1.html",
'template': "dashboard/_template-choose.html",
'templates': templates.all(), # TODO acl?
})
return context
......@@ -871,7 +871,7 @@ class TemplateCreate(SuccessMessageMixin, CreateView):
def get_template_names(self):
if self.request.is_ajax():
return ['dashboard/modal-wrapper.html']
pass
else:
return ['dashboard/nojs-wrapper.html']
......@@ -880,8 +880,7 @@ class TemplateCreate(SuccessMessageMixin, CreateView):
context.update({
'box_title': _("Create a new base VM"),
'ajax_title': False,
'template': "dashboard/_template-create-2.html",
'template': "dashboard/_template-create.html",
'leases': Lease.objects.count()
})
return context
......@@ -890,12 +889,10 @@ class TemplateCreate(SuccessMessageMixin, CreateView):
if not self.request.user.has_perm('vm.create_template'):
raise PermissionDenied()
self.parent = self.request.GET.get("parent")
return super(TemplateCreate, self).get(*args, **kwargs)
def get_form_kwargs(self):
kwargs = super(TemplateCreate, self).get_form_kwargs()
kwargs['parent'] = getattr(self, "parent", None)
kwargs['user'] = self.request.user
return kwargs
......@@ -908,37 +905,17 @@ class TemplateCreate(SuccessMessageMixin, CreateView):
return self.get(request, form, *args, **kwargs)
else:
post = form.cleaned_data
networks = self.__create_networks(post.pop("networks"),
request.user)
post.pop("parent")
parent_type = post.pop("parent_type")
post['max_ram_size'] = post['ram_size']
# if it's not a base vm we need to add disks and deploy it
if parent_type != "base_vm":
template = get_object_or_404(InstanceTemplate, pk=parent_type)
inst = Instance.create_from_template(template=template,
networks=networks,
**post)
inst.deploy_async()
messages.info(request, _("Your instance has been created, "
"modify it then press the save as "
"button to save it as a new template!"
))
else:
req_traits = post.pop("req_traits")
tags = post.pop("tags")
post['pw'] = User.objects.make_random_password()
post['is_base'] = True
inst = Instance.create(params=post, disks=[],
networks=networks,
tags=tags, req_traits=req_traits)
messages.info(request, _("Your new base vm has been created, "
"add disks, make modifications, then"
" use the save as button to save it"
" as a new template!"
))
return redirect("%s#resources" % inst.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