Commit 3743cec7 by Kálmán Viktor

dashboard: better way to handle disk add origin

parent df4f36a3
......@@ -729,14 +729,14 @@ class DiskAddForm(forms.Form):
name = forms.CharField()
size = forms.CharField(widget=FileSizeWidget, required=False)
url = forms.CharField(required=False)
add_to = forms.CharField()
is_template = forms.CharField()
object_pk = forms.CharField()
def __init__(self, *args, **kwargs):
self.add_to = kwargs.pop("add_to")
self.is_template = kwargs.pop("is_template")
self.object_pk = kwargs.pop("object_pk")
super(DiskAddForm, self).__init__(*args, **kwargs)
self.initial['add_to'] = self.add_to
self.initial['is_template'] = 1 if self.is_template is True else 0
self.initial['object_pk'] = self.object_pk
def clean_size(self):
......@@ -773,7 +773,7 @@ class DiskAddForm(forms.Form):
# TODO
d = None
if self.add_to == "template":
if self.is_template:
vm_or_temp = InstanceTemplate.objects.get(pk=self.object_pk)
else:
vm_or_temp = Instance.objects.get(pk=self.object_pk)
......@@ -787,7 +787,7 @@ class DiskAddForm(forms.Form):
helper = FormHelper()
helper.form_show_labels = False
helper.layout = Layout(
Field("add_to", type="hidden"),
Field("is_template", type="hidden"),
Field("object_pk", type="hidden"),
Field("name", placeholder=_("Name")),
Field("size", placeholder=_("Disk size (for example: 20GB, "
......
......@@ -181,7 +181,7 @@ class VmDetailView(CheckedDetailView):
context['acl'] = get_vm_acl_data(instance)
context['forms'] = {
'disk_add_form': DiskAddForm(
add_to="instance", object_pk=self.get_object().pk,
is_template=False, object_pk=self.get_object().pk,
prefix="disk"),
}
context['os_type_icon'] = instance.os_type.replace("unknown",
......@@ -733,7 +733,7 @@ class TemplateDetail(LoginRequiredMixin, SuccessMessageMixin, UpdateView):
context = super(TemplateDetail, self).get_context_data(**kwargs)
context['acl'] = get_vm_acl_data(self.get_object())
context['disk_add_form'] = DiskAddForm(
add_to="template",
is_template=True,
object_pk=self.get_object().pk,
prefix="disk",
)
......@@ -1711,11 +1711,20 @@ class VmMigrateView(SuperuserRequiredMixin, TemplateView):
class DiskAddView(SuperuserRequiredMixin, TemplateView):
def post(self, *args, **kwargs):
add_to = self.request.POST.get("disk-add_to")
is_template = self.request.POST.get("disk-is_template")
object_pk = self.request.POST.get("disk-object_pk")
is_template = int(is_template) == 1
if is_template:
obj = InstanceTemplate.objects.get(pk=object_pk)
else:
obj = Instance.objects.get(pk=object_pk)
if not obj.has_level(self.request.user, 'owner'):
raise PermissionDenied()
form = DiskAddForm(
self.request.POST,
add_to=add_to, object_pk=object_pk,
is_template=is_template, object_pk=object_pk,
prefix="disk"
)
......@@ -1728,9 +1737,9 @@ class DiskAddView(SuperuserRequiredMixin, TemplateView):
for i in form.errors.items()])
messages.error(self.request, error)
if add_to == "template":
r = InstanceTemplate.objects.get(pk=object_pk).get_absolute_url()
if is_template:
r = obj.get_absolute_url()
else:
r = Instance.objects.get(pk=object_pk).get_absolute_url()
r = obj.get_absolute_url()
r = "%s#resources" % r
return redirect(r)
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