sort editions correctly for picking default

This commit is contained in:
Mouse Reeve 2020-03-30 15:27:54 -07:00
parent defd026ed0
commit 33327e2bb6
2 changed files with 13 additions and 3 deletions

View file

@ -129,7 +129,7 @@ class Connector(AbstractConnector):
response = requests.get( response = requests.get(
'%s/works/%s/editions.json' % (self.url, work.openlibrary_key)) '%s/works/%s/editions.json' % (self.url, work.openlibrary_key))
edition_data = response.json() edition_data = response.json()
for data in edition_data.get('entries', [])[:5]: for data in edition_data.get('entries', []):
try: try:
olkey = data['key'].split('/')[-1] olkey = data['key'].split('/')[-1]
except KeyError: except KeyError:
@ -200,7 +200,17 @@ def set_default_edition(work):
'paperback': 1, 'paperback': 1,
'mass market paperback': 2, 'mass market paperback': 2,
} }
options = sorted(options, key=lambda e: format_prefs.get(e, 3)) options = sorted(
options,
key=lambda e: format_prefs.get(str(e.physical_format).lower(), 3)
)
if options[0].physical_format in format_prefs:
options = [e for e in options if \
e.physical_format == options[0].physical_format]
options = sorted(
options,
key=lambda e: -1 * e.published_date.year
)
options[0].default = True options[0].default = True
options[0].save() options[0].save()

View file

@ -147,7 +147,7 @@ def shelve(request):
if request.POST.get('reshelve', True): if request.POST.get('reshelve', True):
try: try:
current_shelf = models.Shelf.objects.get( current_shelf = models.Shelf.objects.get(
shelf__user=request.user, user=request.user,
edition=book edition=book
) )
outgoing.handle_unshelve(request.user, book, current_shelf) outgoing.handle_unshelve(request.user, book, current_shelf)