28 from itertools
import chain
30 from django
import forms
31 from django.forms.fields
import CheckboxInput
32 from django.utils.encoding
import force_unicode
33 from django.utils.html
import conditional_escape
34 from django.utils.safestring
import mark_safe
35 from django.utils.translation
import ugettext_lazy
as _
37 from wi.utils
import parsing
38 from wi.utils.forms
import attrs_dict, RequiredSelectValidation
39 from wi.utils.regexp
import regexp, regexp_text
40 from wi.utils.states
import stat_names_reversed, stat_short_names_reversed, stat_resolutions_reversed, stat_ranges_reversed
41 from wi.utils.widgets
import SelectWithDisabled, CheckboxSelectMultipleWithDisabled
45 def render(self, name, value, attrs=None, choices=()):
48 has_id = attrs
and 'id' in attrs
49 final_attrs = self.build_attrs(attrs, name=name)
52 str_values = set([force_unicode(v)
for v
in value])
53 for i, (option_value, option_label)
in enumerate(chain(self.choices, choices)):
57 final_attrs = dict(final_attrs, id=
'%s_%s' % (attrs[
'id'], i))
58 label_for =
u' for="%s"' % final_attrs[
'id']
62 cb = CheckboxInput(final_attrs, check_test=
lambda value: value
in str_values)
63 option_value = force_unicode(option_value)
64 rendered_cb = cb.render(name, option_value)
65 option_label = conditional_escape(force_unicode(option_label))
66 output.append(
u'<li class="stats ' + stat_short_names_reversed[int(option_value)] +
'"><label%s>%s %s</label></li>' % (label_for, rendered_cb, option_label))
67 output.append(
u'</ul>')
68 return mark_safe(
u'\n'.join(output))
77 name = forms.CharField(widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=45)),
79 description = forms.CharField(required=
False,
80 widget=forms.Textarea(attrs=dict(attrs_dict, maxlength=512, rows=4, cols=20)),
81 label=_(
'Description'))
89 vm_username = forms.RegexField(regex=regexp[
'login'],
91 widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=35)),
92 error_messages={
'invalid': regexp_text[
'login']},
96 rest_data = kwargs.pop(
'rest_data')
97 super(SetSshKeyForm, self).
__init__(*args, **kwargs)
98 self.fields[
'vm_key'] = forms.ChoiceField(choices=parsing.parse_ssh_keys(rest_data), initial=0, label=_(
'Key'))
99 self.fields[
'vm_key'].widget.attrs[
'class'] =
'medium'
106 return int(self.cleaned_data[
'vm_key'])
114 vm_username = forms.RegexField(regex=regexp[
'login'],
116 widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=35)),
117 error_messages={
'invalid': regexp_text[
'login']},
128 rest_data = kwargs.pop(
'rest_data')
129 super(AssignChosenIPForm, self).
__init__(*args, **kwargs)
130 self.fields[
'public_ip_id'] = forms.ChoiceField(choices=parsing.parse_ips(rest_data,
False), initial=0,
131 widget=SelectWithDisabled(attrs=dict({
'class':
'medium'})),
132 label=_(
"IP address"))
133 self.fields[
'public_ip_id'].widget.attrs[
'class'] =
'medium'
140 return int(self.cleaned_data[
'public_ip_id'])
150 rest_data = kwargs.pop(
'rest_data')
151 super(AssignIPForm, self).
__init__(*args, **kwargs)
152 self.fields[
'public_ip_id'] = forms.ChoiceField(choices=parsing.parse_ips(rest_data,
False), initial=0,
153 widget=SelectWithDisabled(attrs=dict({
'class':
'medium'})),
154 label=_(
"IP address"))
155 self.fields[
'public_ip_id'].widget.attrs[
'class'] =
'medium'
156 self.fields[
'lease_id'] = forms.ChoiceField(choices=parsing.parse_leases(rest_data), initial=0,
157 widget=SelectWithDisabled(attrs=dict({
'class':
'medium'})),
159 self.fields[
'lease_id'].widget.attrs[
'class'] =
'medium'
166 return int(self.cleaned_data[
'lease_id'])
173 return int(self.cleaned_data[
'public_ip_id'])
183 rest_data = kwargs.pop(
'rest_data')
184 super(RevokeIPForm, self).
__init__(*args, **kwargs)
185 self.fields[
'public_ip_id'] = forms.ChoiceField(choices=parsing.parse_ips_from_vm(rest_data), initial=0,
186 widget=SelectWithDisabled(attrs=dict({
'class':
'medium'})),
187 label=_(
"IP address"))
188 self.fields[
'public_ip_id'].widget.attrs[
'class'] =
'medium'
195 return int(self.cleaned_data[
'public_ip_id'])
205 rest_data = kwargs.pop(
'rest_data')
206 super(AssignDiskForm, self).
__init__(*args, **kwargs)
207 self.fields[
'storage_image_id'] = forms.ChoiceField(choices=parsing.parse_disks(rest_data,
False), initial=0,
208 widget=SelectWithDisabled(attrs=dict({
'class':
'medium'})),
210 self.fields[
'storage_image_id'].widget.attrs[
'class'] =
'medium'
217 return int(self.cleaned_data[
'storage_image_id'])
227 rest_data = kwargs.pop(
'rest_data')
228 super(RevokeDiskForm, self).
__init__(*args, **kwargs)
229 self.fields[
'storage_image_id'] = forms.ChoiceField(choices=parsing.parse_disks_from_vm(rest_data), initial=0,
230 widget=SelectWithDisabled(attrs=dict({
'class':
'medium'})),
232 self.fields[
'storage_image_id'].widget.attrs[
'class'] =
'medium'
239 return int(self.cleaned_data[
'storage_image_id'])
248 image_id = forms.CharField(widget=forms.HiddenInput())
251 super(CreateVMForm1, self).
__init__(*args, **kwargs)
258 if int(self.cleaned_data[
'image_id']) < 0:
259 raise forms.ValidationError(_(
'Please select an image.'))
261 return int(self.cleaned_data[
'image_id'])
270 template_id = forms.CharField(widget=forms.HiddenInput())
273 super(CreateVMForm2, self).
__init__(*args, **kwargs)
280 if int(self.cleaned_data[
'template_id']) < 0:
281 raise forms.ValidationError(_(
'Please select a template.'))
283 return int(self.cleaned_data[
'template_id'])
293 rest_data = kwargs.pop(
'rest_data')
294 super(CreateVMForm3, self).
__init__(*args, **kwargs)
296 self.fields[
'public_ip_id'] = forms.ChoiceField(choices=parsing.parse_ips(rest_data),
298 widget=SelectWithDisabled(attrs=dict()),
299 label=_(
'Assign IP address'),
300 help_text=_(
'Public IP address - To get a new IP address go to: VM Resources -> Elastic IP addresses'))
302 self.fields[
'disk_list'] = forms.MultipleChoiceField(choices=parsing.parse_disks(rest_data,
True),
304 widget=CheckboxSelectMultipleWithDisabled,
305 label=_(
'Attach disk volume'),
306 help_text=_(
'Virtual disk - '))
308 self.fields[
'iso_list'] = forms.ChoiceField(choices=parsing.parse_iso(rest_data),
310 widget=SelectWithDisabled(attrs=dict()),
311 label=_(
"Attach ISO image"))
313 self.fields[
'vnc'] = forms.BooleanField(required=
False,
315 widget=forms.CheckboxInput(attrs={
'class':
'checkbox'}),
316 help_text=_(
'VNC - Enable/Disable VNC redirection'))
318 self.fields.keyOrder = [
'public_ip_id',
'disk_list',
'iso_list',
'vnc']
325 return int(self.cleaned_data[
'public_ip_id'])
332 for i
in self.cleaned_data[
'disk_list']:
335 return [int(a)
for a
in self.cleaned_data[
'disk_list']]
342 if int(self.cleaned_data[
'iso_list']) < 0:
344 return [int(self.cleaned_data[
'iso_list'])]
354 super(CreateVMForm4, self).
__init__(*args, **kwargs)
355 self.fields.keyOrder = [
'name',
'description']
364 vm_id = forms.CharField(widget=forms.HiddenInput())
365 time = forms.CharField(widget=forms.HiddenInput())
368 super(MonitoringVMForm, self).
__init__(*args, **kwargs)
370 self.fields[
'stat_name'] = forms.MultipleChoiceField(choices=stat_names_reversed.items(),
371 widget=My2CheckboxSelectMultiple,
372 label=_(
'Statistic'),
374 self.fields[
'resolution'] = forms.ChoiceField(choices=stat_resolutions_reversed.items(),
375 label=_(
'Time range'))
376 self.fields[
'resolution'].widget.attrs[
'class'] =
'small'
378 self.fields[
'stat_range'] = forms.ChoiceField(choices=stat_ranges_reversed.items(),
380 self.fields[
'stat_range'].widget.attrs[
'class'] =
'small'
387 return int(self.cleaned_data[
'vm_id'])
394 return [int(i)
for i
in self.cleaned_data[
'stat_name']]
401 return int(self.cleaned_data[
'resolution'])
408 return int(self.cleaned_data[
'stat_range'])
419 rest_data = kwargs[
'rest_data']
420 super(CreateVMOnNodeForm, self).
__init__(*args, **kwargs)
422 self.fields[
'image_id'] = forms.ChoiceField(choices=parsing.parse_image_names(rest_data),
423 widget=SelectWithDisabled(attrs=dict()),
425 help_text=_(
'Image file - From your public, private or group image pool'))
427 self.fields[
'template_id'] = forms.ChoiceField(choices=parsing.parse_template_names(rest_data),
428 widget=forms.Select(attrs=dict()),
430 help_text=_(
'Template of machine - Virtual machine parameters'))
432 self.fields.keyOrder = [
'image_id',
'name',
'description',
433 'template_id',
'public_ip_id',
'disk_list',
441 if int(self.cleaned_data[
'image_id']) < 0:
442 raise forms.ValidationError(_(
"Please select an image."))
444 return int(self.cleaned_data[
'image_id'])
451 if int(self.cleaned_data[
'template_id']) < 0:
452 raise forms.ValidationError(_(
"Please select a template."))
454 return int(self.cleaned_data[
'template_id'])