76 lines
2.2 KiB
Python
76 lines
2.2 KiB
Python
from rest_framework.decorators import permission_classes
|
|
from rest_framework.parsers import MultiPartParser, FormParser
|
|
from rest_framework.permissions import AllowAny
|
|
from rest_framework.views import APIView
|
|
|
|
from rest_framework.response import Response
|
|
from rest_framework import status
|
|
|
|
from django.core.mail import EmailMessage
|
|
|
|
import user.models
|
|
# import os
|
|
from utils import RandCode
|
|
|
|
from django.core.cache import caches
|
|
|
|
# 获取不同的缓存实例
|
|
default_cache = caches['default']
|
|
session_cache = caches['session']
|
|
celery_cache = caches['celery']
|
|
|
|
from ..models import FUser
|
|
|
|
class SendUserEmail(APIView):
|
|
permission_classes = [AllowAny]
|
|
def post(self, request):
|
|
to_email = request.data.get('to_email', None)
|
|
print(to_email)
|
|
|
|
if to_email is None:
|
|
return Response(status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
subject = 'test邮件'
|
|
code = RandCode.get_digit_characters_code_8()
|
|
body = f"你的验证码为 {code}, 该验证码仅用于账号注册, 十分钟内有效."
|
|
from_email = 'cs10086086@qq.com'
|
|
to_email = [to_email]
|
|
|
|
default_cache.set(f"register_{to_email[0]}", code, timeout=600)
|
|
|
|
email = EmailMessage(
|
|
subject=subject,
|
|
body=body,
|
|
from_email=from_email,
|
|
to=to_email,
|
|
)
|
|
|
|
email.send()
|
|
return Response('邮件发送成功!', status=status.HTTP_201_CREATED)
|
|
|
|
class RegisterByEmail(APIView):
|
|
permission_classes = [AllowAny]
|
|
def post(self, request):
|
|
code = request.data.get('code', None)
|
|
to_email = request.data.get('to_email', None)
|
|
|
|
if code is None or to_email is None:
|
|
return Response(status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
vcode = default_cache.get(f"register_{to_email}")
|
|
if vcode is None:
|
|
return Response(status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
if code == vcode:
|
|
password = RandCode.get_alphanumeric_characters_code_8()
|
|
FUser.objects.create_user(
|
|
username=to_email,
|
|
email=to_email,
|
|
password=password
|
|
)
|
|
return Response("创建成功.",status=status.HTTP_200_OK)
|
|
else:
|
|
return Response(status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|