Skip to content

Commit

Permalink
bibtex: added doi and url fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandromumo authored and kpsherva committed Oct 31, 2023
1 parent cb109d0 commit d947d95
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
15 changes: 11 additions & 4 deletions invenio_rdm_records/resources/serializers/bibtex/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class BibTexSchema(BaseSerializerSchema, CommonFieldsMixin):
pages = fields.Method("get_pages")
note = fields.Method("get_note")
venue = fields.Method("get_venue")
url = fields.Method("get_url")

entry_mapper = {
# Publication fields
Expand Down Expand Up @@ -148,6 +149,14 @@ def get_school(self, obj):
"""
return obj.get("custom_fields", {}).get("thesis:university")

def get_url(self, obj):
"""Generate url."""
doi = obj.get("pids", {}).get("doi", {}).get("identifier")
url = None
if doi:
url = f"https://doi.org/{doi}"
return url

@post_dump(pass_original=True)
def dump_record(self, data, original, many, **kwargs):
"""Dumps record."""
Expand Down Expand Up @@ -227,9 +236,7 @@ def _fetch_fields_map(self, data):
"doi": data.get("doi", None),
"month": data.get("date_created", {}).get("month", None),
"version": data.get("version", None),
"url": (lambda doi: None if doi is None else "https://doi.org/" + doi)(
data.get("doi", None)
),
"url": data.get("url", None),
"school": data.get("school", None),
"journal": data.get("journal", None),
"volume": data.get("volume", None),
Expand Down Expand Up @@ -261,7 +268,7 @@ def _format_output_row(self, field, value):
elif field == "month":
out = " {0:<12} = {1},\n".format(field, value)
elif field == "url":
out == " {0:<12} = {{{1}}}\n".format(field, value)
out = " {0:<12} = {{{1}}}\n".format(field, value)
else:
if not isinstance(value, list) and value.isdigit():
out = " {0:<12} = {1},\n".format(field, value)
Expand Down
31 changes: 23 additions & 8 deletions invenio_rdm_records/resources/serializers/bibtex/schema_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ class BibTexFormatter:
book = {
"name": "book",
"req_fields": ["author", "title", "publisher", "year"],
"opt_fields": ["volume", "address", "month", "note"],
"opt_fields": ["volume", "address", "month", "note", "doi", "url"],
}
"""A single-volume book."""

booklet = {
"name": "booklet",
"req_fields": ["title"],
"opt_fields": ["author", "address", "month", "year", "note"],
"opt_fields": ["author", "address", "month", "year", "note", "doi", "url"],
}
"""A book-like work without a formal publisher or sponsoring institution."""

Expand All @@ -43,49 +43,60 @@ class BibTexFormatter:
"note",
"publisher",
"version",
"doi",
"url",
],
}
"""A fallback type for entries which do not fit into any other category."""

in_proceedings = {
"name": "inproceedings",
"req_fields": ["author", "title", "booktitle", "year"],
"opt_fields": ["pages", "publisher", "address", "month", "note", "venue"],
"opt_fields": [
"pages",
"publisher",
"address",
"month",
"note",
"venue",
"doi",
"url",
],
}
"""An article in a conference proceedings."""

proceedings = {
"name": "proceedings",
"req_fields": ["title", "year"],
"opt_fields": ["publisher", "address", "month", "note"],
"opt_fields": ["publisher", "address", "month", "note", "doi", "url"],
}
"""A single-volume conference proceedings."""

article = {
"name": "article",
"req_fields": ["author", "title", "journal", "year"],
"opt_fields": ["volume", "number", "pages", "month", "note"],
"opt_fields": ["volume", "number", "pages", "month", "note", "doi", "url"],
}
"""An article in a journal, magazine, newspaper, or other periodical which forms a self-contained unit with its own title."""

unpublished = {
"name": "unpublished",
"req_fields": ["author", "title", "note"],
"opt_fields": ["month", "year"],
"opt_fields": ["month", "year", "doi", "url"],
}
"""A work with an author and a title which has not been formally published."""

thesis = {
"name": "phdthesis",
"req_fields": ["author", "title", "school", "year"],
"opt_fields": ["address", "month", "note"],
"opt_fields": ["address", "month", "note", "doi", "url"],
}
"""A thesis written for an educational institution."""

manual = {
"name": "manual",
"req_fields": ["title"],
"opt_fields": ["author", "address", "month", "year", "note"],
"opt_fields": ["author", "address", "month", "year", "note", "doi", "url"],
}
"""Technical or other documentation, not necessarily in printed form."""

Expand All @@ -100,6 +111,8 @@ class BibTexFormatter:
"note",
"publisher",
"version",
"doi",
"url",
],
}
"""A data set or a similar collection of (mostly) raw data."""
Expand All @@ -115,6 +128,8 @@ class BibTexFormatter:
"note",
"publisher",
"version",
"doi",
"url",
],
}
"""Computer software."""
2 changes: 2 additions & 0 deletions tests/resources/serializers/test_bibtex_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ def test_bibtex_serializer_full_record(running_app, updated_full_record):
" year = 2023,",
" publisher = {InvenioRDM},",
" version = {v1.0},",
" doi = {10.1234/inveniordm.1234},",
" url = {https://doi.org/10.1234/inveniordm.1234}",
"}",
]
)
Expand Down

0 comments on commit d947d95

Please sign in to comment.