handle unset default editions

This commit is contained in:
Mouse Reeve 2020-11-28 17:41:57 -08:00
parent 72c7829bab
commit dfd730757d
4 changed files with 32 additions and 2 deletions

View file

@ -49,6 +49,7 @@ class Edition(Book):
class Work(Book):
''' work instance of a book object '''
lccn: str = ''
defaultEdition: str = ''
editions: List[str]
type: str = 'Work'

View file

@ -0,0 +1,24 @@
# Generated by Django 3.0.7 on 2020-11-28 21:42
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('bookwyrm', '0017_auto_20201128_1849'),
]
operations = [
migrations.AlterField(
model_name='edition',
name='parent_work',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, related_name='editions', to='bookwyrm.Work'),
),
migrations.AlterField(
model_name='tag',
name='name',
field=models.CharField(max_length=100, unique=True),
),
]

View file

@ -86,6 +86,7 @@ class Book(ActivitypubMixin, BookWyrmModel):
ActivityMapping('lccn', 'lccn'),
ActivityMapping('editions', 'editions'),
ActivityMapping('defaultEdition', 'default_edition'),
ActivityMapping('cover', 'cover'),
]
@ -125,6 +126,10 @@ class Work(OrderedCollectionPageMixin, Book):
null=True
)
def get_default_edition(self):
''' in case the default edition is not set '''
return self.default_edition or self.editions.first()
activity_serializer = activitypub.Work

View file

@ -531,7 +531,7 @@ def book_page(request, book_id):
return JsonResponse(book.to_activity(), encoder=ActivityEncoder)
if isinstance(book, models.Work):
book = book.default_edition
book = book.get_default_edition()
if not book:
return HttpResponseNotFound()
@ -646,7 +646,7 @@ def author_page(request, author_id):
data = {
'title': author.name,
'author': author,
'books': [b.default_edition for b in books],
'books': [b.get_default_edition() for b in books],
}
return TemplateResponse(request, 'author.html', data)