Example #1
0
def register(request):
    if request.user.is_authenticated():
        return redirect(reverse('home'))
    if request.method == 'POST':
        form = RegisterForm(request.POST)
        if form.is_valid():
            new_user = User(
                username=form.cleaned_data['username'],
                email=form.cleaned_data['email'],
                password=make_password(form.cleaned_data['password1']),
                is_active=False,
            )
            new_user.save()
            if SERVER_EMAIL:
                messages.success(request, "Your account was successfully created. An activation link has been sent to your email address.")
                key = User.objects.make_random_password(length=32)
                activation = Activation(user=new_user, key=key)
                activation.save()
                send_mail('upTee registration', 'Thank you for your registration.\r\nClick the link below to activate your account:\r\n\r\nhttp://{0}/activate/{1}'.format(request.META['HTTP_HOST'], key), SERVER_EMAIL, [new_user.email], fail_silently=not DEBUG)
            else:
                messages.success(request, "Your account was successfully created. An Admin should contact you shortly.")
            if ADMINS:
                mail_admins('User registration', u'The following user just registered and wants to be activated:\r\n\r\n{0}'.format(form.cleaned_data['username']), fail_silently=not DEBUG)
            return redirect(reverse('home'))
    else:
        form = RegisterForm()
    challenge, response = captcha_settings.get_challenge()()
    store = CaptchaStore.objects.create(challenge=challenge, response=response)
    key = store.hashkey
    return render_to_response('accounts/register.html', {
            'captcha': key,
            'register_form': form,
        }, context_instance=RequestContext(request))
    def setUp(self):

        self.stores = {}
        self.__current_settings_output_format = settings.CAPTCHA_OUTPUT_FORMAT
        self.__current_settings_dictionary = settings.CAPTCHA_WORDS_DICTIONARY
        self.__current_settings_punctuation = settings.CAPTCHA_PUNCTUATION

        tested_helpers = [
            "captcha.helpers.math_challenge",
            "captcha.helpers.random_char_challenge",
            "captcha.helpers.unicode_challenge",
        ]
        if os.path.exists("/usr/share/dict/words"):
            settings.CAPTCHA_WORDS_DICTIONARY = "/usr/share/dict/words"
            settings.CAPTCHA_PUNCTUATION = ";-,."
            tested_helpers.append("captcha.helpers.word_challenge")
            tested_helpers.append("captcha.helpers.huge_words_and_punctuation_challenge")
        for helper in tested_helpers:
            challenge, response = settings._callable_from_string(helper)()
            self.stores[
                helper.rsplit(".", 1)[-1].replace("_challenge", "_store")
            ], _ = CaptchaStore.objects.get_or_create(challenge=challenge, response=response)
        challenge, response = settings.get_challenge()()
        self.stores["default_store"], _ = CaptchaStore.objects.get_or_create(challenge=challenge, response=response)
        self.default_store = self.stores["default_store"]
Example #3
0
    def render(self, name, value, attrs=None):
        try:
            reverse('captcha-image', args=('dummy',))
        except Exception:
            raise ImproperlyConfigured(
                'Make sure you\'ve included captcha.urls as explained in the '
                'INSTALLATION section on '
                'http://code.google.com/p/django-simple-captcha/')

        challenge, response = get_challenge()()

        store = CaptchaStore.objects.create(
            challenge=challenge, response=response)
        key = store.hashkey
        value = [key, u'']

        self.image_and_audio = (
            '<img src="%s" id="%s_img" alt="captcha" class="captcha" />' % (
                reverse(
                    'captcha-image', kwargs=dict(key=key)),
                attrs['id']))
        if CAPTCHA_FLITE_PATH:
            self.image_and_audio = '<a href="%s" title="%s">%s</a>' % (
                reverse('captcha-audio', kwargs=dict(key=key)),
                unicode(_('Play captcha as audio file')), self.image_and_audio)

        return super(CaptchaTextInput, self).render(name, value, attrs=attrs)
Example #4
0
File: views.py Project: kolko/upTee
def register(request):
    if request.user.is_authenticated():
        return redirect(reverse('home'))
    if request.method == 'POST':
        form = RegisterForm(request.POST)
        if form.is_valid():
            new_user = User(
                username=form.cleaned_data['username'],
                email=form.cleaned_data['email'],
                password=make_password(form.cleaned_data['password1']),
                is_active=False,
            )
            new_user.save()
            messages.success(request, "Your account was successfully created. An Admin will contact you shortly.")
            if ADMINS:
                mail_admins('User registration', u'The following user just registered and wants to be activated:\r\n\r\n{0}'.format(form.cleaned_data['username']), fail_silently=not DEBUG)
            return redirect(reverse('home'))
    else:
        form = RegisterForm()
    challenge, response = captcha_settings.get_challenge()()
    store = CaptchaStore.objects.create(challenge=challenge, response=response)
    key = store.hashkey
    return render_to_response('accounts/register.html', {
            'captcha': key,
            'register_form': form,
        }, context_instance=RequestContext(request))
Example #5
0
 def get_captcha(self, request):
     from captcha.conf.settings import get_challenge
     from captcha.models import CaptchaStore
     challenge, response = get_challenge()()
     store = CaptchaStore.objects.create(
         challenge=challenge, response=response)
     key = store.hashkey
     return HttpResponse(key)
Example #6
0
 def setUp(self):
     self.default_challenge = settings.get_challenge()()
     self.math_challenge = settings._callable_from_string('captcha.helpers.math_challenge')()
     self.chars_challenge = settings._callable_from_string('captcha.helpers.random_char_challenge')()
     
     self.default_store, created =  CaptchaStore.objects.get_or_create(challenge=self.default_challenge[0],response=self.default_challenge[1])
     self.math_store, created = CaptchaStore.objects.get_or_create(challenge=self.math_challenge[0],response=self.math_challenge[1])
     self.chars_store, created = CaptchaStore.objects.get_or_create(challenge=self.chars_challenge[0],response=self.chars_challenge[1])
def captcha_ajax_reload(request):
    if request.method == 'POST' and request.is_ajax():
        challenge, response = settings.get_challenge()()
        store = CaptchaStore.objects.create(challenge=challenge, response=response)
        data = {
            'key': store.hashkey,
            'image': reverse('captcha-image', args=[store.hashkey]),
        }
        return HttpResponse(simplejson.dumps(data), content_type="application/javascript; charset=utf-8")
    return HttpResponse('')
Example #8
0
def captcha_refresh(request):

    if request.is_ajax():
        to_json_responce = dict()
        challenge,response = settings.get_challenge()()
        store = CaptchaStore.objects.create(challenge=challenge, response=response)
        to_json_responce['key'] = store.hashkey
        to_json_responce['image_url'] = reverse('captcha-image', kwargs=dict(key=store.hashkey))

        return HttpResponse(json.dumps(to_json_responce), mimetype='application/json')
    raise Http404
Example #9
0
 def render(self, name, value, attrs=None):
     challenge,response= settings.get_challenge()()
     
     store, created = CaptchaStore.objects.get_or_create(challenge=challenge,response=response)
     key = store.hashkey
     value = [key, u'']
     
     ret = '<img src="%s" alt="captcha" class="captcha" />' %reverse('captcha-image',kwargs=dict(key=key))
     if settings.CAPTCHA_FLITE_PATH:
         ret = '<a href="%s" title="%s">%s</a>' %( reverse('captcha-audio', kwargs=dict(key=key)), unicode(_('Play captcha as audio file')), ret)
     return mark_safe(ret + super(CaptchaTextInput, self).render(name, value, attrs=attrs))
Example #10
0
    def render(self, name, value, attrs=None):
        challenge,response= settings.get_challenge()()

        store, created = CaptchaStore.objects.get_or_create(challenge=challenge,response=response)
        key = store.hashkey
        value = [key, u'']

        return render_to_string("captcha/captcha.html", {
            "key": key,
            "flite": settings.CAPTCHA_FLITE_PATH,
            "field": super(CaptchaTextInput, self).render(name, value, attrs=attrs),
        })
Example #11
0
def captcha_refresh(request):
    """  Return json with new captcha for ajax refresh request """
    if not request.is_ajax():
        raise Http404

    challenge, response = settings.get_challenge()()

    new_key = create(challenge, response)
    to_json_response = {
        'key': new_key,
        'image_url': captcha_image_url(new_key),
    }
    return HttpResponse(json.dumps(to_json_response), content_type='application/json')
Example #12
0
    def render(self, name, value, attrs=None):
        try:
            reverse('captcha-image', args=('dummy',))
        except NoReverseMatch:
            raise ImproperlyConfigured('Make sure you\'ve included captcha.urls as explained in the INSTALLATION section on http://readthedocs.org/docs/django-simple-captcha/en/latest/usage.html#installation')

        challenge, response = settings.get_challenge()()
        store = CaptchaStore.objects.create(challenge=challenge, response=response)
        key = store.hashkey
        value = [key, u'']

        self.image_and_audio = '<img src="%s" alt="captcha" class="captcha" />' % reverse('captcha-image', kwargs=dict(key=key))
        return super(CaptchaTextInput, self).render(name, value, attrs=attrs)
Example #13
0
    def test_manga_report_view_post_anonymous(self):
        from captcha.conf import settings
        from captcha.models import CaptchaStore

        challenge, response = settings.get_challenge()()
        store = CaptchaStore.objects.create(challenge=challenge, response=response)

        self.client.logout()
        response = self.client.post(
            reverse("manga.report", args=[self.manga.id, self.manga.slug]),
            {"type": ReportMangaType.COPYRIGHT, "check": "on", "captcha_0": store.hashkey, "captcha_1": store.response},
        )
        self.assertRedirects(response, reverse("manga.list"))
    def setUp(self):

        self.stores = {}

        tested_helpers = ['captcha.helpers.math_challenge', 'captcha.helpers.random_char_challenge', 'captcha.helpers.unicode_challenge']
        if os.path.exists('/usr/share/dict/words'):
            settings.CAPTCHA_WORDS_DICTIONARY = '/usr/share/dict/words'
            settings.CAPTCHA_PUNCTUATION = ';-,.'
            tested_helpers.append('captcha.helpers.word_challenge')
            tested_helpers.append('captcha.helpers.huge_words_and_punctuation_challenge')
        for helper in tested_helpers:
            challenge, response = settings._callable_from_string(helper)()
            self.stores[helper.rsplit('.', 1)[-1].replace('_challenge', '_store')], _ = CaptchaStore.objects.get_or_create(challenge=challenge, response=response)
        challenge, response = settings.get_challenge()()
        self.stores['default_store'], _ = CaptchaStore.objects.get_or_create(challenge=challenge, response=response)
        self.default_store = self.stores['default_store']
Example #15
0
    def test_account_register_view_post_disabled(self):
        SiteSetting.set_val(SiteSettingKey.ENABLE_REGISTRATION, 'False', self.user)
        challenge, response = settings.get_challenge()()
        store = CaptchaStore.objects.create(challenge=challenge, response=response)

        self.client.logout()
        response = self.client.post(reverse('account.register'), {
            'username': '******',
            'password1': 'password',
            'password2': 'password',
            'next': reverse('tag.list.author'),
            'captcha_0': store.hashkey,
            'captcha_1': store.response,
        })

        self.assertRedirects(response, reverse('account.register'))
Example #16
0
    def test_account_register_view_post(self):
        challenge, response = settings.get_challenge()()
        store = CaptchaStore.objects.create(challenge=challenge, response=response)

        self.client.logout()
        response = self.client.post(reverse('account.register'), {
            'username': '******',
            'password1': 'password',
            'password2': 'password',
            'next': reverse('tag.list.author'),
            'captcha_0': store.hashkey,
            'captcha_1': store.response,
        })
        self.assertRedirects(response, reverse('tag.list.author'))

        user = User.objects.get(username='******')
        self.assertTrue(user.check_password('password'))
Example #17
0
 def render(self, name, value, attrs=None):
     challenge,response= settings.get_challenge()()
     _attrs = {'class':'captcha-input'}
     if attrs:
         _attrs.update(attrs)
     store, created = CaptchaStore.objects.get_or_create(challenge=challenge,response=response)
     key = store.hashkey
     value = [key, u'']
     self.key = key
     ret = '<img src="%s" alt="captcha" class="captcha" />' %reverse('captcha-image',kwargs=dict(key=key))
     self.img = ret
     if settings.CAPTCHA_FLITE_PATH:
         ret = '<a href="%s" title="%s">%s</a>' %( reverse('captcha-audio', kwargs=dict(key=key)), unicode(_('Play captcha as audio file')), ret)
     
     if settings.CAPTCHA_IMAGE_BEFORE_FIELD:
         return mark_safe(ret + super(CaptchaTextInput, self).render(name, value, attrs=_attrs))
     else:
         return mark_safe(super(CaptchaTextInput, self).render(name, value, _attrs=attrs)+ret)
Example #18
0
    def fetch_captcha_store(self, name, value, attrs=None):
        """
        Fetches a new CaptchaStore
        This has to be called inside render
        """
        try:
            reverse('captcha-image', args=('dummy',))
        except NoReverseMatch:
            raise ImproperlyConfigured('Make sure you\'ve included captcha.urls as explained in the INSTALLATION section on http://readthedocs.org/docs/django-simple-captcha/en/latest/usage.html#installation')

        challenge, response = settings.get_challenge()()

        key = create(challenge, response)

        # these can be used by format_output and render
        self._value = [key, u('')]
        self._key = key
        self.id_ = self.build_attrs(attrs).get('id', None)
Example #19
0
    def test_manga_report_form_anonymous_user(self):
        from captcha.conf import settings
        from captcha.models import CaptchaStore

        challenge, response = settings.get_challenge()()
        store = CaptchaStore.objects.create(challenge=challenge, response=response)

        self.request.user = AnonymousUser()

        form = MangaReportForm(request=self.request, manga=self.manga, data={
            'type': ReportMangaType.COPYRIGHT,
            'check': 'on',
            'captcha_0': store.hashkey,
            'captcha_1': store.response,
        })
        self.assertTrue(form.is_valid())

        report_manga = form.save()
        self.assertEqual(report_manga.created_by, None)
        self.assertEqual(report_manga.ip_address, '127.0.0.1')
        self.assertEqual(report_manga.weight, MangaReportForm.ANONYMOUS_USER_REPORT_WEIGHT)
        self.assertEqual(report_manga.manga, self.manga)
        self.assertEqual(report_manga.type, ReportMangaType.COPYRIGHT)
Example #20
0
    def generate_key(cls, generator=None):
        challenge, response = captcha_settings.get_challenge(generator)()
        store = cls.objects.create(challenge=challenge, response=response)

        return store.hashkey
 def generate_key(cls, generator=None):
     challenge, response = captcha_settings.get_challenge(generator)()
     captcha_document = cls(challenge=challenge, response=response)
     captcha_document.save()
     logger.debug("Created a captcha doc in MongoDB %r", captcha_document)
     return captcha_document.hashkey
Example #22
0
    def generate_key(cls):
        challenge, response = captcha_settings.get_challenge()()
        store = cls.objects.create(challenge=challenge, response=response)

        return store.hashkey
def code_new_key(request):
    challenge, response = settings.get_challenge()()
    store = CaptchaStore.objects.create(challenge=challenge, response=response)
    return HttpResponse(store.hashkey)
Example #24
0
 def __init__(self):
     challenge, response = captcha_settings.get_challenge()()
     store = CaptchaStore.objects.create(challenge=challenge, response=response)
     self.key = store.hashkey