TorbatYar/backend/shared-lib/shared/phone.py
Mortezakoohjani 800b0ba2c5 Deploy TorbatYar for torbatyar.ir with nginx multi-tenant routing.
Wire production domain, CORS for tenant subdomains, celery volume mounts, and nginx reverse proxy configs for apex, API, identity, auth, and wildcard tenants.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-21 21:43:33 +03:30

40 lines
1.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""نرمال‌سازی شماره موبایل و کد OTP — مشترک بین Core و Identity."""
from __future__ import annotations
import re
MOBILE_PATTERN = re.compile(r"^09\d{9}$")
def normalize_otp_digits(value: str | None) -> str | None:
if value is None:
return None
s = str(value).strip()
if not s:
return None
persian = "۰۱۲۳۴۵۶۷۸۹"
arabic = "٠١٢٣٤٥٦٧٨٩"
latin = "0123456789"
trans = str.maketrans(persian + arabic, latin + latin)
s = s.translate(trans)
s = "".join(c for c in s if c.isdigit())
return s if s else None
def normalize_mobile(raw: str) -> str:
"""تبدیل به فرمت 09xxxxxxxxx."""
digits = normalize_otp_digits(raw)
if not digits:
raise ValueError("شماره موبایل نامعتبر است")
if digits.startswith("0098"):
digits = digits[4:]
elif digits.startswith("98") and len(digits) >= 12:
digits = digits[2:]
if len(digits) == 10 and not digits.startswith("0"):
digits = "0" + digits
if len(digits) == 11 and digits.startswith("0"):
if not MOBILE_PATTERN.match(digits):
raise ValueError("شماره موبایل باید با 09 شروع شود و ۱۱ رقم باشد")
return digits
raise ValueError("شماره موبایل باید با 09 شروع شود و ۱۱ رقم باشد")