add mastodon support

This commit is contained in:
Sina Atalay
2024-01-28 21:37:00 +01:00
parent 25fabcd9e5
commit 63bebebe4e
2 changed files with 24 additions and 1 deletions

View File

@@ -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

View File

@@ -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):