修复验证码开关

pull/47/head
zghmvp 2022-04-11 16:21:15 +08:00
parent 75174311c8
commit 4abe392313
1 changed files with 15 additions and 13 deletions

View File

@ -11,6 +11,7 @@ import hashlib
from datetime import datetime, timedelta from datetime import datetime, timedelta
from captcha.views import CaptchaStore, captcha_image from captcha.views import CaptchaStore, captcha_image
from django.conf import settings
from django.contrib import auth from django.contrib import auth
from django.contrib.auth import login from django.contrib.auth import login
from django.shortcuts import redirect from django.shortcuts import redirect
@ -56,7 +57,7 @@ class LoginSerializer(TokenObtainPairSerializer):
登录的序列化器: 登录的序列化器:
重写djangorestframework-simplejwt的序列化器 重写djangorestframework-simplejwt的序列化器
""" """
captcha = serializers.CharField(max_length=6) captcha = serializers.CharField(max_length=6, required=False, allow_null=True)
class Meta: class Meta:
model = Users model = Users
@ -68,18 +69,19 @@ class LoginSerializer(TokenObtainPairSerializer):
} }
def validate_captcha(self, captcha): def validate_captcha(self, captcha):
self.image_code = CaptchaStore.objects.filter( if settings.CAPTCHA_STATE is True:
id=self.initial_data['captchaKey']).first() self.image_code = CaptchaStore.objects.filter(
five_minute_ago = datetime.now() - timedelta(hours=0, minutes=5, seconds=0) id=self.initial_data['captchaKey']).first()
if self.image_code and five_minute_ago > self.image_code.expiration: five_minute_ago = datetime.now() - timedelta(hours=0, minutes=5, seconds=0)
self.image_code and self.image_code.delete() if self.image_code and five_minute_ago > self.image_code.expiration:
raise CustomValidationError('验证码过期') self.image_code and self.image_code.delete()
else: raise CustomValidationError('验证码过期')
if self.image_code and (self.image_code.response == captcha or self.image_code.challenge == captcha): else:
self.image_code and self.image_code.delete() if self.image_code and (self.image_code.response == captcha or self.image_code.challenge == captcha):
else: self.image_code and self.image_code.delete()
self.image_code and self.image_code.delete() else:
raise CustomValidationError("图片验证码错误") self.image_code and self.image_code.delete()
raise CustomValidationError("图片验证码错误")
def validate(self, attrs): def validate(self, attrs):
data = super().validate(attrs) data = super().validate(attrs)