Skip to content

Commit

Permalink
Add author date when showing author name to librarians (#9052)
Browse files Browse the repository at this point in the history
* Re-implement changes

- Store data in dictionaries
- Only show years for librarians on book page

* Delete integration test

* Use a regex to determine the year to display

* Use single quotes

* Add helper function to extract a year

* Register helper

* Final fixes

* Small clean-up

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Address comments

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
Aeltumn and pre-commit-ci[bot] authored Apr 19, 2024
1 parent 92b64f0 commit d9fb943
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 123 deletions.
9 changes: 9 additions & 0 deletions openlibrary/core/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"bookreader_host",
"private_collections",
"private_collection_in",
"extract_year",
# functions imported from elsewhere
"parse_datetime",
"safeint",
Expand Down Expand Up @@ -324,6 +325,14 @@ def private_collection_in(collections):
return any(x in private_collections() for x in collections)


def extract_year(input):
"""Extracts the year from an author's birth or death date."""
if result := re.search(r'\d{4}', input):
return result.group()
else:
return ''


def _get_helpers():
_globals = globals()
return web.storage((k, _globals[k]) for k in __all__)
Expand Down
31 changes: 19 additions & 12 deletions openlibrary/macros/BookByline.html
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
$def with(author_names_url_birth_date_death_date, limit=None, overflow_url=None, attrs='')
$def with(author_data, limit=None, overflow_url=None, attrs='', show_years=False)

$def render_author_link(name, url, birth_date, death_date):
$def render_author_link(author):
$ name = author.get('name')
$ url = author.get('url')
$ rendered_name = cond(name, truncate(name, 40), _("Unknown author"))
$ birth_date_formatted = ''
$ death_date_formatted = ''
$if birth_date and death_date:
$ birth_date_formatted = birth_date[-4:]
$ death_date_formatted = death_date[-4:]
$if url:
$if show_years and author.get('birth_date'):
$ birth_date_formatted = extract_year(author.get('birth_date'))
$if author.get('death_date'):
$ death_date_formatted = extract_year(author.get('death_date'))
$else:
$ death_date_formatted = ''
$if show_years and url and author.get('birth_date'):
<a href="$url" $:attrs>$rendered_name ($birth_date_formatted - $death_date_formatted)</a>
$else:
$elif show_years and author.get('birth_date'):
<span $:attrs>$rendered_name ($birth_date_formatted - $death_date_formatted)</span>
$elif url:
<a href="$url" $:attrs>$rendered_name</a>
$else:
<span $:attrs>$rendered_name</span>

$def render_overflow_link(remaining_authors, overflow_url):
<small><em><a href="$overflow_url" $:attrs>$ungettext('%(n)s other', '%(n)s others', remaining_authors, n=remaining_authors)</a></em></small>

$code:
if limit is None:
limit = len(author_names_url_birth_date_death_date)
limit = len(author_data)

remaining_authors = max(0, len(author_names_url_birth_date_death_date) - limit)
render_items = [render_author_link(name, url,birth_date, death_date) for name, url, birth_date, death_date in author_names_url_birth_date_death_date][:limit]
remaining_authors = max(0, len(author_data) - limit)
render_items = [render_author_link(author) for author in author_data][:limit]

if remaining_authors > 0:
render_items.append(render_overflow_link(remaining_authors, overflow_url))
Expand Down
14 changes: 6 additions & 8 deletions openlibrary/macros/SearchResultsWork.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,14 @@ <h3 itemprop="name" class="booktitle">
<em>$_('Unknown author')</em>
$else:
$code:
author_names_and_urls = [
(
a.get('name') or a.get('author', {}).get('name'),
a.get('url') or a.get('key') or a.get('author', {}).get('url') or a.get('author', {}).get('key'),
a.get('birth_date'),
a.get('death_date')
)
author_data = [
{
'name': a.get('name') or a.get('author', {}).get('name'),
'url': (a.get('url') or a.get('key') or a.get('author', {}).get('url') or a.get('author', {}).get('key'))
}
for a in authors
]
$:macros.BookByline(author_names_and_urls, limit=max_rendered_authors, overflow_url=work_edition_url, attrs='class="results"')
$:macros.BookByline(author_data, limit=max_rendered_authors, overflow_url=work_edition_url, attrs='class="results"')
</span>
<span class="resultPublisher">
$if doc.get('first_publish_year'):
Expand Down
4 changes: 2 additions & 2 deletions openlibrary/templates/account/loans.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
</a>
</span>
<span class="author">
$:macros.BookByline([(a.name, a.url(), a.birth_date, a.death_date) for a in book.get_authors()])
$:macros.BookByline([{'name': a.name, 'url': a.url()} for a in book.get_authors()])
</span>
<div class="date">
Borrowed $datestr(datetime_from_utc_timestamp(loan['loaned_at']))
Expand Down Expand Up @@ -161,7 +161,7 @@ <h1 class="details-title">$_("Books You're Waiting For")</h1>
<a href="$book.key" class="borrowResults"><strong>$book.title</strong></a>
</span>
<span class="author">
$:macros.BookByline([(a.name, a.url(), a.birth_date, a.death_date) for a in book.get_authors()])
$:macros.BookByline([{'name': a.name, 'url': a.url()} for a in book.get_authors()])
</span>
<div class="date">
$ ndays = record.get_waiting_in_days()
Expand Down
2 changes: 1 addition & 1 deletion openlibrary/templates/admin/loans_table.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
</span>

<span class="author">
$# :macros.BookByline([(a.name, a.url(), a.birth_date, a.death_date) for a in book.get_authors()])
$# :macros.BookByline([{'name': a.name, 'url': a.url()} for a in book.get_authors()])
</span>
<div class="date">
$if waiting_loan:
Expand Down
3 changes: 2 additions & 1 deletion openlibrary/templates/books/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ <h2 class="editFormTitle">$:title</h2>
<h1 class="editFormBookTitle">$this_title</h1>
$if authors:
<h3 class="editFormBookAuthors">
$:macros.BookByline([(author.name, author.url(), author.birth_date, author.death_date) for author in authors])
$ is_librarian = ctx.user and ctx.user.is_librarian()
$:macros.BookByline([{'name': a.name, 'url': a.url(), 'birth_date': a.birth_date, 'death_date': a.death_date} for a in authors], show_years=is_librarian)
</h3>
<div id="tabsAddbook">
<ul>
Expand Down
5 changes: 3 additions & 2 deletions openlibrary/templates/type/edition/title_and_author.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ <h2 class="work-subtitle">
$ author_names = (work or edition).author_names
$if authors:
<h2 class="edition-byline">
$:macros.BookByline([(author.name, author.url(), author.birth_date, author.death_date) for author in authors], attrs='itemprop="author"')
$ is_librarian = ctx.user and ctx.user.is_librarian()
$:macros.BookByline([{'name': author.name, 'url': author.url(), 'birth_date': author.birth_date, 'death_date': author.death_date} for author in authors], attrs='itemprop="author"', show_years=is_librarian)
</h2>
$elif author_names:
<h2 class="edition-byline">
$:macros.BookByline([(name, None, author.birth_date, author.death_date) for name in author_names], attrs='itemprop="author"')
$:macros.BookByline([{'name': name} for name in author_names], attrs='itemprop="author"')
</h2>
$:star_ratings_stats
</span>
Expand Down
97 changes: 0 additions & 97 deletions tests/integration/test_author_name.py

This file was deleted.

0 comments on commit d9fb943

Please sign in to comment.