From 5500badc6a2d99de41e11d954ed1dfffa0ddfb18 Mon Sep 17 00:00:00 2001 From: Amirhossein Khalili Date: Sun, 24 May 2026 11:01:50 +0330 Subject: [PATCH] refactor(users): replace escaped persian auth messages --- apps/users/api/serializers.py | 10 +++++----- apps/users/services/auth.py | 30 +++++++++++++++--------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/apps/users/api/serializers.py b/apps/users/api/serializers.py index b7cfb0b..ea45fcb 100644 --- a/apps/users/api/serializers.py +++ b/apps/users/api/serializers.py @@ -8,11 +8,11 @@ from apps.users.email_identity import normalize_email_identity User = get_user_model() -INVALID_MOBILE_FORMAT_MESSAGE = "\u0641\u0631\u0645\u062a \u0634\u0645\u0627\u0631\u0647 \u0645\u0648\u0628\u0627\u06cc\u0644 \u0646\u0627\u062f\u0631\u0633\u062a \u0627\u0633\u062a." -INVALID_MOBILE_NUMBER_MESSAGE = "\u0634\u0645\u0627\u0631\u0647 \u0645\u0648\u0628\u0627\u06cc\u0644 \u0645\u0639\u062a\u0628\u0631 \u0646\u06cc\u0633\u062a." -PASSWORD_MISMATCH_MESSAGE = "\u0631\u0645\u0632 \u0639\u0628\u0648\u0631 \u0645\u0637\u0627\u0628\u0642\u062a \u0646\u062f\u0627\u0631\u062f." -NEW_PASSWORD_MISMATCH_MESSAGE = "\u0631\u0645\u0632 \u0639\u0628\u0648\u0631 \u062c\u062f\u06cc\u062f \u0648 \u062a\u06a9\u0631\u0627\u0631 \u0622\u0646 \u0645\u0637\u0627\u0628\u0642\u062a \u0646\u062f\u0627\u0631\u0646\u062f." -PASSWORD_REUSE_MESSAGE = "\u0631\u0645\u0632 \u0639\u0628\u0648\u0631 \u062c\u062f\u06cc\u062f \u0646\u0628\u0627\u06cc\u062f \u0628\u0627 \u0631\u0645\u0632 \u0639\u0628\u0648\u0631 \u0642\u0628\u0644\u06cc \u06cc\u06a9\u0633\u0627\u0646 \u0628\u0627\u0634\u062f." +INVALID_MOBILE_FORMAT_MESSAGE = "فرمت شماره موبایل نادرست است." +INVALID_MOBILE_NUMBER_MESSAGE = "شماره موبایل معتبر نیست." +PASSWORD_MISMATCH_MESSAGE = "رمز عبور مطابقت ندارد." +NEW_PASSWORD_MISMATCH_MESSAGE = "رمز عبور جدید و تکرار آن مطابقت ندارند." +PASSWORD_REUSE_MESSAGE = "رمز عبور جدید نباید با رمز عبور قبلی یکسان باشد." def _raise_password_validation_error(password: str, *, user, field_name: str) -> None: diff --git a/apps/users/services/auth.py b/apps/users/services/auth.py index b8ee019..f0939ad 100644 --- a/apps/users/services/auth.py +++ b/apps/users/services/auth.py @@ -18,7 +18,7 @@ from apps.users.utils import record_login_attempt User = get_user_model() USER_ALREADY_EXISTS_MESSAGE = "User already exists." -PASSWORD_REUSE_MESSAGE = "\u0631\u0645\u0632 \u0639\u0628\u0648\u0631 \u062c\u062f\u06cc\u062f \u0646\u0628\u0627\u06cc\u062f \u0628\u0627 \u0631\u0645\u0632 \u0639\u0628\u0648\u0631 \u0642\u0628\u0644\u06cc \u06cc\u06a9\u0633\u0627\u0646 \u0628\u0627\u0634\u062f." +PASSWORD_REUSE_MESSAGE = "رمز عبور جدید نباید با رمز عبور قبلی یکسان باشد." OTP_EXPIRY_SECONDS = 120 @@ -55,15 +55,15 @@ def register_user_with_password(mobile, password): def register_user_with_otp(mobile, code, password, first_name="", last_name=""): """Business logic for verifying OTP and registering a user.""" if User.objects.filter(mobile=mobile).exists(): - raise ValidationError({"mobile": "\u0627\u06cc\u0646 \u0634\u0645\u0627\u0631\u0647 \u0642\u0628\u0644\u0627\u064b \u062b\u0628\u062a \u0634\u062f\u0647 \u0627\u0633\u062a."}) + raise ValidationError({"mobile": "این شماره قبلاً ثبت شده است."}) redis_conn = get_redis_connection("default") stored_code = redis_conn.get(f"verification_code:{mobile}") if not stored_code: - raise ValidationError({"code": "\u06a9\u062f \u062a\u0623\u06cc\u06cc\u062f \u06cc\u0627\u0641\u062a \u0646\u0634\u062f."}) + raise ValidationError({"code": "کد تأیید یافت نشد."}) if stored_code.decode("utf-8") != code: - raise ValidationError({"code": "\u06a9\u062f \u062a\u0623\u06cc\u06cc\u062f \u0627\u0634\u062a\u0628\u0627\u0647 \u0627\u0633\u062a."}) + raise ValidationError({"code": "کد تأیید اشتباه است."}) user = User.objects.create_user( mobile=mobile, @@ -84,10 +84,10 @@ def generate_and_send_otp(mobile, mode): user_exists = User.objects.filter(mobile=mobile).exists() if mode == "register" and user_exists: - raise ValidationError({"mobile": "\u0627\u06cc\u0646 \u0634\u0645\u0627\u0631\u0647 \u0642\u0628\u0644\u0627\u064b \u062b\u0628\u062a\u200c\u0646\u0627\u0645 \u0634\u062f\u0647 \u0627\u0633\u062a."}) + raise ValidationError({"mobile": "این شماره قبلاً ثبت‌نام شده است."}) if mode in ["login", "forget_password"] and not user_exists: - raise ValidationError({"mobile": "\u0627\u06cc\u0646 \u0634\u0645\u0627\u0631\u0647 \u06cc\u0627\u0641\u062a \u0646\u0634\u062f."}) + raise ValidationError({"mobile": "این شماره یافت نشد."}) verification_code = "".join(random.choices(string.digits, k=5)) @@ -109,10 +109,10 @@ def login_with_password(mobile, password, request=None): if not user or not user.check_password(password): record_login_attempt(request, user, LoginAttempt.StatusType.FAILED) - raise ValidationError({"detail": "\u0634\u0645\u0627\u0631\u0647 \u0645\u0648\u0628\u0627\u06cc\u0644 \u06cc\u0627 \u0631\u0645\u0632 \u0639\u0628\u0648\u0631 \u0627\u0634\u062a\u0628\u0627\u0647 \u0627\u0633\u062a."}) + raise ValidationError({"detail": "شماره موبایل یا رمز عبور اشتباه است."}) if not user.is_active: - raise ValidationError({"detail": "\u062d\u0633\u0627\u0628 \u06a9\u0627\u0631\u0628\u0631\u06cc \u0634\u0645\u0627 \u063a\u06cc\u0631\u0641\u0639\u0627\u0644 \u0634\u062f\u0647 \u0627\u0633\u062a."}) + raise ValidationError({"detail": "حساب کاربری شما غیرفعال شده است."}) record_login_attempt(request, user, LoginAttempt.StatusType.SUCCESS) return get_tokens_for_user(user) @@ -125,7 +125,7 @@ def login_with_otp(mobile, code, request=None): if not stored_code or stored_code.decode("utf-8") != code: record_login_attempt(request, None, LoginAttempt.StatusType.FAILED) - raise ValidationError({"code": "\u06a9\u062f \u062a\u0627\u06cc\u06cc\u062f \u0646\u0627\u0645\u0639\u062a\u0628\u0631 \u0627\u0633\u062a \u06cc\u0627 \u0645\u0646\u0642\u0636\u06cc \u0634\u062f\u0647 \u0627\u0633\u062a."}) + raise ValidationError({"code": "کد تایید نامعتبر است یا منقضی شده است."}) user, created = User.objects.get_or_create(mobile=mobile) if created: @@ -133,7 +133,7 @@ def login_with_otp(mobile, code, request=None): user.save() if not user.is_active: - raise ValidationError({"detail": "\u062d\u0633\u0627\u0628 \u06a9\u0627\u0631\u0628\u0631\u06cc \u0634\u0645\u0627 \u063a\u06cc\u0631\u0641\u0639\u0627\u0644 \u0634\u062f\u0647 \u0627\u0633\u062a."}) + raise ValidationError({"detail": "حساب کاربری شما غیرفعال شده است."}) record_login_attempt(request, user, LoginAttempt.StatusType.SUCCESS) redis_conn.delete(f"verification_code:{mobile}") @@ -145,13 +145,13 @@ def reset_password_with_otp(mobile, code, password): """Verify OTP and change forgotten password.""" user = User.objects.filter(mobile=mobile).first() if not user: - raise ValidationError({"mobile": "\u06a9\u0627\u0631\u0628\u0631\u06cc \u0628\u0627 \u0627\u06cc\u0646 \u0634\u0645\u0627\u0631\u0647 \u06cc\u0627\u0641\u062a \u0646\u0634\u062f."}) + raise ValidationError({"mobile": "کاربری با این شماره یافت نشد."}) redis_conn = get_redis_connection("default") stored_code = redis_conn.get(f"verification_code:{mobile}") if not stored_code or stored_code.decode("utf-8") != code: - raise ValidationError({"code": "\u06a9\u062f \u062a\u0627\u06cc\u06cc\u062f \u0646\u0627\u0645\u0639\u062a\u0628\u0631 \u0627\u0633\u062a \u06cc\u0627 \u0645\u0646\u0642\u0636\u06cc \u0634\u062f\u0647 \u0627\u0633\u062a."}) + raise ValidationError({"code": "کد تایید نامعتبر است یا منقضی شده است."}) _validate_new_password(password, user=user, field_name="password") @@ -167,7 +167,7 @@ def reset_password_with_otp(mobile, code, password): def change_password(user, old_password, new_password): """Change password for an already authenticated user.""" if not user.check_password(old_password): - raise ValidationError({"old_password": "\u0631\u0645\u0632 \u0639\u0628\u0648\u0631 \u0641\u0639\u0644\u06cc \u0627\u0634\u062a\u0628\u0627\u0647 \u0627\u0633\u062a."}) + raise ValidationError({"old_password": "رمز عبور فعلی اشتباه است."}) _validate_new_password(new_password, user=user, field_name="new_password") @@ -182,9 +182,9 @@ def change_password(user, old_password, new_password): def logout_user(refresh_token_str): """Blacklist the user's refresh token.""" if not refresh_token_str: - raise ValidationError({"refresh": "\u062a\u0648\u06a9\u0646 \u0631\u0641\u0631\u0634 \u0627\u0644\u0632\u0627\u0645\u06cc \u0627\u0633\u062a."}) + raise ValidationError({"refresh": "توکن رفرش الزامی است."}) try: token = RefreshToken(refresh_token_str) token.blacklist() except TokenError: - raise ValidationError({"detail": "\u062a\u0648\u06a9\u0646 \u0646\u0627\u0645\u0639\u062a\u0628\u0631 \u0627\u0633\u062a \u06cc\u0627 \u0642\u0628\u0644\u0627 \u0645\u0646\u0642\u0636\u06cc \u0634\u062f\u0647 \u0627\u0633\u062a."}) + raise ValidationError({"detail": "توکن نامعتبر است یا قبلا منقضی شده است."})