From 488c0892ddd5e75c6ba0ecf84b734d7dd5f1df86 Mon Sep 17 00:00:00 2001 From: Thomas Morrell Date: Thu, 22 Jun 2023 11:09:20 -0700 Subject: [PATCH 1/2] journal: ui serializer formatting improvements --- .../resources/serializers/ui/schema.py | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/invenio_rdm_records/resources/serializers/ui/schema.py b/invenio_rdm_records/resources/serializers/ui/schema.py index 74d72f338..aa05b4c99 100644 --- a/invenio_rdm_records/resources/serializers/ui/schema.py +++ b/invenio_rdm_records/resources/serializers/ui/schema.py @@ -204,27 +204,30 @@ class MeetingSchema(Schema): def compute_publishing_information(obj, dummyctx): """Computes 'publishing information' string from custom fields.""" - def _format_journal(journal, publication_date): + def _format_journal(journal): """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"}) + >>> 'The Effects of Climate Change, 10(2), 15-22, ISSN:1234-5678.' """ 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)) + 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] + formatted = ", ".join(filter(None, fields)) + formatted += "." return formatted if formatted else "" @@ -239,7 +242,6 @@ def _format_imprint(imprint, publisher): 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 @@ -249,7 +251,7 @@ def _format_imprint(imprint, publisher): result = {} if journal: - journal_string = _format_journal(journal, publication_date) + journal_string = _format_journal(journal) if journal_string: result.update({"journal": journal_string}) From bc9ed029e125d0aa4eac544c11c6fc7448d1bb6e Mon Sep 17 00:00:00 2001 From: Karolina Przerwa Date: Mon, 9 Oct 2023 14:29:58 +0200 Subject: [PATCH 2/2] serializers: ui - add publication date to journal citation --- .../resources/serializers/ui/schema.py | 44 ++++++++++++++----- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/invenio_rdm_records/resources/serializers/ui/schema.py b/invenio_rdm_records/resources/serializers/ui/schema.py index aa05b4c99..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 ( @@ -204,20 +205,28 @@ class MeetingSchema(Schema): def compute_publishing_information(obj, dummyctx): """Computes 'publishing information' string from custom fields.""" - def _format_journal(journal): + 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"}) - >>> 'The Effects of Climate Change, 10(2), 15-22, ISSN:1234-5678.' + _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") journal_issn = journal.get("issn") journal_issue = journal.get("issue") journal_volume = journal.get("volume") journal_pages = journal.get("pages") + 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})" @@ -225,7 +234,8 @@ def _format_journal(journal): 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] + fields = [title, vol_issue, pages, issn, publication_date_formatted] + formatted = ", ".join(filter(None, fields)) formatted += "." @@ -233,11 +243,24 @@ def _format_journal(journal): 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" @@ -249,9 +272,10 @@ 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) + journal_string = _format_journal(journal, publication_date) if journal_string: result.update({"journal": journal_string})