45
INVALID_ARCHITECTURE_MESSAGE = (
46
"%(value)s is not a valid architecture. " +
47
"It should be one of: %s." % ", ".join(
48
name for name, value in ARCHITECTURE_CHOICES))
37
51
class NodeForm(ModelForm):
38
52
system_id = forms.CharField(
39
53
widget=forms.TextInput(attrs={'readonly': 'readonly'}),
41
55
after_commissioning_action = forms.TypedChoiceField(
42
56
choices=NODE_AFTER_COMMISSIONING_ACTION_CHOICES, required=False,
43
57
empty_value=NODE_AFTER_COMMISSIONING_ACTION.DEFAULT)
58
architecture = forms.ChoiceField(
59
choices=ARCHITECTURE_CHOICES, required=False,
60
error_messages={'invalid_choice': INVALID_ARCHITECTURE_MESSAGE})
47
fields = ('hostname', 'system_id', 'after_commissioning_action')
50
class MACAddressForm(ModelForm):
65
'hostname', 'system_id', 'after_commissioning_action',
55
69
class MACAddressForm(ModelForm):
60
74
class MultipleMACAddressField(forms.MultiValueField):
61
75
def __init__(self, nb_macs=1, *args, **kwargs):
62
fields = [MACAddressFormField() for i in xrange(nb_macs)]
76
fields = [MACAddressFormField() for i in range(nb_macs)]
63
77
super(MultipleMACAddressField, self).__init__(fields, *args, **kwargs)
65
79
def compress(self, data_list):
99
113
class ProfileForm(ModelForm):
114
# We use the field 'last_name' to store the user's full name (and
115
# don't display Django's 'first_name' field).
116
last_name = forms.CharField(
117
label="Full name", max_length=30, required=False)
102
fields = ('first_name', 'last_name', 'email')
121
fields = ('last_name', 'email')
105
124
class NewUserCreationForm(UserCreationForm):
106
# Add is_superuser field.
107
125
is_superuser = forms.BooleanField(
108
label="Administrator status", required=False)
126
label="MaaS administrator", required=False)
128
def __init__(self, *args, **kwargs):
129
super(NewUserCreationForm, self).__init__(*args, **kwargs)
130
# Insert 'last_name' field at the right place (right after
131
# the 'username' field).
134
forms.CharField(label="Full name", max_length=30, required=False))
135
# Insert 'email' field at the right place (right after
136
# the 'last_name' field).
140
label="E-mail address", max_length=75, required=False))
110
142
def save(self, commit=True):
111
143
user = super(NewUserCreationForm, self).save(commit=False)
112
144
if self.cleaned_data.get('is_superuser', False):
113
145
user.is_superuser = True
146
new_last_name = self.cleaned_data.get('last_name', None)
147
if new_last_name is not None:
148
user.last_name = new_last_name
149
new_email = self.cleaned_data.get('email', None)
150
if new_email is not None:
151
user.email = new_email
118
156
class EditUserForm(UserChangeForm):
119
157
# Override the default label.
120
158
is_superuser = forms.BooleanField(
121
label="Administrator status", required=False)
159
label="MaaS administrator", required=False)
160
last_name = forms.CharField(
161
label="Full name", max_length=30, required=False)
126
'username', 'first_name', 'last_name', 'email', 'is_superuser')
166
'username', 'last_name', 'email', 'is_superuser')
129
169
class ConfigForm(Form):
134
174
def __init__(self, *args, **kwargs):
135
175
super(ConfigForm, self).__init__(*args, **kwargs)
136
176
if 'initial' not in kwargs:
137
configs = Config.objects.filter(name__in=list(self.fields))
138
self.initial = {config.name: config.value for config in configs}
177
self._load_initials()
179
def _load_initials(self):
181
for name in self.fields.keys():
182
conf = Config.objects.get_config(name)
184
self.initial[name] = conf
141
187
"""Save the content of the fields into the database.
151
197
for name, value in self.cleaned_data.items():
152
198
Config.objects.set_config(name, value)
202
class MaaSAndNetworkForm(ConfigForm):
203
"""Settings page, MaaS and Network section."""
204
maas_name = forms.CharField(label="MaaS name")
205
provide_dhcp = forms.BooleanField(
206
label="Provide DHCP on this subnet", required=False)
209
class CommissioningForm(ConfigForm):
210
"""Settings page, CommissioningF section."""
211
check_compatibility = forms.BooleanField(
212
label="Check component compatibility and certification",
214
after_commissioning = forms.ChoiceField(
215
choices=NODE_AFTER_COMMISSIONING_ACTION_CHOICES,
216
label="After commissioning")
219
class UbuntuForm(ConfigForm):
220
"""Settings page, Ubuntu section."""
221
fallback_master_archive = forms.BooleanField(
222
label="Fallback to Ubuntu master archive",
224
keep_mirror_list_uptodate = forms.BooleanField(
225
label="Keep mirror list up to date",
227
fetch_new_releases = forms.BooleanField(
228
label="Fetch new releases automatically",
231
def __init__(self, *args, **kwargs):
232
super(UbuntuForm, self).__init__(*args, **kwargs)
233
# The field 'update_from' must be added dynamically because its
234
# 'choices' must be evaluated each time the form is instantiated.
235
self.fields['update_from'] = forms.ChoiceField(
237
choices=Config.objects.get_config('update_from_choice'))
238
# The list of fields has changed: load initial values.
239
self._load_initials()
242
hostname_error_msg = "Enter a valid hostname (e.g. host.example.com)."
245
def validate_hostname(value):
247
validator = URLValidator(verify_exists=False)
248
validator('http://%s' % value)
249
except ValidationError:
250
raise ValidationError(hostname_error_msg)
253
class HostnameFormField(CharField):
255
def __init__(self, *args, **kwargs):
256
super(HostnameFormField, self).__init__(
257
validators=[validate_hostname], *args, **kwargs)
260
class AddArchiveForm(ConfigForm):
261
archive_name = HostnameFormField(label="Archive name")
264
"""Save the archive name in the Config table."""
265
archive_name = self.cleaned_data.get('archive_name')
266
archives = Config.objects.get_config('update_from_choice')
267
archives.append([archive_name, archive_name])
268
Config.objects.set_config('update_from_choice', archives)