Revision 2c669a3a snf-astakos-app/astakos/im/forms.py
b/snf-astakos-app/astakos/im/forms.py | ||
---|---|---|
45 | 45 |
from django.utils.safestring import mark_safe |
46 | 46 |
from django.contrib import messages |
47 | 47 |
from django.utils.encoding import smart_str |
48 |
from captcha.fields import ReCaptchaField |
|
48 | 49 |
|
49 | 50 |
from astakos.im.models import AstakosUser, Invitation, get_latest_terms, EmailChange |
50 | 51 |
from astakos.im.settings import INVITATIONS_PER_LEVEL, DEFAULT_FROM_EMAIL, \ |
51 |
BASEURL, SITENAME, RECAPTCHA_PRIVATE_KEY, DEFAULT_CONTACT_EMAIL, \ |
|
52 |
RECAPTCHA_ENABLED, LOGGING_LEVEL |
|
52 |
BASEURL, SITENAME, DEFAULT_CONTACT_EMAIL, \ |
|
53 |
RECAPTCHA_ENABLED, RECAPTCHA_PRIVATE_KEY, RECAPTCHA_PUBLIC_KEY, RECAPTCHA_USE_SSL, RECAPTCHA_OPTIONS, \ |
|
54 |
LOGGING_LEVEL |
|
53 | 55 |
from astakos.im.widgets import DummyWidget, RecaptchaWidget |
54 | 56 |
from astakos.im.functions import send_change_email |
55 | 57 |
|
... | ... | |
67 | 69 |
""" |
68 | 70 |
Extends the built in UserCreationForm in several ways: |
69 | 71 |
|
70 |
* Adds email, first_name, last_name, recaptcha_challenge_field, recaptcha_response_field field.
|
|
72 |
* Adds email, first_name, last_name, recaptcha_challenge_field field. |
|
71 | 73 |
* The username field isn't visible and it is assigned a generated id. |
72 | 74 |
* User created is not active. |
73 | 75 |
""" |
74 |
recaptcha_challenge_field = forms.CharField(widget=DummyWidget) |
|
75 |
recaptcha_response_field = forms.CharField(widget=RecaptchaWidget, label='') |
|
76 |
recaptcha_challenge_field = ReCaptchaField(private_key=RECAPTCHA_PRIVATE_KEY, |
|
77 |
public_key=RECAPTCHA_PUBLIC_KEY, |
|
78 |
use_ssl=RECAPTCHA_USE_SSL, |
|
79 |
attrs=RECAPTCHA_OPTIONS) |
|
76 | 80 |
|
77 | 81 |
class Meta: |
78 | 82 |
model = AstakosUser |
... | ... | |
94 | 98 |
if get_latest_terms(): |
95 | 99 |
self.fields.keyOrder.append('has_signed_terms') |
96 | 100 |
if RECAPTCHA_ENABLED: |
97 |
self.fields.keyOrder.extend(['recaptcha_challenge_field', |
|
98 |
'recaptcha_response_field',]) |
|
101 |
self.fields.keyOrder.extend(['recaptcha_challenge_field']) |
|
99 | 102 |
|
100 | 103 |
if 'has_signed_terms' in self.fields: |
101 | 104 |
# Overriding field label since we need to apply a link |
... | ... | |
118 | 121 |
if not has_signed_terms: |
119 | 122 |
raise forms.ValidationError(_('You have to agree with the terms')) |
120 | 123 |
return has_signed_terms |
121 |
|
|
122 |
def clean_recaptcha_response_field(self): |
|
123 |
if 'recaptcha_challenge_field' in self.cleaned_data: |
|
124 |
self.validate_captcha() |
|
125 |
return self.cleaned_data['recaptcha_response_field'] |
|
126 |
|
|
127 |
def clean_recaptcha_challenge_field(self): |
|
128 |
if 'recaptcha_response_field' in self.cleaned_data: |
|
129 |
self.validate_captcha() |
|
130 |
return self.cleaned_data['recaptcha_challenge_field'] |
|
131 |
|
|
132 |
def validate_captcha(self): |
|
133 |
rcf = self.cleaned_data['recaptcha_challenge_field'] |
|
134 |
rrf = self.cleaned_data['recaptcha_response_field'] |
|
135 |
check = captcha.submit(rcf, rrf, RECAPTCHA_PRIVATE_KEY, self.ip) |
|
136 |
if not check.is_valid: |
|
137 |
raise forms.ValidationError(_('You have not entered the correct words')) |
|
138 |
|
|
124 |
|
|
139 | 125 |
def save(self, commit=True): |
140 | 126 |
""" |
141 | 127 |
Saves the email, first_name and last_name properties, after the normal |
... | ... | |
280 | 266 |
|
281 | 267 |
class LoginForm(AuthenticationForm): |
282 | 268 |
username = forms.EmailField(label=_("Email")) |
283 |
recaptcha_challenge_field = forms.CharField(widget=DummyWidget) |
|
284 |
recaptcha_response_field = forms.CharField(widget=RecaptchaWidget, label='') |
|
269 |
recaptcha_challenge_field = ReCaptchaField(private_key=RECAPTCHA_PRIVATE_KEY, |
|
270 |
public_key=RECAPTCHA_PUBLIC_KEY, |
|
271 |
use_ssl=RECAPTCHA_USE_SSL, |
|
272 |
attrs=RECAPTCHA_OPTIONS) |
|
285 | 273 |
|
286 | 274 |
def __init__(self, *args, **kwargs): |
287 | 275 |
was_limited = kwargs.get('was_limited', False) |
... | ... | |
298 | 286 |
|
299 | 287 |
self.fields.keyOrder = ['username', 'password'] |
300 | 288 |
if was_limited and RECAPTCHA_ENABLED: |
301 |
self.fields.keyOrder.extend(['recaptcha_challenge_field', |
|
302 |
'recaptcha_response_field',]) |
|
303 |
|
|
304 |
def clean_recaptcha_response_field(self): |
|
305 |
if 'recaptcha_challenge_field' in self.cleaned_data: |
|
306 |
self.validate_captcha() |
|
307 |
return self.cleaned_data['recaptcha_response_field'] |
|
308 |
|
|
309 |
def clean_recaptcha_challenge_field(self): |
|
310 |
if 'recaptcha_response_field' in self.cleaned_data: |
|
311 |
self.validate_captcha() |
|
312 |
return self.cleaned_data['recaptcha_challenge_field'] |
|
313 |
|
|
314 |
def validate_captcha(self): |
|
315 |
rcf = self.cleaned_data['recaptcha_challenge_field'] |
|
316 |
rrf = self.cleaned_data['recaptcha_response_field'] |
|
317 |
check = captcha.submit(rcf, rrf, RECAPTCHA_PRIVATE_KEY, self.ip) |
|
318 |
if not check.is_valid: |
|
319 |
raise forms.ValidationError(_('You have not entered the correct words')) |
|
320 |
|
|
321 |
def clean(self): |
|
322 |
cleaned_data = super(LoginForm, self).clean() |
|
323 |
username = cleaned_data.get('username') |
|
324 |
if username: |
|
325 |
try: |
|
326 |
user = AstakosUser.objects.get(email=username, is_active=True) |
|
327 |
except: |
|
328 |
pass |
|
329 |
else: |
|
330 |
if user.provider != 'local': |
|
331 |
raise forms.ValidationError(_("Invalid authentication method.")) |
|
332 |
return cleaned_data |
|
289 |
self.fields.keyOrder.extend(['recaptcha_challenge_field']) |
|
333 | 290 |
|
334 | 291 |
class ProfileForm(forms.ModelForm): |
335 | 292 |
""" |
... | ... | |
392 | 349 |
try: |
393 | 350 |
user = AstakosUser.objects.get(email=email, is_active=True) |
394 | 351 |
if not user.has_usable_password(): |
395 |
raise forms.ValidationError(_("This account has not a usable password."))
|
|
352 |
raise forms.ValidationError(_("This account does not have a usable password."))
|
|
396 | 353 |
except AstakosUser.DoesNotExist, e: |
397 | 354 |
raise forms.ValidationError(_('That e-mail address doesn\'t have an associated user account. Are you sure you\'ve registered?')) |
398 | 355 |
return email |
Also available in: Unified diff