def test_empty_pool_fallback(self): __current_test_get_from_pool_setting = settings.CAPTCHA_GET_FROM_POOL settings.CAPTCHA_GET_FROM_POOL = True CaptchaStore.objects.all().delete() # Delete objects created during SetUp with LogCapture() as l: CaptchaStore.pick() l.check(('captcha.models', 'ERROR', "Couldn't get a captcha from pool, generating"),) self.assertEqual(CaptchaStore.objects.count(), 1) settings.CAPTCHA_GET_FROM_POOL = __current_test_get_from_pool_setting
def get(self, request, *args, **kwargs): new_key = CaptchaStore.pick() response = { 'key': new_key, 'image_url': request.build_absolute_uri(location=captcha_image_url(new_key)), } return Response(response)
def refresh_captcha(request): new_key = CaptchaStore.pick() to_json_response = { "hashkey": new_key, "image_url": captcha_image_url(new_key), } return HttpResponse(json.dumps(to_json_response), content_type="application/json")
def get(self, request): new_key = CaptchaStore.pick() to_json_response = { "key": new_key, "image_url": captcha_image_url(new_key), "audio_url": captcha_audio_url(new_key) if ca_settings.CAPTCHA_FLITE_PATH else None, } return SuccessResponse(to_json_response)
def get(self, request): new_key = CaptchaStore.pick() to_json_response = { 'key': new_key, 'image_url': captcha_image_url(new_key), 'membership': organizations_domains_hashes(new_key), } return Response(to_json_response)
def create_captcha(request): if request.method == "GET": from captcha.helpers import captcha_image_url hashkey = CaptchaStore.pick() to_json_response = { 'hide_hashkey': hashkey, 'captcha': 'captcha', 'image_url': settings.URL + captcha_image_url(hashkey) } return Response(to_json_response)
def get(self, *args, **kwargs): new_key = CaptchaStore.pick() captcha_response_serializer = CaptchaResponseSerializer( data={ 'key': new_key, 'image_url': new_key, }) captcha_response_serializer.is_valid(raise_exception=True) return Response(captcha_response_serializer.data)
def captcha_refresh(request): """ Return json with new captcha for ajax refresh request """ if not request.is_ajax(): raise Http404 new_key = CaptchaStore.pick() to_json_response = { 'key': new_key, 'image_url': captcha_image_url(new_key), } return HttpResponse(json.dumps(to_json_response), content_type='application/json')
def list(self, request): pp = self.get_serializer(data=request.GET) if pp.is_valid(): CaptchaStore.remove_expired() # 删除失效的验证码,过期时间为五分钟 captcha_key = CaptchaStore.pick() to_json_response = { 'key': captcha_key, 'url': captcha_image_url(captcha_key), } return Response(to_json_response) else: raise ParseError(pp.errors)
def captcha_refresh(request): # 内置的源码 if not request.is_ajax(): raise Http404 new_key = CaptchaStore.pick() to_json_response = { 'key': new_key, 'image_url': captcha_image_url(new_key), # 'audio_url': captcha_audio_url(new_key) if settings.CAPTCHA_FLITE_PATH else None } return HttpResponse(json.dumps(to_json_response), content_type='application/json')
def captcha_refresh(request): """ Return json with new captcha for ajax refresh request """ if not request.is_ajax(): raise Http404 new_key = CaptchaStore.pick() to_json_response = { "key": new_key, "image_url": captcha_image_url(new_key), "audio_url": captcha_audio_url(new_key) if settings.CAPTCHA_ESPEAK_PATH else None, } return HttpResponse(json.dumps(to_json_response), content_type="application/json")
def captcha_refresh(request): """ Return json with new captcha for ajax refresh request """ was_limited = getattr(request, 'limited', False) if was_limited: return HttpResponseForbidden() if not request.is_ajax(): raise Http404 new_key = CaptchaStore.pick() to_json_response = { 'key': new_key, 'image_url': captcha_image_url(new_key), } return HttpResponse(json.dumps(to_json_response), content_type='application/json')
def captcha_refresh(request): """ Return json with new captcha for ajax refresh request """ if not request.headers.get('x-requested-with') == 'XMLHttpRequest': raise Http404 new_key = CaptchaStore.pick() to_json_response = { "key": new_key, "image_url": captcha_image_url(new_key), "audio_url": captcha_audio_url(new_key) if settings.CAPTCHA_FLITE_PATH else None, } return HttpResponse(json.dumps(to_json_response), content_type="application/json")
def output_yzm(request): if request.method == "POST": print(request.POST) yzm = request.POST.get('yzm', '') hashkey = request.POST.get('code') # 根据key获取验证码对象 cap = CaptchaStore.objects.filter(hashkey=hashkey).first() if cap: #存在 if cap.response == yzm.lower(): return HttpResponse("验证成功") return HttpResponse("验证失败") else: # 生成hashkey和image_url new_key = CaptchaStore.pick() image_url = captcha_image_url(new_key) return render(request, 'app/vc.html', locals())
def post(self, request): json_data = json.loads(request.body.decode('utf-8')) form = SignupForm(json_data) if form.is_valid(): email = form.cleaned_data.get("email") if email: email=email.lower() user, created = User.objects.get_or_create( email=email, is_active=True ) OneTimePassword.objects.filter( user=user, is_active=True ).update(is_active=False) password = OneTimePassword.objects.create(user=user) token, created = Token.objects.get_or_create(user=user) send_mail( "{} - One-Time Password".format( settings.EMAIL_SUBJECT_PREFIX), password.one_time_password, settings.DEFAULT_FROM_EMAIL, [email] ) print("One Time Password", password.one_time_password) return Response({'token': token.key}) new_key = CaptchaStore.pick() to_json_response = { 'key': new_key, 'image_url': captcha_image_url(new_key), 'membership': organizations_domains_hashes(new_key), } return Response(to_json_response, status=status.HTTP_400_BAD_REQUEST)
def fetch_captcha_store(self, name, value, attrs=None, generator=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') if settings.CAPTCHA_GET_FROM_POOL: key = CaptchaStore.pick() else: key = CaptchaStore.generate_key(generator) # 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)
def test_get_from_pool(self): __current_test_get_from_pool_setting = settings.CAPTCHA_GET_FROM_POOL __current_test_get_from_pool_timeout_setting = settings.CAPTCHA_GET_FROM_POOL_TIMEOUT __current_test_timeout_setting = settings.CAPTCHA_TIMEOUT settings.CAPTCHA_GET_FROM_POOL = True settings.CAPTCHA_GET_FROM_POOL_TIMEOUT = 5 settings.CAPTCHA_TIMEOUT = 90 CaptchaStore.objects.all().delete() # Delete objects created during SetUp POOL_SIZE = 10 CaptchaStore.create_pool(count=POOL_SIZE) self.assertEqual(CaptchaStore.objects.count(), POOL_SIZE) pool = CaptchaStore.objects.values_list('hashkey', flat=True) random_pick = CaptchaStore.pick() self.assertIn(random_pick, pool) # pick() should not create any extra captcha self.assertEqual(CaptchaStore.objects.count(), POOL_SIZE) settings.CAPTCHA_GET_FROM_POOL = __current_test_get_from_pool_setting settings.CAPTCHA_GET_FROM_POOL_TIMEOUT = __current_test_get_from_pool_timeout_setting settings.CAPTCHA_TIMEOUT = __current_test_timeout_setting
def get(self, request): hash_key = CaptchaStore.pick() img_url = "/captcha/image/{}".format(hash_key) return JsonResponse({'hashkey': hash_key, "url": img_url})
def get(self, request, format=None): hashkey = CaptchaStore.pick() captcha_url = captcha_image_url(hashkey) return Response({'url': captcha_url}, status=status.HTTP_200_OK)
def refresh(request): new_key = CaptchaStore.pick() res = captcha_image_url(new_key) print(new_key) print(res) return HttpResponse(res)