diff --git a/rendercv/data_models.py b/rendercv/data_models.py index f982f985..19232b8e 100644 --- a/rendercv/data_models.py +++ b/rendercv/data_models.py @@ -569,7 +569,9 @@ default_entry_types_for_a_given_title: dict[ class SocialNetwork(RenderCVBaseModel): """This class is the data model of a social network.""" - network: Literal["LinkedIn", "GitHub", "Instagram", "Orcid"] = pydantic.Field( + network: Literal[ + "LinkedIn", "GitHub", "Instagram", "Orcid", "Mastodon", "Twitter" + ] = pydantic.Field( title="Social Network", description="The social network name.", ) @@ -578,6 +580,23 @@ class SocialNetwork(RenderCVBaseModel): description="The username of the social network. The link will be generated.", ) + @pydantic.model_validator(mode="after") + @classmethod + def check_networks(cls, model): + if model.network == "Mastodon": + if not model.username.startswith("@"): + raise ValueError( + "Mastodon username should start with '@'. The username is" + f" {model.username}." + ) + if model.username.count("@") > 2: + raise ValueError( + "Mastodon username should contain only two '@'. The username is" + f" {model.username}." + ) + + return model + @pydantic.computed_field @cached_property def url(self) -> pydantic.HttpUrl: @@ -587,6 +606,8 @@ class SocialNetwork(RenderCVBaseModel): "GitHub": "https://github.com/", "Instagram": "https://instagram.com/", "Orcid": "https://orcid.org/", + "Mastodon": "https://mastodon.social/", + "Twitter": "https://twitter.com/", } url = url_dictionary[self.network] + self.username diff --git a/tests/test_data_models.py b/tests/test_data_models.py index 183d8037..c7ef8571 100644 --- a/tests/test_data_models.py +++ b/tests/test_data_models.py @@ -141,6 +141,8 @@ def test_invalid_doi(publication_entry, doi): ("GitHub", "myusername", "https://github.com/myusername"), ("Instagram", "myusername", "https://instagram.com/myusername"), ("Orcid", "myusername", "https://orcid.org/myusername"), + ("Twitter", "myusername", "https://twitter.com/myusername"), + ("Mastodon", "@myusername", "https://mastodon.social/@myusername"), ], ) def test_social_network_url(network, username, expected_url):