44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
from rest_captcha.settings import api_settings as settings
|
|
from rest_framework import views
|
|
import uuid
|
|
from rest_captcha import utils
|
|
import base64
|
|
from rest_framework import response
|
|
from .utils import (
|
|
random_char_challenge,
|
|
generate_image
|
|
)
|
|
from django.core.cache import cache
|
|
|
|
|
|
class CustomizeRestCaptchaView(views.APIView):
|
|
"""
|
|
overriding RestCaptchaView to generate captcha image
|
|
"""
|
|
authentication_classes = () # noqa
|
|
permission_classes = ()
|
|
|
|
def post(self, request):
|
|
key = str(uuid.uuid4())
|
|
value = random_char_challenge(settings.CAPTCHA_LENGTH)
|
|
cache_key = utils.get_cache_key(key)
|
|
print(cache_key)
|
|
cache.set(cache_key, value, settings.CAPTCHA_TIMEOUT)
|
|
print(cache.get(cache_key))
|
|
|
|
# generate image
|
|
image_bytes = generate_image(value)
|
|
image_b64 = base64.b64encode(image_bytes)
|
|
|
|
data = {
|
|
settings.CAPTCHA_KEY: key,
|
|
settings.CAPTCHA_IMAGE: image_b64,
|
|
'image_type': 'image/png',
|
|
'image_decode': 'base64'
|
|
}
|
|
return response.Response(data)
|
|
|
|
def get(self, request):
|
|
key = cache.get("rest_captcha_9e3ca166-c2f8-41e8-8f19-aa6f520fc123.0")
|
|
return response.Response(key)
|