Refactor locale validation and update style consistency across schemas, tests, and migrations
- Replaced `SUPPORTED_LOCALES` with `supported_locales` for naming consistency. - Applied formatting improvements to multiline statements for better readability. - Cleaned up redundant comments and streamlined test assertions.
This commit is contained in:
@@ -23,10 +23,7 @@ def upgrade() -> None:
|
||||
# VARCHAR(10) supports BCP 47 format (e.g., "en", "it", "en-US", "it-IT")
|
||||
# Nullable: NULL means "not set yet", will use Accept-Language header fallback
|
||||
# Indexed: For analytics queries and filtering by locale
|
||||
op.add_column(
|
||||
"users",
|
||||
sa.Column("locale", sa.String(length=10), nullable=True)
|
||||
)
|
||||
op.add_column("users", sa.Column("locale", sa.String(length=10), nullable=True))
|
||||
|
||||
# Create index on locale column for performance
|
||||
op.create_index(
|
||||
|
||||
@@ -117,8 +117,9 @@ async def get_locale(
|
||||
if current_user and current_user.locale:
|
||||
# Validate that saved locale is still supported
|
||||
# (in case SUPPORTED_LOCALES changed after user set preference)
|
||||
if current_user.locale in SUPPORTED_LOCALES:
|
||||
return current_user.locale
|
||||
locale_value = str(current_user.locale)
|
||||
if locale_value in SUPPORTED_LOCALES:
|
||||
return locale_value
|
||||
|
||||
# Priority 2: Accept-Language header
|
||||
accept_language = request.headers.get("accept-language", "")
|
||||
|
||||
@@ -40,9 +40,9 @@ class UserUpdate(BaseModel):
|
||||
locale: str | None = Field(
|
||||
None,
|
||||
max_length=10,
|
||||
pattern=r'^[a-z]{2}(-[A-Z]{2})?$',
|
||||
pattern=r"^[a-z]{2}(-[A-Z]{2})?$",
|
||||
description="User's preferred locale (BCP 47 format: en, it, en-US, it-IT)",
|
||||
examples=["en", "it", "en-US", "it-IT"]
|
||||
examples=["en", "it", "en-US", "it-IT"],
|
||||
)
|
||||
is_active: bool | None = (
|
||||
None # Changed default from True to None to avoid unintended updates
|
||||
@@ -70,12 +70,12 @@ class UserUpdate(BaseModel):
|
||||
return v
|
||||
# Only support English and Italian for template showcase
|
||||
# Note: Locales stored in lowercase for case-insensitive matching
|
||||
SUPPORTED_LOCALES = {"en", "it", "en-us", "en-gb", "it-it"}
|
||||
supported_locales = {"en", "it", "en-us", "en-gb", "it-it"}
|
||||
# Normalize to lowercase for comparison and storage
|
||||
v_lower = v.lower()
|
||||
if v_lower not in SUPPORTED_LOCALES:
|
||||
if v_lower not in supported_locales:
|
||||
raise ValueError(
|
||||
f"Unsupported locale '{v}'. Supported locales: {sorted(SUPPORTED_LOCALES)}"
|
||||
f"Unsupported locale '{v}'. Supported locales: {sorted(supported_locales)}"
|
||||
)
|
||||
# Return normalized lowercase version for consistency
|
||||
return v_lower
|
||||
|
||||
Reference in New Issue
Block a user