Commit 0c61f4e0 by Őry Máté

Merge branch 'issue-301' into 'master'

Default values in vm-op forms

closes #301

See merge request !232
parents faaace05 de78a338
......@@ -18,6 +18,7 @@
from __future__ import absolute_import
from datetime import timedelta
from urlparse import urlparse
from django.contrib.auth.forms import (
AuthenticationForm, PasswordResetForm, SetPasswordForm,
......@@ -79,6 +80,12 @@ class VmSaveForm(forms.Form):
helper.form_tag = False
return helper
def __init__(self, *args, **kwargs):
default = kwargs.pop('default', None)
super(VmSaveForm, self).__init__(*args, **kwargs)
if default:
self.fields['name'].initial = default
class VmCustomizeForm(forms.Form):
name = forms.CharField(widget=forms.TextInput(attrs={
......@@ -788,6 +795,12 @@ class VmCreateDiskForm(forms.Form):
help_text=_('Size of disk to create in bytes or with units '
'like MB or GB.'))
def __init__(self, *args, **kwargs):
default = kwargs.pop('default', None)
super(VmCreateDiskForm, self).__init__(*args, **kwargs)
if default:
self.fields['name'].initial = default
def clean_size(self):
size_in_bytes = self.cleaned_data.get("size")
if not size_in_bytes.isdigit() and len(size_in_bytes) > 0:
......@@ -845,7 +858,7 @@ class VmDiskResizeForm(forms.Form):
class VmDownloadDiskForm(forms.Form):
name = forms.CharField(max_length=100, label=_("Name"))
name = forms.CharField(max_length=100, label=_("Name"), required=False)
url = forms.CharField(label=_('URL'), validators=[URLValidator(), ])
@property
......@@ -854,6 +867,18 @@ class VmDownloadDiskForm(forms.Form):
helper.form_tag = False
return helper
def clean(self):
cleaned_data = super(VmDownloadDiskForm, self).clean()
if not cleaned_data['name']:
if cleaned_data['url']:
cleaned_data['name'] = urlparse(
cleaned_data['url']).path.split('/')[-1]
if not cleaned_data['name']:
raise forms.ValidationError(
_("Could not find filename in URL, "
"please specify a name explicitly."))
return cleaned_data
class VmAddInterfaceForm(forms.Form):
def __init__(self, *args, **kwargs):
......
......@@ -219,6 +219,7 @@ class VmOperationViewTestCase(unittest.TestCase):
with patch.object(view, 'get_object') as go, \
patch('dashboard.views.util.messages') as msg:
inst = MagicMock(spec=Instance)
inst.name = "asd"
inst._meta.object_name = "Instance"
inst.save_as_template = Instance._ops['save_as_template'](inst)
inst.save_as_template.async = MagicMock()
......@@ -235,6 +236,7 @@ class VmOperationViewTestCase(unittest.TestCase):
with patch.object(view, 'get_object') as go, \
patch('dashboard.views.util.messages') as msg:
inst = MagicMock(spec=Instance)
inst.name = "asd"
inst._meta.object_name = "Instance"
inst.save_as_template = Instance._ops['save_as_template'](inst)
inst.save_as_template.async = MagicMock()
......
......@@ -403,6 +403,13 @@ class VmCreateDiskView(FormOperationMixin, VmOperationView):
effect = "success"
is_disk_operation = True
def get_form_kwargs(self):
op = self.get_op()
val = super(VmCreateDiskView, self).get_form_kwargs()
num = op.instance.disks.count() + 1
val['default'] = "%s %d" % (op.instance.name, num)
return val
class VmDownloadDiskView(FormOperationMixin, VmOperationView):
......@@ -453,6 +460,12 @@ class VmSaveView(FormOperationMixin, VmOperationView):
effect = 'info'
form_class = VmSaveForm
def get_form_kwargs(self):
op = self.get_op()
val = super(VmSaveView, self).get_form_kwargs()
val['default'] = op._rename(op.instance.name)
return val
class VmResourcesChangeView(VmOperationView):
op = 'resources_change'
......
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