Revision db7fecd9 snf-astakos-app/astakos/im/forms.py

b/snf-astakos-app/astakos/im/forms.py
42 42
from django.core.urlresolvers import reverse
43 43

  
44 44
from astakos.im.models import AstakosUser
45
from astakos.im.settings import INVITATIONS_PER_LEVEL, DEFAULT_FROM_EMAIL, BASEURL, SITENAME
45
from astakos.im.settings import INVITATIONS_PER_LEVEL, DEFAULT_FROM_EMAIL, BASEURL, SITENAME, RECAPTCHA_PRIVATE_KEY
46
from astakos.im.widgets import DummyWidget, RecaptchaWidget
46 47

  
47 48
import logging
49
import recaptcha.client.captcha as captcha
48 50

  
49 51
logger = logging.getLogger(__name__)
50 52

  
......
56 58
    * The username field isn't visible and it is assigned a generated id.
57 59
    * User created is not active. 
58 60
    """
61
    recaptcha_challenge_field = forms.CharField(widget=DummyWidget)
62
    recaptcha_response_field = forms.CharField(widget=RecaptchaWidget, label='')
59 63
    
60 64
    class Meta:
61 65
        model = AstakosUser
......
65 69
        """
66 70
        Changes the order of fields, and removes the username field.
67 71
        """
72
        if 'ip' in kwargs:
73
            self.ip = kwargs['ip']
74
            kwargs.pop('ip')
68 75
        super(LocalUserCreationForm, self).__init__(*args, **kwargs)
69 76
        self.fields.keyOrder = ['email', 'first_name', 'last_name',
70
                                'password1', 'password2']
77
                                'password1', 'password2',
78
                                'recaptcha_challenge_field',
79
                                'recaptcha_response_field']
71 80
    
72 81
    def clean_email(self):
73 82
        email = self.cleaned_data['email']
......
79 88
        except AstakosUser.DoesNotExist:
80 89
            return email
81 90
    
91
    def clean_recaptcha_response_field(self):
92
        if 'recaptcha_challenge_field' in self.cleaned_data:
93
            self.validate_captcha()
94
        return self.cleaned_data['recaptcha_response_field']
95

  
96
    def clean_recaptcha_challenge_field(self):
97
        if 'recaptcha_response_field' in self.cleaned_data:
98
            self.validate_captcha()
99
        return self.cleaned_data['recaptcha_challenge_field']
100

  
101
    def validate_captcha(self):
102
        rcf = self.cleaned_data['recaptcha_challenge_field']
103
        rrf = self.cleaned_data['recaptcha_response_field']
104
        check = captcha.submit(rcf, rrf, RECAPTCHA_PRIVATE_KEY, self.ip)
105
        if not check.is_valid:
106
            raise forms.ValidationError(_('You have not entered the correct words'))
107
    
82 108
    def save(self, commit=True):
83 109
        """
84 110
        Saves the email, first_name and last_name properties, after the normal
......
163 189
        fields = ('email', 'last_name', 'first_name', 'affiliation', 'provider', 'third_party_identifier')
164 190
    
165 191
    def __init__(self, *args, **kwargs):
192
        if 'ip' in kwargs:
193
            self.ip = kwargs['ip']
194
            kwargs.pop('ip')
166 195
        super(ThirdPartyUserCreationForm, self).__init__(*args, **kwargs)
167 196
        self.fields.keyOrder = ['email']
168 197
    

Also available in: Unified diff