mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2025-01-20 14:08:09 +00:00
Simplify default edition logic
This commit is contained in:
parent
3cc948a55b
commit
ca26a712c3
2 changed files with 8 additions and 15 deletions
|
@ -44,7 +44,7 @@ def get_book(book):
|
||||||
'published_date': book.published_date.isoformat() if \
|
'published_date': book.published_date.isoformat() if \
|
||||||
book.published_date else None,
|
book.published_date else None,
|
||||||
'parent_work': book.parent_work.absolute_id if \
|
'parent_work': book.parent_work.absolute_id if \
|
||||||
book.parent_work else None,
|
hasattr(book, 'parent_work') else None,
|
||||||
}
|
}
|
||||||
for field in fields:
|
for field in fields:
|
||||||
if hasattr(book, field):
|
if hasattr(book, field):
|
||||||
|
|
|
@ -190,26 +190,19 @@ class Connector(AbstractConnector):
|
||||||
def set_default_edition(work):
|
def set_default_edition(work):
|
||||||
''' pick one edition to be what gets shown by default '''
|
''' pick one edition to be what gets shown by default '''
|
||||||
# check for an existing default work, in which case we're done
|
# check for an existing default work, in which case we're done
|
||||||
# favor recent, hardcover, english editions
|
if models.Edition.objects.filter(
|
||||||
|
parent_work=work,
|
||||||
|
default=True
|
||||||
|
).count():
|
||||||
|
return
|
||||||
editions = models.Edition.objects.filter(
|
editions = models.Edition.objects.filter(
|
||||||
parent_work=work,
|
parent_work=work,
|
||||||
).all()
|
).all()
|
||||||
options = [e for e in editions if 'English' in e.languages] or editions
|
options = [e for e in editions if 'English' in e.languages] or editions
|
||||||
format_prefs = {
|
options = [e for e in options if e.cover] or options
|
||||||
'hardcover': 0,
|
|
||||||
'paperback': 1,
|
|
||||||
'mass market paperback': 2,
|
|
||||||
}
|
|
||||||
options = sorted(
|
options = sorted(
|
||||||
options,
|
options,
|
||||||
key=lambda e: format_prefs.get(str(e.physical_format).lower(), 3)
|
key=lambda e: e.published_date.year if e.published_date else None
|
||||||
)
|
|
||||||
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()
|
||||||
|
|
Loading…
Reference in a new issue