data_models: make DOI optional for PublicationEntry (#33)

This commit is contained in:
Sina Atalay 2024-03-09 18:53:14 +01:00
parent 5169612ece
commit 0354b77fab
2 changed files with 34 additions and 23 deletions

View File

@ -483,7 +483,8 @@ class PublicationEntry(RenderCVBaseModel):
title="Authors", title="Authors",
description="The authors of the publication in order as a list of strings.", description="The authors of the publication in order as a list of strings.",
) )
doi: str = pydantic.Field( doi: Optional[str] = pydantic.Field(
default=None,
title="DOI", title="DOI",
description="The DOI of the publication.", description="The DOI of the publication.",
examples=["10.48550/arXiv.2310.03138"], examples=["10.48550/arXiv.2310.03138"],
@ -513,26 +514,32 @@ class PublicationEntry(RenderCVBaseModel):
@pydantic.field_validator("doi") @pydantic.field_validator("doi")
@classmethod @classmethod
def check_doi(cls, doi: str) -> str: def check_doi(cls, doi: Optional[str]) -> Optional[str]:
"""Check if the DOI exists in the DOI System.""" """Check if the DOI exists in the DOI System."""
# see https://stackoverflow.com/a/60671292/18840665 for the explanation of the if doi is not None:
# next line: # see https://stackoverflow.com/a/60671292/18840665 for the explanation of
ssl._create_default_https_context = ssl._create_unverified_context # type: ignore # the next line:
ssl._create_default_https_context = ssl._create_unverified_context # type: ignore
doi_url = f"http://doi.org/{doi}" doi_url = f"http://doi.org/{doi}"
try: try:
urlopen(doi_url) urlopen(doi_url)
except HTTPError as err: except HTTPError as err:
if err.code == 404: if err.code == 404:
raise ValueError("DOI cannot be found in the DOI System!") raise ValueError("DOI cannot be found in the DOI System!")
return doi return doi
@functools.cached_property @functools.cached_property
def doi_url(self) -> str: def doi_url(self) -> str:
"""Return the URL of the DOI.""" """Return the URL of the DOI."""
return f"https://doi.org/{self.doi}" # self.doi == "" is added because None values are replaced with "" in
# renderer.TemplatedFile class (to make templating easier)
if self.doi is None or self.doi == "":
return ""
else:
return f"https://doi.org/{self.doi}"
@functools.cached_property @functools.cached_property
def date_string(self) -> str: def date_string(self) -> str:

View File

@ -4,6 +4,10 @@
"additionalProperties": false, "additionalProperties": false,
"description": "This class is the data model of the theme options for the classic theme.", "description": "This class is the data model of the theme options for the classic theme.",
"properties": { "properties": {
"theme": {
"const": "classic",
"title": "Theme"
},
"font_size": { "font_size": {
"default": "10pt", "default": "10pt",
"description": "The font size of the CV. The default value is 10pt.", "description": "The font size of the CV. The default value is 10pt.",
@ -109,10 +113,6 @@
"description": "Page, section title, entry field, and highlights field margins.", "description": "Page, section title, entry field, and highlights field margins.",
"title": "Margins" "title": "Margins"
}, },
"theme": {
"const": "classic",
"title": "Theme"
},
"show_timespan_in": { "show_timespan_in": {
"default": [], "default": [],
"description": "The time span will be shown in the date and location column in these sections. The input should be a list of section titles as strings (case-sensitive). The default value is an empty list, which means the time span will not be shown in any section.", "description": "The time span will be shown in the date and location column in these sections. The input should be a list of section titles as strings (case-sensitive). The default value is an empty list, which means the time span will not be shown in any section.",
@ -962,12 +962,17 @@
"type": "array" "type": "array"
}, },
"doi": { "doi": {
"default": null,
"description": "The DOI of the publication.", "description": "The DOI of the publication.",
"examples": [ "examples": [
"10.48550/arXiv.2310.03138" "10.48550/arXiv.2310.03138"
], ],
"title": "DOI", "title": "DOI",
"type": "string" "allOf": [
{
"type": "string"
}
]
}, },
"date": { "date": {
"default": "2020-01-01", "default": "2020-01-01",
@ -998,7 +1003,6 @@
"required": [ "required": [
"title", "title",
"authors", "authors",
"doi",
"date" "date"
], ],
"title": "PublicationEntry", "title": "PublicationEntry",
@ -1008,6 +1012,10 @@
"additionalProperties": false, "additionalProperties": false,
"description": "This class is the data model of the theme options for the sb2nov theme.", "description": "This class is the data model of the theme options for the sb2nov theme.",
"properties": { "properties": {
"theme": {
"const": "sb2nov",
"title": "Theme"
},
"font_size": { "font_size": {
"default": "10pt", "default": "10pt",
"description": "The font size of the CV. The default value is 10pt.", "description": "The font size of the CV. The default value is 10pt.",
@ -1112,10 +1120,6 @@
}, },
"description": "Page, section title, entry field, and highlights field margins.", "description": "Page, section title, entry field, and highlights field margins.",
"title": "Margins" "title": "Margins"
},
"theme": {
"const": "sb2nov",
"title": "Theme"
} }
}, },
"required": [ "required": [
@ -1191,6 +1195,7 @@
}, },
"design": { "design": {
"default": { "default": {
"theme": "classic",
"font_size": "10pt", "font_size": "10pt",
"page_size": "letterpaper", "page_size": "letterpaper",
"color": "#004f90", "color": "#004f90",
@ -1226,7 +1231,6 @@
"top": "0.3 cm" "top": "0.3 cm"
} }
}, },
"theme": "classic",
"show_timespan_in": [] "show_timespan_in": []
}, },
"description": "The design information of the CV. The default is the classic theme.", "description": "The design information of the CV. The default is the classic theme.",