add username or email check api

This commit is contained in:
zemal 2017-08-19 17:25:39 +08:00
parent d1767e775d
commit 0647312124
3 changed files with 31 additions and 5 deletions

View File

@ -11,11 +11,15 @@ class UserLoginSerializer(serializers.Serializer):
tfa_code = serializers.CharField(min_length=6, max_length=6, required=False, allow_null=True)
class UsernameOrEmailCheckSerializer(serializers.Serializer):
username = serializers.CharField(max_length=30, required=False)
email = serializers.EmailField(max_length=30, required=False)
class UserRegisterSerializer(serializers.Serializer):
username = serializers.CharField(max_length=30)
password = serializers.CharField(max_length=30, min_length=6)
email = serializers.EmailField(max_length=254)
captcha = serializers.CharField(max_length=4, min_length=4)
email = serializers.EmailField(max_length=30)
captcha = serializers.CharField(max_length=4, min_length=1)
class UserChangePasswordSerializer(serializers.Serializer):

View File

@ -2,7 +2,7 @@ from django.conf.urls import url
from ..views.oj import (ApplyResetPasswordAPI, ResetPasswordAPI,
UserChangePasswordAPI, UserRegisterAPI,
UserLoginAPI, UserLogoutAPI)
UserLoginAPI, UserLogoutAPI, UsernameOrEmailCheck)
from utils.captcha.views import CaptchaAPIView
@ -14,4 +14,5 @@ urlpatterns = [
url(r"^apply_reset_password/?$", ApplyResetPasswordAPI.as_view(), name="apply_reset_password_api"),
url(r"^reset_password/?$", ResetPasswordAPI.as_view(), name="apply_reset_password_api"),
url(r"^captcha/?$", CaptchaAPIView.as_view(), name="show_captcha"),
url(r"^check_username_or_email", UsernameOrEmailCheck.as_view(), name="check_username_or_email")
]

View File

@ -16,7 +16,7 @@ from ..models import User, UserProfile
from ..serializers import (ApplyResetPasswordSerializer,
ResetPasswordSerializer,
UserChangePasswordSerializer, UserLoginSerializer,
UserRegisterSerializer)
UserRegisterSerializer, UsernameOrEmailCheckSerializer)
from ..tasks import send_email_async
@ -58,6 +58,27 @@ class UserLogoutAPI(APIView):
return self.success({})
class UsernameOrEmailCheck(APIView):
@validate_serializer(UsernameOrEmailCheckSerializer)
def post(self, request):
"""
check username or email is duplicate
"""
data = request.data
# True means OK.
result = {
"username": True,
"email": True
}
if data.get("username"):
if User.objects.filter(username=data["username"]).exists():
result["username"] = False
if data.get("email"):
if User.objects.filter(email=data["email"]).exists():
result["email"] = False
return self.success(result)
class UserRegisterAPI(APIView):
@validate_serializer(UserRegisterSerializer)
def post(self, request):
@ -66,7 +87,7 @@ class UserRegisterAPI(APIView):
"""
data = request.data
captcha = Captcha(request)
if not captcha.check(data["captcha"]):
if not captcha.validate(data["captcha"]):
return self.error("Invalid captcha")
try:
User.objects.get(username=data["username"])