Commit 7278c20e by Kálmán Viktor Committed by Guba Sándor

dashboard: create diskless vm from new template

parent dba6a664
...@@ -451,10 +451,6 @@ class TemplateForm(forms.ModelForm): ...@@ -451,10 +451,6 @@ class TemplateForm(forms.ModelForm):
parent = kwargs.pop("parent", None) parent = kwargs.pop("parent", None)
self.user = kwargs.pop("user", None) self.user = kwargs.pop("user", None)
super(TemplateForm, self).__init__(*args, **kwargs) super(TemplateForm, self).__init__(*args, **kwargs)
self.fields['disks'] = forms.ModelMultipleChoiceField(
queryset=Disk.get_objects_with_level(
'user', self.user).exclude(type="qcow2-snap")
)
data = self.data.copy() data = self.data.copy()
data['owner'] = self.user.pk data['owner'] = self.user.pk
...@@ -469,7 +465,6 @@ class TemplateForm(forms.ModelForm): ...@@ -469,7 +465,6 @@ class TemplateForm(forms.ModelForm):
for f in fields: for f in fields:
self.initial[f] = parent[f] self.initial[f] = parent[f]
self.initial['lease'] = parent['lease_id'] self.initial['lease'] = parent['lease_id']
self.initial['disks'] = template.disks.all()
self.initial['parent'] = template self.initial['parent'] = template
self.initial['name'] = "Clone of %s" % self.initial['name'] self.initial['name'] = "Clone of %s" % self.initial['name']
self.for_networks = template self.for_networks = template
...@@ -507,8 +502,6 @@ class TemplateForm(forms.ModelForm): ...@@ -507,8 +502,6 @@ class TemplateForm(forms.ModelForm):
if commit: if commit:
instance.save() instance.save()
self.instance.disks = data['disks'] # TODO why do I need this
# create and/or delete InterfaceTemplates # create and/or delete InterfaceTemplates
networks = InterfaceTemplate.objects.filter( networks = InterfaceTemplate.objects.filter(
template=self.instance).values_list("vlan", flat=True) template=self.instance).values_list("vlan", flat=True)
...@@ -527,6 +520,7 @@ class TemplateForm(forms.ModelForm): ...@@ -527,6 +520,7 @@ class TemplateForm(forms.ModelForm):
kwargs_raw_data = {} kwargs_raw_data = {}
if not self.user.is_superuser: if not self.user.is_superuser:
kwargs_raw_data['readonly'] = None kwargs_raw_data['readonly'] = None
helper = FormHelper() helper = FormHelper()
helper.layout = Layout( helper.layout = Layout(
Field("name"), Field("name"),
...@@ -586,7 +580,6 @@ class TemplateForm(forms.ModelForm): ...@@ -586,7 +580,6 @@ class TemplateForm(forms.ModelForm):
), ),
Fieldset( Fieldset(
_("External"), _("External"),
Field("disks"),
Field("networks"), Field("networks"),
Field("lease"), Field("lease"),
Field("tags"), Field("tags"),
...@@ -597,7 +590,7 @@ class TemplateForm(forms.ModelForm): ...@@ -597,7 +590,7 @@ class TemplateForm(forms.ModelForm):
class Meta: class Meta:
model = InstanceTemplate model = InstanceTemplate
exclude = ('state', ) exclude = ('state', 'disks', )
class LeaseForm(forms.ModelForm): class LeaseForm(forms.ModelForm):
......
...@@ -768,13 +768,29 @@ class TemplateCreate(SuccessMessageMixin, CreateView): ...@@ -768,13 +768,29 @@ class TemplateCreate(SuccessMessageMixin, CreateView):
form = self.form_class(request.POST, user=request.user) form = self.form_class(request.POST, user=request.user)
if not form.is_valid(): if not form.is_valid():
return self.get(request, form, *args, **kwargs) return self.get(request, form, *args, **kwargs)
else:
post = form.cleaned_data post = form.cleaned_data
for disk in post['disks']:
if not disk.has_level(request.user, 'user'): networks = self.__create_networks(post.pop("networks"))
raise PermissionDenied() req_traits = post.pop("req_traits")
tags = post.pop("tags")
post['pw'] = User.objects.make_random_password()
post.pop("parent")
inst = Instance.create(params=post, disks=[], networks=networks,
tags=tags, req_traits=req_traits)
messages.success(request, _("Your disk has been created, "
"you can now add disks to it!"))
return redirect("%s#resources" % inst.get_absolute_url())
return super(TemplateCreate, self).post(self, request, args, kwargs) return super(TemplateCreate, self).post(self, request, args, kwargs)
def __create_networks(self, vlans):
networks = []
for v in vlans:
networks.append(InterfaceTemplate(vlan=v, managed=v.managed))
return networks
def get_success_url(self): def get_success_url(self):
return reverse_lazy("dashboard.views.template-list") return reverse_lazy("dashboard.views.template-list")
......
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