cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
system_image.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 ##
21 # @package src.wi.forms.system_image
22 #
23 # @author Krzysztof Danielowski
24 # @author Piotr Wójcik
25 #
26 
27 from django import forms
28 from django.utils.translation import ugettext_lazy as _
29 
30 from wi.utils import parsing
31 from wi.utils.forms import attrs_dict, BetterForm, RequiredSelectValidation
32 from wi.utils.states import image_platforms_reversed
33 from wi.utils.widgets import SelectWithDisabled
34 
35 
36 ##
37 #
38 # Form for <b>assigning image to group</b>.
39 #
40 class AssignGroupForm(RequiredSelectValidation):
41  def __init__(self, *args, **kwargs):
42  rest_data = kwargs.pop('rest_data')
43  super(AssignGroupForm, self).__init__(*args, **kwargs)
44  self.fields['group_id'] = forms.ChoiceField(choices=parsing.parse_groups(rest_data), initial=0,
45  label=_("Group name"))
46  self.fields['group_id'].widget.attrs['class'] = 'medium'
47 
48  ##
49  #
50  # Cast 'group_id' to int.
51  #
52  def clean_group_id(self):
53  return int(self.cleaned_data['group_id'])
54 
55 
56 ##
57 #
58 # Form for <b>image's edition</b>.
59 #
60 class EditImageForm(BetterForm):
61  ##
62  #
63  # Fieldset names definition.
64  #
65  class Meta:
66  fieldsets = (('description', {'fields': ('name', 'description'), 'legend': _('Images description')}),
67  ('settings', {'fields': ('platform',), 'legend': _('Settings')}),
68  ('advanced', {'fields': ('video_device', 'network_device', 'disk_controller'), 'description': 'advanced', 'legend': _('Advanced settings')}),)
69 
70  def __init__(self):
71  pass
72 
73  name = forms.CharField(widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=45)),
74  label=_('Name'))
75  description = forms.CharField(required=False,
76  widget=forms.Textarea(attrs=dict(attrs_dict, maxlength=512, rows=3, cols=20)),
77  label=_('Description'))
78 
79  def __init__(self, *args, **kwargs):
80  rest_data = kwargs.pop('rest_data')
81  super(EditImageForm, self).__init__(*args, **kwargs)
82  self.fields['platform'] = forms.ChoiceField(choices=image_platforms_reversed.items(),
83  label=_('Platform'))
84  self.fields['platform'].widget.attrs['class'] = 'medium'
85 
86  self.fields['disk_controller'] = forms.ChoiceField(choices=parsing.parse_generic_enabled(rest_data, 'disk_controllers'),
87  widget=SelectWithDisabled(attrs=dict()),
88  label=_('Bus'))
89  self.fields['disk_controller'].widget.attrs['class'] = 'medium'
90 
91  self.fields['video_device'] = forms.ChoiceField(choices=parsing.parse_generic(rest_data, 'video_devices'),
92  widget=SelectWithDisabled(attrs=dict()),
93  label=_('Video'))
94  self.fields['video_device'].widget.attrs['class'] = 'medium'
95 
96  self.fields['network_device'] = forms.ChoiceField(choices=parsing.parse_generic(rest_data, 'network_devices'),
97  widget=SelectWithDisabled(attrs=dict()),
98  label=_("Net"))
99  self.fields['network_device'].widget.attrs['class'] = 'medium'
100 
101  self.fields.keyOrder = ['name', 'description', 'platform', 'disk_controller', 'video_device', 'network_device']
102 
103  ##
104  #
105  # Cast 'disk_controller' to int.
106  #
108  return int(self.cleaned_data['disk_controller'])
109 
110  ##
111  #
112  # Cast 'video_device' to int.
113  #
115  return int(self.cleaned_data['video_device'])
116 
117  ##
118  #
119  # Cast 'network_device' to int.
120  #
122  return int(self.cleaned_data['network_device'])
123 
124  ##
125  #
126  # Cast 'platform' to int.
127  #
128  def clean_platform(self):
129  return int(self.cleaned_data['platform'])
130 
131 
132 ##
133 #
134 # Form for <b>adding HTTP to image</b>.
135 #
136 class AddImageHttp(EditImageForm, BetterForm):
137  ##
138  #
139  # Fieldset names definition.
140  #
141  class Meta:
142  fieldsets = (('description', {'fields': ('name', 'description'), 'legend': _('Images description')}),
143  ('settings', {'fields': ('platform', 'path'), 'legend': _('Settings')}),
144  ('advanced', {'fields': ('video_device', 'network_device', 'disk_controller'), 'description': 'advanced', 'legend': _('Advanced settings')}),)
145 
146  def __init__(self):
147  pass
148 
149  def __init__(self, *args, **kwargs):
150  super(AddImageHttp, self).__init__(*args, **kwargs)
151  self.fields['path'] = forms.CharField(widget=forms.Textarea(attrs=dict(attrs_dict, maxlength=500, rows=3, cols=20)),
152  label=_("Link to image (http:// or ftp://)"))
153 
154  self.fields.keyOrder = ['name', 'description', 'path', 'platform',
155  'disk_controller', 'video_device',
156  'network_device']
157