Update LowerCaseCharField to subclass proper CharField
[ganeti_webmgr.git] / ganeti_web / forms / cluster.py
bloba1d15f6d6173687899af99e6f502aa1d79dc6890
1 # Copyright (C) 2010 Oregon State University et al.
2 # Copyright (C) 2010 Greek Research and Technology Network
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17 # USA.
20 from django import forms
21 from django.template.defaultfilters import slugify
22 from django.utils.translation import ugettext as _
24 from ganeti_web.fields import DataVolumeField
25 from ganeti_web.models import Cluster, ClusterUser
28 class QuotaForm(forms.Form):
29 """
30 Form for editing user quota on a cluster
31 """
32 input = forms.TextInput(attrs={'size': 5})
34 user = forms.ModelChoiceField(queryset=ClusterUser.objects.all(),
35 widget=forms.HiddenInput)
36 ram = DataVolumeField(label='Memory', required=False, min_value=0)
37 virtual_cpus = forms.IntegerField(label='Virtual CPUs', required=False,
38 min_value=0, widget=input)
39 disk = DataVolumeField(label='Disk Space', required=False, min_value=0)
40 delete = forms.BooleanField(required=False, widget=forms.HiddenInput)
43 class EditClusterForm(forms.ModelForm):
44 """
45 Basic form for editing a cluster.
46 """
48 class Meta:
49 model = Cluster
50 widgets = {
51 'password': forms.PasswordInput(),
54 ram = DataVolumeField(label=_('Memory'), required=False, min_value=0)
55 disk = DataVolumeField(label=_('Disk Space'), required=False, min_value=0)
57 def need_username(self):
58 msg = _('Enter a username')
59 self._errors['username'] = self.error_class([msg])
61 def need_password(self):
62 msg = _('Enter a password')
63 self._errors['password'] = self.error_class([msg])
65 def clean(self):
66 """
67 Validate this form.
69 Much of the validation is handled in the Cluster model; this method
70 should not duplicate any validation done as part of the Cluster model
71 definition.
72 """
74 data = self.cleaned_data = super(EditClusterForm, self).clean()
76 # Automatically set the slug on cluster creation, based on the
77 # hostname, if no slug was provided.
78 if "hostname" in data and 'slug' not in data:
79 data['slug'] = slugify(data["hostname"].split('.')[0])
80 del self._errors['slug']
82 username = data.get('username', "")
83 password = data.get('password', "")
85 if self.instance is None or not self.instance.username:
86 # This is a new cluster, or a cluster without a username.
87 if username and not password:
88 self.need_password()
89 elif password and not username:
90 self.need_username()
92 elif self.instance.username:
93 # The cluster had a username set. Password is not required unless
94 # the username has changed.
95 if username and not password:
96 if username == self.instance.username:
97 # The user didn't enter a password and it wasn't required;
98 # retain the existing password instead of setting it to
99 # the empty string.
100 data['password'] = self.instance.password
101 else:
102 # New username; get a new password too.
103 self.need_password()
105 elif password and not username:
106 self.need_username()
108 return data