add username or email check api
This commit is contained in:
@@ -11,11 +11,15 @@ class UserLoginSerializer(serializers.Serializer):
|
|||||||
tfa_code = serializers.CharField(min_length=6, max_length=6, required=False, allow_null=True)
|
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):
|
class UserRegisterSerializer(serializers.Serializer):
|
||||||
username = serializers.CharField(max_length=30)
|
username = serializers.CharField(max_length=30)
|
||||||
password = serializers.CharField(max_length=30, min_length=6)
|
password = serializers.CharField(max_length=30, min_length=6)
|
||||||
email = serializers.EmailField(max_length=254)
|
email = serializers.EmailField(max_length=30)
|
||||||
captcha = serializers.CharField(max_length=4, min_length=4)
|
captcha = serializers.CharField(max_length=4, min_length=1)
|
||||||
|
|
||||||
|
|
||||||
class UserChangePasswordSerializer(serializers.Serializer):
|
class UserChangePasswordSerializer(serializers.Serializer):
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ from django.conf.urls import url
|
|||||||
|
|
||||||
from ..views.oj import (ApplyResetPasswordAPI, ResetPasswordAPI,
|
from ..views.oj import (ApplyResetPasswordAPI, ResetPasswordAPI,
|
||||||
UserChangePasswordAPI, UserRegisterAPI,
|
UserChangePasswordAPI, UserRegisterAPI,
|
||||||
UserLoginAPI, UserLogoutAPI)
|
UserLoginAPI, UserLogoutAPI, UsernameOrEmailCheck)
|
||||||
|
|
||||||
from utils.captcha.views import CaptchaAPIView
|
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"^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"^reset_password/?$", ResetPasswordAPI.as_view(), name="apply_reset_password_api"),
|
||||||
url(r"^captcha/?$", CaptchaAPIView.as_view(), name="show_captcha"),
|
url(r"^captcha/?$", CaptchaAPIView.as_view(), name="show_captcha"),
|
||||||
|
url(r"^check_username_or_email", UsernameOrEmailCheck.as_view(), name="check_username_or_email")
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ from ..models import User, UserProfile
|
|||||||
from ..serializers import (ApplyResetPasswordSerializer,
|
from ..serializers import (ApplyResetPasswordSerializer,
|
||||||
ResetPasswordSerializer,
|
ResetPasswordSerializer,
|
||||||
UserChangePasswordSerializer, UserLoginSerializer,
|
UserChangePasswordSerializer, UserLoginSerializer,
|
||||||
UserRegisterSerializer)
|
UserRegisterSerializer, UsernameOrEmailCheckSerializer)
|
||||||
from ..tasks import send_email_async
|
from ..tasks import send_email_async
|
||||||
|
|
||||||
|
|
||||||
@@ -58,6 +58,27 @@ class UserLogoutAPI(APIView):
|
|||||||
return self.success({})
|
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):
|
class UserRegisterAPI(APIView):
|
||||||
@validate_serializer(UserRegisterSerializer)
|
@validate_serializer(UserRegisterSerializer)
|
||||||
def post(self, request):
|
def post(self, request):
|
||||||
@@ -66,7 +87,7 @@ class UserRegisterAPI(APIView):
|
|||||||
"""
|
"""
|
||||||
data = request.data
|
data = request.data
|
||||||
captcha = Captcha(request)
|
captcha = Captcha(request)
|
||||||
if not captcha.check(data["captcha"]):
|
if not captcha.validate(data["captcha"]):
|
||||||
return self.error("Invalid captcha")
|
return self.error("Invalid captcha")
|
||||||
try:
|
try:
|
||||||
User.objects.get(username=data["username"])
|
User.objects.get(username=data["username"])
|
||||||
|
|||||||
Reference in New Issue
Block a user