Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Journal formatting #1524

Merged
merged 2 commits into from
Oct 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 44 additions & 18 deletions invenio_rdm_records/resources/serializers/ui/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -208,45 +209,70 @@ 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
journal = field.get("journal:journal")
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)
Expand Down
Loading