remove i18n

This commit is contained in:
virusdefender
2017-04-19 02:03:48 +08:00
parent 8128e076f2
commit ef9cdd9f75
9 changed files with 80 additions and 83 deletions

View File

@@ -4,7 +4,6 @@ from django.conf import settings
from django.contrib import auth
from django.core.exceptions import MultipleObjectsReturned
from django.utils.timezone import now
from django.utils.translation import ugettext as _
from otpauth import OtpAuth
from conf.models import WebsiteConfig
@@ -18,7 +17,7 @@ from ..serializers import (ApplyResetPasswordSerializer,
ResetPasswordSerializer,
UserChangePasswordSerializer, UserLoginSerializer,
UserRegisterSerializer)
from ..tasks import _send_email
from ..tasks import send_email_async
class UserLoginAPI(APIView):
@@ -33,7 +32,7 @@ class UserLoginAPI(APIView):
if user:
if not user.two_factor_auth:
auth.login(request, user)
return self.success(_("Succeeded"))
return self.success("Succeeded")
# `tfa_code` not in post data
if user.two_factor_auth and "tfa_code" not in data:
@@ -41,11 +40,11 @@ class UserLoginAPI(APIView):
if OtpAuth(user.tfa_token).valid_totp(data["tfa_code"]):
auth.login(request, user)
return self.success(_("Succeeded"))
return self.success("Succeeded")
else:
return self.error(_("Invalid two factor verification code"))
return self.error("Invalid two factor verification code")
else:
return self.error(_("Invalid username or password"))
return self.error("Invalid username or password")
# todo remove this, only for debug use
def get(self, request):
@@ -62,24 +61,24 @@ class UserRegisterAPI(APIView):
data = request.data
captcha = Captcha(request)
if not captcha.check(data["captcha"]):
return self.error(_("Invalid captcha"))
return self.error("Invalid captcha")
try:
User.objects.get(username=data["username"])
return self.error(_("Username already exists"))
return self.error("Username already exists")
except User.DoesNotExist:
pass
try:
User.objects.get(email=data["email"])
return self.error(_("Email already exists"))
return self.error("Email already exists")
# Some old data has duplicate email
except MultipleObjectsReturned:
return self.error(_("Email already exists"))
return self.error("Email already exists")
except User.DoesNotExist:
user = User.objects.create(username=data["username"], email=data["email"])
user.set_password(data["password"])
user.save()
UserProfile.objects.create(user=user)
return self.success(_("Succeeded"))
return self.success("Succeeded")
class UserChangePasswordAPI(APIView):
@@ -92,15 +91,15 @@ class UserChangePasswordAPI(APIView):
data = request.data
captcha = Captcha(request)
if not captcha.check(data["captcha"]):
return self.error(_("Invalid captcha"))
return self.error("Invalid captcha")
username = request.user.username
user = auth.authenticate(username=username, password=data["old_password"])
if user:
user.set_password(data["new_password"])
user.save()
return self.success(_("Succeeded"))
return self.success("Succeeded")
else:
return self.error(_("Invalid old password"))
return self.error("Invalid old password")
class ApplyResetPasswordAPI(APIView):
@@ -110,14 +109,14 @@ class ApplyResetPasswordAPI(APIView):
captcha = Captcha(request)
config = WebsiteConfig.objects.first()
if not captcha.check(data["captcha"]):
return self.error(_("Invalid captcha"))
return self.error("Invalid captcha")
try:
user = User.objects.get(email=data["email"])
except User.DoesNotExist:
return self.error(_("User does not exist"))
return self.error("User does not exist")
if user.reset_password_token_expire_time and 0 < (
user.reset_password_token_expire_time - now()).total_seconds() < 20 * 60:
return self.error(_("You can only reset password once per 20 minutes"))
return self.error("You can only reset password once per 20 minutes")
user.reset_password_token = rand_str()
user.reset_password_token_expire_time = now() + timedelta(minutes=20)
@@ -128,12 +127,12 @@ class ApplyResetPasswordAPI(APIView):
replace("{{ website_name }}", settings.WEBSITE_INFO["website_name"]). \
replace("{{ link }}", settings.WEBSITE_INFO["url"] + "/reset_password/t/" +
user.reset_password_token)
_send_email.delay(config.name,
user.email,
user.username,
config.name + " 登录信息找回邮件",
email_template)
return self.success(_("Succeeded"))
send_email_async.delay(config.name,
user.email,
user.username,
config.name + " 登录信息找回邮件",
email_template)
return self.success("Succeeded")
class ResetPasswordAPI(APIView):
@@ -142,14 +141,14 @@ class ResetPasswordAPI(APIView):
data = request.data
captcha = Captcha(request)
if not captcha.check(data["captcha"]):
return self.error(_("Invalid captcha"))
return self.error("Invalid captcha")
try:
user = User.objects.get(reset_password_token=data["token"])
except User.DoesNotExist:
return self.error(_("Token dose not exist"))
return self.error("Token dose not exist")
if 0 < (user.reset_password_token_expire_time - now()).total_seconds() < 30 * 60:
return self.error(_("Token expired"))
return self.error("Token expired")
user.reset_password_token = None
user.set_password(data["password"])
user.save()
return self.success(_("Succeeded"))
return self.success("Succeeded")