diff --git a/invenio_rdm_records/resources/serializers/ui/schema.py b/invenio_rdm_records/resources/serializers/ui/schema.py index 74d72f338..971104ede 100644 --- a/invenio_rdm_records/resources/serializers/ui/schema.py +++ b/invenio_rdm_records/resources/serializers/ui/schema.py @@ -13,6 +13,7 @@ from copy import deepcopy from functools import partial +from edtf import parse_edtf from flask import current_app, g from flask_resources import BaseObjectSchema from invenio_communities.communities.resources.ui_schema import ( @@ -208,38 +209,62 @@ def _format_journal(journal, publication_date): """Formats a journal object into a string based on its attributes. Example: - _format_journal({"title": "The Effects of Climate Change", "volume": 10, "issue": 2, "pages": "15-22", "issn": "1234-5678"}, "2022") - >>> 'The Effects of Climate Change: 10 (2022) no. 1234-5678 pp. 15-22 (2)' + _format_journal({"title": "The Effects of Climate Change", "volume": 10, "issue": 2, "pages": "15-22", "issn": "1234-5678", "publication_date": "2023-03-02}) + >>> 'The Effects of Climate Change, 10(2), 15-22, ISSN:1234-5678, 2023.' """ journal_title = journal.get("title") - if not journal_title: - return "" - journal_issn = journal.get("issn") journal_issue = journal.get("issue") + journal_volume = journal.get("volume") journal_pages = journal.get("pages") - publication_date = f"({publication_date})" if publication_date else None - title = f"{journal_title}:" - issn = f"no. {journal_issn}" if journal_issn else None - issue = f"({journal_issue})" if journal_issue else None - pages = f"pp. {journal_pages}" if journal_pages else None - fields = [title, journal.get("volume"), publication_date, issn, pages, issue] - formatted = " ".join(filter(None, fields)) + publication_date_edtf = ( + parse_edtf(publication_date).lower_strict() if publication_date else None + ) + publication_date_formatted = ( + f"{publication_date_edtf.tm_year}" if publication_date_edtf else None + ) + + title = f"{journal_title}" if journal_title else None + vol_issue = f"{journal_volume}" if journal_volume else None + + if journal_issue: + if vol_issue: + vol_issue += f"({journal_issue})" + else: + vol_issue = f"{journal_issue}" + pages = f"{journal_pages}" if journal_pages else None + issn = f"ISSN: {journal_issn}" if journal_issn else None + fields = [title, vol_issue, pages, issn, publication_date_formatted] + + formatted = ", ".join(filter(None, fields)) + formatted += "." return formatted if formatted else "" def _format_imprint(imprint, publisher): """Formats a imprint object into a string based on its attributes.""" - place = imprint.get("place", "") - isbn = imprint.get("isbn", "") - formatted = "{publisher}{place} {isbn}".format( - publisher=publisher, place=f", {place}", isbn=f"({isbn})" - ) + imprint_title = imprint.get("title") + imprint_place = imprint.get("place") + imprint_isbn = imprint.get("isbn") + imprint_pages = imprint.get("pages") + title_page = f"{imprint_title}" if imprint_title else "" + if imprint_pages: + if title_page: + title_page += f", {imprint_pages}." + else: + title_page = f"{imprint_pages}." + elif title_page: + title_page += "." + else: + title_page = None + place = f"{imprint_place}." if imprint_place else None + isbn = f"ISBN: {imprint_isbn}." if imprint_isbn else None + formatted = " ".join(filter(None, [title_page, place, isbn])) + return formatted attr = "custom_fields" field = obj.get(attr, {}) - publication_date = obj.get("metadata", {}).get("publication_date") publisher = obj.get("metadata", {}).get("publisher") # Retrieve publishing related custom fields @@ -247,6 +272,7 @@ def _format_imprint(imprint, publisher): imprint = field.get("imprint:imprint") thesis = field.get("thesis:university") + publication_date = obj.get("metadata", {}).get("publication_date", None) result = {} if journal: journal_string = _format_journal(journal, publication_date)