28 from django
import forms
29 from django.utils.translation
import ugettext_lazy
as _
31 from common.states
import user_active_states
32 from wi
import settings
33 from wi.recaptcha_django
import ReCaptchaField
34 from wi.utils
import parsing
35 from wi.utils.auth
import authenticate, cm_authenticate
36 from wi.utils.forms
import PasswordForm, attrs_dict
37 from wi.utils.regexp
import regexp, regexp_text
38 from wi.utils.views
import make_request
39 from wi.utils.widgets
import SelectWithDisabled
47 username = forms.CharField(max_length=63,
49 widget=forms.TextInput(attrs={
'tabindex':
'1',
'class':
'required'}))
50 password = forms.RegexField(regex=regexp[
'password'],
53 widget=forms.PasswordInput(attrs={
'tabindex':
'2',
'class':
'required'}),
54 error_messages={
'invalid': regexp_text[
'password']})
66 def __init__(self, request=None, *args, **kwargs):
69 super(AuthenticationForm, self).
__init__(*args, **kwargs)
76 if not self.cleaned_data.get(
'password')
or not self.cleaned_data.get(
'username'):
78 self.cleaned_data[
'password'] = hashlib.sha1(self.cleaned_data[
'password']).hexdigest()
80 username = self.cleaned_data[
'username']
81 password = self.cleaned_data[
'password']
86 raise forms.ValidationError(_(
"Please enter a correct username and password. Note that both fields are case-sensitive."))
87 elif self.user_cache.is_active == user_active_states[
'inactive']:
88 raise forms.ValidationError(_(
"Account has not been activated yet. Please, click on the activation link in the email sent to you after the registration step."))
89 elif self.user_cache.is_active == user_active_states[
'email_confirmed']:
90 raise forms.ValidationError(_(
"This account is inactive. Please wait for system operator to activate your account."))
92 if self.
request and not self.request.session.test_cookie_worked():
93 raise forms.ValidationError(_(
"Your Web browser doesn't appear to have cookies enabled. Cookies are required for logging in."))
95 return self.cleaned_data
110 email = forms.EmailField(label=_(
"E-mail"), max_length=255)
117 email = self.cleaned_data[
'email']
119 rest_data =
make_request(
'guest/user/email_exists/', {
'email': email})
120 if rest_data[
'status'] ==
'ok' and rest_data[
'data'] ==
False:
121 raise forms.ValidationError(_(
'Incorrect email address.'))
131 new_password1 = forms.RegexField(regex=regexp[
'password'],
133 widget=forms.PasswordInput(attrs=dict(attrs_dict)),
134 label=_(
"New password"),
135 error_messages={
'invalid': regexp_text[
'password']})
137 new_password2 = forms.RegexField(regex=regexp[
'password'],
139 widget=forms.PasswordInput(attrs=dict(attrs_dict)),
140 label=_(
"New password confirmation"),
141 error_messages={
'invalid': regexp_text[
'password']})
147 if 'new_password1' in self.cleaned_data
and 'new_password2' in self.cleaned_data:
148 if self.cleaned_data[
'new_password1'] != self.cleaned_data[
'new_password2']:
149 raise forms.ValidationError(_(
"The two password fields didn't match."))
151 self.cleaned_data[
'new_password1'] = hashlib.sha1(self.cleaned_data[
'new_password1']).hexdigest()
152 del self.cleaned_data[
'new_password2']
153 return self.cleaned_data
164 login = forms.RegexField(regex=regexp[
'login'],
166 widget=forms.TextInput(attrs=attrs_dict),
168 error_messages={
'invalid': regexp_text[
'login']})
169 first = forms.CharField(max_length=63,
170 widget=forms.TextInput(attrs=attrs_dict),
171 label=_(
'First name'))
172 last = forms.CharField(max_length=63,
173 widget=forms.TextInput(attrs=attrs_dict),
174 label=_(
'Last name'))
175 organization = forms.CharField(max_length=63,
176 widget=forms.TextInput(attrs=attrs_dict),
177 label=_(
'Organization'))
178 email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=255)),
179 label=_(
'Email address'))
182 super(RegistrationForm, self).
__init__(*args, **kwargs)
184 self.fields.keyOrder = [
'login',
'first',
'last',
'organization',
'email',
'new_password',
'password2']
187 self.fields[
'recaptcha'] = ReCaptchaField()
194 response =
make_request(
'guest/user/exists/', {
'login': self.cleaned_data[
'login']})
196 if response[
'data'] ==
False:
197 return self.cleaned_data[
'login']
199 raise forms.ValidationError(_(
"A user with that login already exists."))
206 response =
make_request(
'guest/user/email_exists/', {
'email': self.cleaned_data[
'email']})
208 if response[
'data'] ==
False:
209 return self.cleaned_data[
'email']
211 raise forms.ValidationError(_(
"This email address is already in use. Please supply a different email address."))
219 email = forms.EmailField(widget=forms.TextInput(attrs=dict(maxlength=255)),
220 label=_(
'Email address'))
223 rest_data = kwargs.pop(
'rest_data')
228 super(AccountDataEdit, self).
__init__(*args, **kwargs)
230 self.fields[
'default_cluster_id'] = forms.ChoiceField(choices=parsing.parse_cm_list(rest_data), label=_(
'Default CM'))
231 self.fields[
'default_cluster_id'].widget.attrs[
'class'] =
'medium'
239 if self.
old_email == self.cleaned_data[
'email']:
240 return self.cleaned_data[
'email']
242 rest_data =
make_request(
'guest/user/email_exists/', {
'email': self.cleaned_data[
'email']})
244 if rest_data[
'data']:
245 raise forms.ValidationError(_(
"This email address is already in use. Please supply a different email address."))
247 return self.cleaned_data[
'email']
254 return int(self.cleaned_data[
'default_cluster_id'])
263 old_password = forms.RegexField(regex=regexp[
'password'],
265 widget=forms.PasswordInput,
266 label=_(
"Old password"),
267 error_messages={
'invalid': regexp_text[
'password']})
271 super(PasswordChangeForm, self).
__init__(*args, **kwargs)
273 self.fields.keyOrder = [
'old_password',
'new_password1',
'new_password2']
280 self.cleaned_data[
'old_password'] = hashlib.sha1(self.cleaned_data[
'old_password']).hexdigest()
281 old_password = self.cleaned_data[
"old_password"]
283 rest_data =
make_request(
'guest/user/check_password/', {
'login': self.user.username,
'password': old_password})
284 if rest_data[
'status'] ==
'ok' and rest_data[
'data'] ==
False:
285 raise forms.ValidationError(_(
"Your old password was entered incorrectly. Please enter it again."))
294 topic = forms.CharField(widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=255)),
297 firstlast = forms.CharField(max_length=127,
298 widget=forms.TextInput(attrs=attrs_dict),
299 label=_(
'First and last name'))
301 email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict, maxlength=255)),
302 label=_(
'Your email address'))
304 issue = forms.CharField(widget=forms.Textarea(attrs=dict(attrs_dict, rows=5, maxlength=2048)),
305 label=_(
'Describe your issue'))
308 super(HelpForm, self).
__init__(*args, **kwargs)
316 first = forms.CharField(max_length=63,
317 widget=forms.TextInput(),
318 label=_(
"First name"),
320 last = forms.CharField(max_length=63,
321 widget=forms.TextInput(),
322 label=_(
"Last name"),
324 email = forms.EmailField(widget=forms.TextInput(attrs=dict(maxlength=255)),
325 label=_(
"Email address"))
326 organization = forms.CharField(max_length=63,
327 widget=forms.TextInput(),
328 label=_(
"Organization"))
335 super(AccountDataEditAdminCLM, self).
__init__(*args, **kwargs)
343 password = forms.RegexField(regex=regexp[
'password'],
346 widget=forms.PasswordInput(attrs={
'tabindex':
'1',
'class':
'required'}),
347 error_messages={
'invalid': regexp_text[
'password']})
351 rest_data = kwargs.pop(
'rest_data')
352 super(CMAuthenticationForm, self).
__init__(*args, **kwargs)
354 self.fields[
'cm'] = forms.ChoiceField(choices=parsing.parse_cm_list(rest_data),
355 initial=request.session[
'user'].default_cluster_id,
356 widget=SelectWithDisabled(attrs=dict({
'class':
'small'})),
357 label=_(
"Cluster Manager"))
358 self.fields.keyOrder = [
'cm',
'password']
365 return int(self.cleaned_data[
'cm'])
372 self.cleaned_data[
'password'] = hashlib.sha1(self.cleaned_data[
'password']).hexdigest()
374 password = self.cleaned_data[
'password']
375 cm = self.cleaned_data[
'cm']
377 if password
and not cm_authenticate(self.request.session[
'user'], password, cm):
378 raise forms.ValidationError(_(
'Please enter a correct password. Note that password field is case-sensitive.'))
380 return self.cleaned_data
388 cpu = forms.IntegerField(label=_(
"Cpu Total"))
389 memory = forms.IntegerField(label=_(
"Memory Total [MB]"))
390 storage = forms.IntegerField(label=_(
"Storage Total [MB]"))
391 public_ip = forms.IntegerField(min_value=0, label=_(
"Public IPs Total"))
392 points = forms.IntegerField(min_value=0, label=_(
"Points"))
400 cpu = forms.IntegerField(label=_(
"Cpu Total"), required=
False)
401 memory = forms.IntegerField(label=_(
"Memory Total [MB]"), required=
False)
402 storage = forms.IntegerField(label=_(
"Storage Total [MB]"), required=
False)
403 public_ip = forms.IntegerField(min_value=0, label=_(
"Public IPs Total"), required=
False)
404 points = forms.IntegerField(min_value=0, label=_(
"Points"), required=
False)
413 rest_data = kwargs.pop(
'rest_data')
414 super(CopyToUserForm, self).
__init__(*args, **kwargs)
415 self.fields[
'dest_user_id'] = forms.ChoiceField(choices=parsing.parse_cm_users(rest_data),