deploy login & reCaptcha

This commit is contained in:
2025-05-04 15:24:28 +03:30
parent 3ab3fa2d13
commit 70fa849840
36 changed files with 494 additions and 5 deletions

View File

View File

@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View File

View File

@@ -0,0 +1,15 @@
from rest_captcha.serializers import RestCaptchaSerializer
from rest_captcha import utils
from rest_captcha.settings import api_settings
from django.core.cache import caches
cache = caches[api_settings.CAPTCHA_CACHE]
def noise_default(image, draw):
draw = utils.noise_dots(draw, image, api_settings.CAPTCHA_FOREGROUND_COLOR)
# draw = utils.noise_arcs(draw, image, api_settings.CAPTCHA_FOREGROUND_COLOR)
class HumanOnlyDataSerializer(RestCaptchaSerializer): # noqa
pass

View File

@@ -0,0 +1,8 @@
from django.urls import path, include
from .views import CustomizeRestCaptchaView
app_name = 'captcha_app'
urlpatterns = [
path('captcha/', CustomizeRestCaptchaView.as_view(), name='captcha')
]

View File

@@ -0,0 +1,71 @@
import os.path
import random
from rest_captcha.settings import api_settings as settings
from django.conf import settings as django_setting
from django.core.cache import caches
from rest_captcha import captcha
from PIL import ImageFont, ImageDraw, Image
from io import BytesIO as StringIO
cache = caches[settings.CAPTCHA_CACHE]
path = os.path.dirname(__file__) + '/' # noqa
def random_char_challenge(length):
chars = '123456789'
ret = ''
for i in range(length):
ret += random.choice(chars)
return ret.upper()
def generate_image(word):
font = ImageFont.load_default()
size = settings.CAPTCHA_IMAGE_SIZE
xpos = 2
from_top = 4
image = captcha.makeimg(size)
for char in word:
fgimage = Image.new('RGB', size, settings.CAPTCHA_FOREGROUND_COLOR)
charimage = Image.new('L', captcha.getsize(font, ' %s ' % char), '#000000')
chardraw = ImageDraw.Draw(charimage)
chardraw.text((0, 0), char, font=font, fill='#ffffff')
charimage = charimage.crop(charimage.getbbox())
maskimage = Image.new('L', size)
xpos2 = xpos + charimage.size[0]
from_top2 = from_top + charimage.size[1]
maskimage.paste(charimage, (xpos, from_top, xpos2, from_top2))
size = maskimage.size
image = Image.composite(fgimage, image, maskimage)
xpos = xpos + 2 + charimage.size[0]
if settings.CAPTCHA_IMAGE_SIZE:
# centering captcha on the image
tmpimg = captcha.makeimg(size)
xpos2 = int((size[0] - xpos) / 2)
from_top2 = int((size[1] - charimage.size[1]) / 2 - from_top)
tmpimg.paste(image, (xpos2, from_top2))
image = tmpimg.crop((0, 0, size[0], size[1]))
else:
image = image.crop((0, 0, xpos + 1, size[1]))
draw = ImageDraw.Draw(image)
# settings.FILTER_FUNCTION(image)
settings.NOISE_FUNCTION(image, draw)
out = StringIO()
image.save(out, 'PNG')
# image.save('ss.png', 'PNG')
content = out.getvalue()
out.seek(0)
out.close()
return content

View File

@@ -0,0 +1,43 @@
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)

6
apps/captcha_app/apps.py Normal file
View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class CaptchaAppConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'apps.captcha_app'

View File

@@ -0,0 +1,12 @@
from django.utils.translation import gettext_lazy as _
from rest_framework.exceptions import APIException
from rest_framework import status
class CaptchaFailed(APIException):
"""
raised exception when user entered wrong captcha code
"""
status_code = status.HTTP_403_FORBIDDEN
default_detail = _('Wrong Captcha')
default_code = 'wrong captcha'