Skip to content

Commit

Permalink
fix(app): Adjust defaults handling
Browse files Browse the repository at this point in the history
Knowledge of the defaults now resides in a more appropriate location,
reducing coupling
  • Loading branch information
alexpovel committed Sep 3, 2022
1 parent b46a41d commit 5a11a4c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
12 changes: 6 additions & 6 deletions ancv/data/models/resume.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ class Config:


class TemplateConfig(BaseModel):
template: str = "Sequential"
theme: str = "basic"
translation: str = "en"
ascii_only: bool = False
template: Optional[str]
theme: Optional[str]
translation: Optional[str]
ascii_only: Optional[bool]


class Meta(BaseModel):
Expand All @@ -134,8 +134,8 @@ class Config:
lastModified: Optional[datetime] = Field(
None, description="Using ISO 8601 with YYYY-MM-DDThh:mm:ss"
)
config: TemplateConfig = Field(
TemplateConfig(),
config: Optional[TemplateConfig] = Field(
None,
alias="ancv",
description="Template configuration to control display",
)
Expand Down
31 changes: 20 additions & 11 deletions ancv/visualization/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
ResumeItem,
ResumeItemContainer,
ResumeSchema,
TemplateConfig,
VolunteerItem,
WorkItem,
)
Expand Down Expand Up @@ -92,30 +93,38 @@ def subclasses(cls) -> dict[str, type["Template"]]:

@classmethod
def from_model_config(cls, model: ResumeSchema) -> "Template":
if (config := model.meta.config) is None:
config = TemplateConfig()

if (theme_name := config.theme) is None:
theme_name = "basic"
try:
theme = THEMES[model.meta.config.theme]
theme = THEMES[theme_name]
except KeyError as e:
raise ResumeConfigError(f"Unknown theme: {model.meta.config.theme}") from e
raise ResumeConfigError(f"Unknown theme: {theme_name}") from e

if (translation_name := config.translation) is None:
translation_name = "en"
try:
translation = TRANSLATIONS[model.meta.config.translation]
translation = TRANSLATIONS[translation_name]
except KeyError as e:
raise ResumeConfigError(
f"Unknown translation: {model.meta.config.translation}"
) from e
raise ResumeConfigError(f"Unknown translation: {translation_name}") from e

if (template_name := config.template) is None:
template_name = Sequential.__name__
try:
template = cls.subclasses()[model.meta.config.template]
template = cls.subclasses()[template_name]
except KeyError as e:
raise ResumeConfigError(
f"Unknown template: {model.meta.config.template}"
) from e
raise ResumeConfigError(f"Unknown template: {template_name}") from e

if (ascii_only := config.ascii_only) is None:
ascii_only = False

return template(
model=model,
theme=theme,
translation=translation,
ascii_only=model.meta.config.ascii_only,
ascii_only=ascii_only,
)

@classmethod
Expand Down

0 comments on commit 5a11a4c

Please sign in to comment.