Commit d739baa2 by Kálmán Viktor

dashboard: distiguish base vms and vms started from template

parent 3de47528
......@@ -450,18 +450,20 @@ class TemplateForm(forms.ModelForm):
networks = forms.ModelMultipleChoiceField(
queryset=VLANS, required=False)
system = forms.CharField(widget=forms.TextInput)
parent_type = forms.CharField(required=False)
def __init__(self, *args, **kwargs):
parent = kwargs.pop("parent", None)
self.parent = kwargs.pop("parent", None)
self.user = kwargs.pop("user", None)
super(TemplateForm, self).__init__(*args, **kwargs)
data = self.data.copy()
data['owner'] = self.user.pk
self.data = data
self.initial['parent_type'] = self.parent
if parent is not None and parent.isdigit():
template = get_object_or_404(InstanceTemplate, pk=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",
......@@ -475,7 +477,7 @@ class TemplateForm(forms.ModelForm):
else:
self.for_networks = self.instance
if self.instance.pk or parent is not None:
if self.instance.pk or self.parent is not None:
n = self.for_networks.interface_set.values_list("vlan", flat=True)
self.initial['networks'] = n
......@@ -501,16 +503,11 @@ 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)
......@@ -532,6 +529,7 @@ class TemplateForm(forms.ModelForm):
helper = FormHelper()
helper.layout = Layout(
Field("parent_type", type="hidden"),
Field("name"),
Fieldset(
_("Resource configuration"),
......
......@@ -842,11 +842,19 @@ class TemplateCreate(SuccessMessageMixin, CreateView):
tags = post.pop("tags")
post['pw'] = User.objects.make_random_password()
post.pop("parent")
parent_type = post.pop("parent_type")
post['max_ram_size'] = post['ram_size']
inst = Instance.create(params=post, disks=[], networks=networks,
tags=tags, req_traits=req_traits)
messages.success(request, _("The template has been created, "
"you can now add disks to it!"))
# 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)
# TODO clone disks
inst.deploy_async()
return redirect("%s#resources" % inst.get_absolute_url())
return super(TemplateCreate, self).post(self, request, args, kwargs)
......
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