refactor data model

This commit is contained in:
Sina Atalay 2023-09-03 19:39:37 +02:00
parent 590ff69d84
commit 4f1a9b8808
1 changed files with 33 additions and 19 deletions

View File

@ -1,4 +1,4 @@
from pydantic import BaseModel, HttpUrl from pydantic import BaseModel, HttpUrl, model_validator
from pydantic_extra_types.phone_numbers import PhoneNumber from pydantic_extra_types.phone_numbers import PhoneNumber
from typing import Literal from typing import Literal
from datetime import date as Date from datetime import date as Date
@ -19,18 +19,6 @@ class Skill(BaseModel):
details: str = None details: str = None
class Activity(BaseModel):
# 1) Mandotory user inputs:
organization: str
position: str
location: Location
# 2) Optional user inputs:
start_date: Date = None
end_date: Date = None
company_url: HttpUrl = None
highlights: list[str] = None
class TestScore(BaseModel): class TestScore(BaseModel):
# 1) Mandotory user inputs: # 1) Mandotory user inputs:
name: str name: str
@ -45,13 +33,13 @@ class Project(BaseModel):
name: str name: str
location: Location location: Location
# 2) Optional user inputs: # 2) Optional user inputs:
start_date: Date= None start_date: Date = None
end_date: Date = None end_date: Date = None
url: HttpUrl = None url: HttpUrl = None
highlights: list[str] = None highlights: list[str] = None
class WorkExperience(BaseModel): class Experience(BaseModel):
# 1) Mandotory user inputs: # 1) Mandotory user inputs:
company: str company: str
position: str position: str
@ -78,10 +66,13 @@ class Education(BaseModel):
class SocialNetwork(BaseModel): class SocialNetwork(BaseModel):
# 1) Mandotory user inputs: # 1) Mandotory user inputs:
network: Literal["LinkedIn", "GitHub", "Twitter", "Facebook", "Instagram"] network: Literal["LinkedIn", "GitHub", "Instagram"]
username: str username: str
url: HttpUrl
class Connection(BaseModel):
# 3) Derived fields (not user inputs):
name: Literal["LinkedIn", "GitHub", "Instagram", "phone", "email", "website"]
value: str
class CurriculumVitae(BaseModel): class CurriculumVitae(BaseModel):
# 1) Mandotory user inputs: # 1) Mandotory user inputs:
@ -93,8 +84,31 @@ class CurriculumVitae(BaseModel):
location: Location = None location: Location = None
social_networks: list[SocialNetwork] = None social_networks: list[SocialNetwork] = None
education: list[Education] = None education: list[Education] = None
work_experience: list[WorkExperience] = None work_experience: list[Experience] = None
academic_projects: list[Project] = None academic_projects: list[Project] = None
extracurricular_activities: list[Activity] = None extracurricular_activities: list[Experience] = None
test_scores: list[TestScore] = None test_scores: list[TestScore] = None
skills: list[Skill] = None skills: list[Skill] = None
# 3) Derived fields (not user inputs):
connections: list[SocialNetwork] = []
@model_validator(mode="after")
@classmethod
def derive_connections(cls, model):
connections = []
if model.email is not None:
connections.append(Connection(name="email", value=model.email))
if model.phone is not None:
connections.append(Connection(name="phone", value=model.phone))
if model.website is not None:
connections.append(Connection(name="website", value=model.website))
if model.social_networks is not None:
for social_network in model.social_networks:
connections.append(Connection(
name=social_network.network,
value=social_network.username
))
model.connections = connections
return model