コード例 #1
0
ファイル: tests.py プロジェクト: githubber/weblate
 def test_generate(self):
     '''
     Test generating of captcha for every operator.
     '''
     captcha = MathCaptcha()
     for operator in MathCaptcha.operators:
         captcha.operators = (operator,)
         self.assertIn(operator, captcha.generate_question())
コード例 #2
0
ファイル: tests.py プロジェクト: githubber/weblate
 def test_object(self):
     captcha = MathCaptcha('1 * 2')
     self.assertFalse(
         captcha.validate(1)
     )
     self.assertTrue(
         captcha.validate(2)
     )
     restored = MathCaptcha.from_hash(captcha.hashed)
     self.assertEqual(
         captcha.question,
         restored.question
     )
     self.assertRaises(
         ValueError,
         MathCaptcha.from_hash,
         captcha.hashed[:40]
     )
コード例 #3
0
ファイル: forms.py プロジェクト: spc-12/weblate
    def __init__(self, data=None, *args, **kwargs):
        super(CaptchaRegistrationForm, self).__init__(
            data,
            *args,
            **kwargs
        )

        # Load data
        self.tampering = False
        if data is None or 'captcha_id' not in data:
            self.captcha = MathCaptcha()
        else:
            try:
                self.captcha = MathCaptcha.from_hash(data['captcha_id'])
            except ValueError:
                self.captcha = MathCaptcha()
                self.tampering = True

        # Set correct label
        self.fields['captcha'].label = _('What is %s') % self.captcha.display
        self.fields['captcha_id'].initial = self.captcha.hashed
コード例 #4
0
ファイル: forms.py プロジェクト: fluentglobe/our
class CaptchaRegistrationForm(RegistrationForm):
    '''
    Registration form with captcha protection.
    '''
    captcha = forms.IntegerField(required=True)
    captcha_id = forms.CharField(widget=forms.HiddenInput)

    def __init__(self, data=None, *args, **kwargs):
        super(CaptchaRegistrationForm, self).__init__(
            data,
            *args,
            **kwargs
        )

        # Load data
        self.tampering = False
        if data is None or 'captcha_id' not in data:
            self.captcha = MathCaptcha()
        else:
            try:
                self.captcha = MathCaptcha.from_hash(data['captcha_id'])
            except ValueError:
                self.captcha = MathCaptcha()
                self.tampering = True

        # Set correct label
        self.fields['captcha'].label = _('What is %s') % self.captcha.display
        self.fields['captcha_id'].initial = self.captcha.hashed

    def clean_captcha(self):
        '''
        Validation for captcha.
        '''
        if (self.tampering
                or not self.captcha.validate(self.cleaned_data['captcha'])):
            raise forms.ValidationError(
                _('Please check your math and try again.')
            )

        if 'email' in self.cleaned_data:
            mail = self.cleaned_data['email']
        else:
            mail = 'NONE'

        weblate.logger.info(
            'Passed captcha for %s (%s = %s)',
            mail,
            self.captcha.question,
            self.cleaned_data['captcha']
        )