data: fix format_date

This commit is contained in:
Sina Atalay
2024-07-23 18:27:39 +03:00
parent 009875ef23
commit 409987cbac
3 changed files with 29 additions and 31 deletions

View File

@@ -73,17 +73,18 @@ def format_date(date: Date, date_style: Optional[str] = None) -> str:
"MONTH_ABBREVIATION": short_month_names[month - 1],
"MONTH": str(month),
"MONTH_IN_TWO_DIGITS": f"{month:02d}",
"YEAR": year,
"YEAR_IN_TWO_DIGITS": year[-2:],
"YEAR": str(year),
"YEAR_IN_TWO_DIGITS": str(year[-2:]),
}
translator = str.maketrans(placeholders)
if date_style is None:
date_style = locale_catalog["date_style"] # type: ignore
date_string = date_style.translate(translator) # type: ignore
for placeholder, value in placeholders.items():
date_style = date_style.replace(placeholder, value) # type: ignore
return date_string
date_string = date_style
return date_string # type: ignore
def compute_time_span_string(

View File

@@ -8,14 +8,16 @@ from typing import Annotated, Literal, Optional
import annotated_types as at
import pydantic
from .base import RenderCVBaseModelWithoutExtraKeys
class LocaleCatalog(RenderCVBaseModelWithoutExtraKeys):
class LocaleCatalog(pydantic.BaseModel):
"""This class is the data model of the locale catalog. The values of each field
updates the `locale_catalog` dictionary.
"""
model_config = pydantic.ConfigDict(
extra="forbid",
validate_default=True, # To initialize the locale catalog with the default values
)
phone_number_format: Optional[Literal["national", "international", "E164"]] = (
pydantic.Field(
default="national",
@@ -25,38 +27,44 @@ class LocaleCatalog(RenderCVBaseModelWithoutExtraKeys):
" If 'international', phone numbers are formatted with the country"
" code. The default value is 'national'."
),
validate_default=True, # To initialize the locale catalog with the default
)
)
date_style: Optional[str] = pydantic.Field(
default="MONTH_ABBREVIATION YEAR",
title="Date Style",
description=(
"The style of the date. The following placeholder can be used:\n-"
" FULL_MONTH_NAME: Full name of the month\n- MONTH_ABBREVIATION:"
" Abbreviation of the month\n- MONTH: Month as a number\n-"
" MONTH_IN_TWO_DIGITS: Month as a number in two digits\n- YEAR: Year as a"
" number\n- YEAR_IN_TWO_DIGITS: Year as a number in two digits\nThe default"
' value is "MONTH_ABBREVIATION YEAR".'
),
)
month: Optional[str] = pydantic.Field(
default="month",
title='Translation of "Month"',
description='Translation of the word "month" in the locale.',
validate_default=True, # To initialize the locale catalog with the default values
)
months: Optional[str] = pydantic.Field(
default="months",
title='Translation of "Months"',
description='Translation of the word "months" in the locale.',
validate_default=True, # To initialize the locale catalog with the default values
)
year: Optional[str] = pydantic.Field(
default="year",
title='Translation of "Year"',
description='Translation of the word "year" in the locale.',
validate_default=True, # To initialize the locale catalog with the default values
)
years: Optional[str] = pydantic.Field(
default="years",
title='Translation of "Years"',
description='Translation of the word "years" in the locale.',
validate_default=True, # To initialize the locale catalog with the default values
)
present: Optional[str] = pydantic.Field(
default="present",
title='Translation of "Present"',
description='Translation of the word "present" in the locale.',
validate_default=True, # To initialize the locale catalog with the default values
)
to: Optional[str] = pydantic.Field(
default="", # en dash
@@ -65,7 +73,6 @@ class LocaleCatalog(RenderCVBaseModelWithoutExtraKeys):
"The word or character used to indicate a range in the locale (e.g.,"
' "2020 - 2021").'
),
validate_default=True, # To initialize the locale catalog with the default values
)
abbreviations_for_months: Optional[
Annotated[list[str], at.Len(min_length=12, max_length=12)]
@@ -88,7 +95,6 @@ class LocaleCatalog(RenderCVBaseModelWithoutExtraKeys):
],
title="Abbreviations of Months",
description="Abbreviations of the months in the locale.",
validate_default=True, # to initialize the locale catalog with the default values
)
full_names_of_months: Optional[
Annotated[list[str], at.Len(min_length=12, max_length=12)]
@@ -109,19 +115,6 @@ class LocaleCatalog(RenderCVBaseModelWithoutExtraKeys):
],
title="Full Names of Months",
description="Full names of the months in the locale.",
validate_default=True, # to initialize the locale catalog with the default values
)
date_style: Optional[str] = pydantic.Field(
default="MONTH_ABBREVIATION YEAR",
title="Date Style",
description=(
"The style of the date. The following placeholder can be used:\n-"
" FULL_MONTH_NAME: Full name of the month\n- MONTH_ABBREVIATION:"
" Abbreviation of the month\n- MONTH: Month as a number\n-"
" MONTH_IN_TWO_DIGITS: Month as a number in two digits\n- YEAR: Year as a"
" number\n- YEAR_IN_TWO_DIGITS: Year as a number in two digits\nThe default"
' value is "MONTH_ABBREVIATION YEAR".'
),
)
@pydantic.field_validator(

View File

@@ -301,6 +301,10 @@ def test_dates(
assert entry_base.time_span_string == expected_time_span
def test_dates_style():
assert "TEST" == data.format_date(Date(2020, 1, 1), "TEST")
@pytest.mark.parametrize(
"date, expected_date_string",
[