Skip to content

Commit 8112abe

Browse files
author
Aayushdev18
committed
Simplify edition availability using safeget helper
Simplify the add_availability_with_edition_preference function by using the safeget helper to extract editions directly, following the pattern from work_search.html. This makes the code more concise and handles both dict and list edition structures gracefully.
1 parent a58fd15 commit 8112abe

File tree

1 file changed

+7
-58
lines changed

1 file changed

+7
-58
lines changed

openlibrary/plugins/upstream/mybooks.py

Lines changed: 7 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
from openlibrary.core.follows import PubSub
1919
from openlibrary.core.lending import (
2020
add_availability,
21-
get_availability,
2221
get_loans_of_user,
2322
)
2423
from openlibrary.core.models import LoggedBooksData, User
2524
from openlibrary.core.observations import Observations, convert_observation_ids
2625
from openlibrary.i18n import gettext as _
26+
from openlibrary.plugins.upstream.utils import safeget
2727
from openlibrary.utils import extract_numeric_id_from_olid
2828
from openlibrary.utils.dateutil import current_year
2929

@@ -77,65 +77,14 @@ def render_template(self, mb):
7777
currently_reading = mb.readlog.get_works(key='currently-reading', **params)
7878
already_read = mb.readlog.get_works(key='already-read', **params)
7979

80-
def add_availability_with_edition_preference(docs):
81-
"""Check edition-level availability when available, fallback to work-level."""
82-
filtered_docs = [d for d in docs if d.get('title')]
83-
84-
docs_with_editions = []
85-
editions_to_check = []
86-
docs_without_editions = []
87-
88-
for doc in filtered_docs:
89-
# editions can be a list [edition] or dict {'docs': [edition]}
90-
editions = doc.get('editions')
91-
if editions:
92-
if isinstance(editions, list) and editions:
93-
edition = editions[0]
94-
elif isinstance(editions, dict) and editions.get('docs'):
95-
edition = editions['docs'][0]
96-
else:
97-
edition = None
98-
99-
if edition:
100-
docs_with_editions.append((doc, edition))
101-
editions_to_check.append(edition)
102-
else:
103-
docs_without_editions.append(doc)
104-
else:
105-
docs_without_editions.append(doc)
106-
107-
if editions_to_check:
108-
edition_olids = [
109-
edition['key'].split('/')[-1]
110-
for doc, edition in docs_with_editions
111-
if edition.get('key')
112-
]
113-
if edition_olids:
114-
edition_availabilities = get_availability(
115-
'openlibrary_edition', edition_olids
116-
)
117-
for doc, edition in docs_with_editions:
118-
if edition.get('key'):
119-
edition_olid = edition['key'].split('/')[-1]
120-
if edition_olid in edition_availabilities:
121-
doc['availability'] = edition_availabilities[
122-
edition_olid
123-
]
124-
125-
if docs_without_editions:
126-
add_availability(docs_without_editions)
127-
128-
return filtered_docs
129-
130-
# Ideally, do all 3 lookups in one add_availability call
131-
want_to_read.docs = add_availability_with_edition_preference(
132-
want_to_read.docs
80+
want_to_read.docs = add_availability(
81+
[safeget(lambda: d['editions']['docs'][0], d) for d in want_to_read.docs if d.get('title')]
13382
)[:5]
134-
currently_reading.docs = add_availability_with_edition_preference(
135-
currently_reading.docs
83+
currently_reading.docs = add_availability(
84+
[safeget(lambda: d['editions']['docs'][0], d) for d in currently_reading.docs if d.get('title')]
13685
)[:5]
137-
already_read.docs = add_availability_with_edition_preference(
138-
already_read.docs
86+
already_read.docs = add_availability(
87+
[safeget(lambda: d['editions']['docs'][0], d) for d in already_read.docs if d.get('title')]
13988
)[:5]
14089

14190
docs = {

0 commit comments

Comments
 (0)