Revision ab8bfb29 snf-astakos-app/astakos/im/forms.py
b/snf-astakos-app/astakos/im/forms.py | ||
---|---|---|
88 | 88 |
kwargs.pop('request') |
89 | 89 |
self.ip = request.META.get('REMOTE_ADDR', |
90 | 90 |
request.META.get('HTTP_X_REAL_IP', None)) |
91 |
|
|
91 |
|
|
92 | 92 |
super(LocalUserCreationForm, self).__init__(*args, **kwargs) |
93 | 93 |
self.fields.keyOrder = ['email', 'first_name', 'last_name', |
94 | 94 |
'password1', 'password2'] |
... | ... | |
98 | 98 |
'recaptcha_response_field',]) |
99 | 99 |
if get_latest_terms(): |
100 | 100 |
self.fields.keyOrder.append('has_signed_terms') |
101 |
|
|
101 |
|
|
102 | 102 |
if 'has_signed_terms' in self.fields: |
103 | 103 |
# Overriding field label since we need to apply a link |
104 | 104 |
# to the terms within the label |
... | ... | |
168 | 168 |
ro = ('email', 'username',) |
169 | 169 |
for f in ro: |
170 | 170 |
self.fields[f].widget.attrs['readonly'] = True |
171 |
|
|
171 |
|
|
172 | 172 |
|
173 | 173 |
def save(self, commit=True): |
174 | 174 |
user = super(InvitedLocalUserCreationForm, self).save(commit=False) |
... | ... | |
184 | 184 |
class Meta: |
185 | 185 |
model = AstakosUser |
186 | 186 |
fields = ("email", "first_name", "last_name", "third_party_identifier", "has_signed_terms") |
187 |
|
|
187 |
|
|
188 | 188 |
def __init__(self, *args, **kwargs): |
189 | 189 |
""" |
190 | 190 |
Changes the order of fields, and removes the username field. |
... | ... | |
200 | 200 |
ro = ["third_party_identifier"] |
201 | 201 |
for f in ro: |
202 | 202 |
self.fields[f].widget.attrs['readonly'] = True |
203 |
|
|
203 |
|
|
204 | 204 |
if 'has_signed_terms' in self.fields: |
205 | 205 |
# Overriding field label since we need to apply a link |
206 | 206 |
# to the terms within the label |
... | ... | |
208 | 208 |
% (reverse('latest_terms'), _("the terms")) |
209 | 209 |
self.fields['has_signed_terms'].label = \ |
210 | 210 |
mark_safe("I agree with %s" % terms_link_html) |
211 |
|
|
211 |
|
|
212 | 212 |
def clean_email(self): |
213 | 213 |
email = self.cleaned_data['email'] |
214 | 214 |
if not email: |
215 | 215 |
raise forms.ValidationError(_("This field is required")) |
216 | 216 |
return email |
217 |
|
|
217 |
|
|
218 | 218 |
def clean_has_signed_terms(self): |
219 | 219 |
has_signed_terms = self.cleaned_data['has_signed_terms'] |
220 | 220 |
if not has_signed_terms: |
221 | 221 |
raise forms.ValidationError(_('You have to agree with the terms')) |
222 | 222 |
return has_signed_terms |
223 |
|
|
223 |
|
|
224 | 224 |
def save(self, commit=True): |
225 | 225 |
user = super(ThirdPartyUserCreationForm, self).save(commit=False) |
226 | 226 |
user.set_unusable_password() |
... | ... | |
245 | 245 |
ro = ('email',) |
246 | 246 |
for f in ro: |
247 | 247 |
self.fields[f].widget.attrs['readonly'] = True |
248 |
|
|
248 |
|
|
249 | 249 |
def save(self, commit=True): |
250 | 250 |
user = super(InvitedThirdPartyUserCreationForm, self).save(commit=False) |
251 | 251 |
level = user.invitation.inviter.level + 1 |
... | ... | |
258 | 258 |
|
259 | 259 |
class ShibbolethUserCreationForm(ThirdPartyUserCreationForm): |
260 | 260 |
additional_email = forms.CharField(widget=forms.HiddenInput(), label='', required = False) |
261 |
|
|
261 |
|
|
262 | 262 |
def __init__(self, *args, **kwargs): |
263 | 263 |
super(ShibbolethUserCreationForm, self).__init__(*args, **kwargs) |
264 | 264 |
self.fields.keyOrder.append('additional_email') |
... | ... | |
266 | 266 |
name = 'email' |
267 | 267 |
field = self.fields[name] |
268 | 268 |
self.initial['additional_email'] = self.initial.get(name, field.initial) |
269 |
|
|
269 |
|
|
270 | 270 |
def clean_email(self): |
271 | 271 |
email = self.cleaned_data['email'] |
272 | 272 |
for user in AstakosUser.objects.filter(email = email): |
... | ... | |
280 | 280 |
|
281 | 281 |
class InvitedShibbolethUserCreationForm(ShibbolethUserCreationForm, InvitedThirdPartyUserCreationForm): |
282 | 282 |
pass |
283 |
|
|
283 |
|
|
284 | 284 |
class LoginForm(AuthenticationForm): |
285 | 285 |
username = forms.EmailField(label=_("Email")) |
286 | 286 |
recaptcha_challenge_field = forms.CharField(widget=DummyWidget) |
287 | 287 |
recaptcha_response_field = forms.CharField(widget=RecaptchaWidget, label='') |
288 |
|
|
288 |
|
|
289 | 289 |
def __init__(self, *args, **kwargs): |
290 | 290 |
was_limited = kwargs.get('was_limited', False) |
291 | 291 |
request = kwargs.get('request', None) |
292 | 292 |
if request: |
293 | 293 |
self.ip = request.META.get('REMOTE_ADDR', |
294 | 294 |
request.META.get('HTTP_X_REAL_IP', None)) |
295 |
|
|
295 |
|
|
296 | 296 |
t = ('request', 'was_limited') |
297 | 297 |
for elem in t: |
298 | 298 |
if elem in kwargs.keys(): |
299 | 299 |
kwargs.pop(elem) |
300 | 300 |
super(LoginForm, self).__init__(*args, **kwargs) |
301 |
|
|
301 |
|
|
302 | 302 |
self.fields.keyOrder = ['username', 'password'] |
303 | 303 |
if was_limited and RECAPTCHA_ENABLED: |
304 | 304 |
self.fields.keyOrder.extend(['recaptcha_challenge_field', |
305 | 305 |
'recaptcha_response_field',]) |
306 |
|
|
306 |
|
|
307 | 307 |
def clean_recaptcha_response_field(self): |
308 | 308 |
if 'recaptcha_challenge_field' in self.cleaned_data: |
309 | 309 |
self.validate_captcha() |
... | ... | |
320 | 320 |
check = captcha.submit(rcf, rrf, RECAPTCHA_PRIVATE_KEY, self.ip) |
321 | 321 |
if not check.is_valid: |
322 | 322 |
raise forms.ValidationError(_('You have not entered the correct words')) |
323 |
|
|
323 |
|
|
324 | 324 |
def clean(self): |
325 | 325 |
super(LoginForm, self).clean() |
326 | 326 |
if self.user_cache.provider != 'local': |
... | ... | |
392 | 392 |
except AstakosUser.DoesNotExist, e: |
393 | 393 |
raise forms.ValidationError(_('That e-mail address doesn\'t have an associated user account. Are you sure you\'ve registered?')) |
394 | 394 |
return email |
395 |
|
|
395 |
|
|
396 | 396 |
def save(self, domain_override=None, email_template_name='registration/password_reset_email.html', |
397 | 397 |
use_https=False, token_generator=default_token_generator, request=None): |
398 | 398 |
""" |
... | ... | |
420 | 420 |
class Meta: |
421 | 421 |
model = EmailChange |
422 | 422 |
fields = ('new_email_address',) |
423 |
|
|
423 |
|
|
424 | 424 |
def clean_new_email_address(self): |
425 | 425 |
addr = self.cleaned_data['new_email_address'] |
426 | 426 |
if AstakosUser.objects.filter(email__iexact=addr): |
427 | 427 |
raise forms.ValidationError(_(u'This email address is already in use. Please supply a different email address.')) |
428 | 428 |
return addr |
429 |
|
|
429 |
|
|
430 | 430 |
def save(self, email_template_name, request, commit=True): |
431 | 431 |
ec = super(EmailChangeForm, self).save(commit=False) |
432 | 432 |
ec.user = request.user |
... | ... | |
452 | 452 |
|
453 | 453 |
class InvitationForm(forms.ModelForm): |
454 | 454 |
username = forms.EmailField(label=_("Email")) |
455 |
|
|
455 |
|
|
456 | 456 |
def __init__(self, *args, **kwargs): |
457 | 457 |
super(InvitationForm, self).__init__(*args, **kwargs) |
458 |
|
|
458 |
|
|
459 | 459 |
class Meta: |
460 | 460 |
model = Invitation |
461 | 461 |
fields = ('username', 'realname') |
462 |
|
|
462 |
|
|
463 | 463 |
def clean_username(self): |
464 | 464 |
username = self.cleaned_data['username'] |
465 | 465 |
try: |
... | ... | |
475 | 475 |
to optionally renew also the token. |
476 | 476 |
""" |
477 | 477 |
renew = forms.BooleanField(label='Renew token', required=False) |
478 |
|
|
478 |
|
|
479 | 479 |
def __init__(self, user, *args, **kwargs): |
480 | 480 |
super(ExtendedPasswordChangeForm, self).__init__(user, *args, **kwargs) |
481 |
print self.fields.keyOrder |
|
482 |
|
|
481 |
|
|
483 | 482 |
def save(self, commit=True): |
484 | 483 |
user = super(ExtendedPasswordChangeForm, self).save(commit=False) |
485 | 484 |
if self.cleaned_data.get('renew'): |
Also available in: Unified diff