handle unset default editions
This commit is contained in:
parent
72c7829bab
commit
dfd730757d
|
@ -49,6 +49,7 @@ class Edition(Book):
|
||||||
class Work(Book):
|
class Work(Book):
|
||||||
''' work instance of a book object '''
|
''' work instance of a book object '''
|
||||||
lccn: str = ''
|
lccn: str = ''
|
||||||
|
defaultEdition: str = ''
|
||||||
editions: List[str]
|
editions: List[str]
|
||||||
type: str = 'Work'
|
type: str = 'Work'
|
||||||
|
|
||||||
|
|
|
@ -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),
|
||||||
|
),
|
||||||
|
]
|
|
@ -86,6 +86,7 @@ class Book(ActivitypubMixin, BookWyrmModel):
|
||||||
|
|
||||||
ActivityMapping('lccn', 'lccn'),
|
ActivityMapping('lccn', 'lccn'),
|
||||||
ActivityMapping('editions', 'editions'),
|
ActivityMapping('editions', 'editions'),
|
||||||
|
ActivityMapping('defaultEdition', 'default_edition'),
|
||||||
ActivityMapping('cover', 'cover'),
|
ActivityMapping('cover', 'cover'),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -125,6 +126,10 @@ class Work(OrderedCollectionPageMixin, Book):
|
||||||
null=True
|
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
|
activity_serializer = activitypub.Work
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -531,7 +531,7 @@ def book_page(request, book_id):
|
||||||
return JsonResponse(book.to_activity(), encoder=ActivityEncoder)
|
return JsonResponse(book.to_activity(), encoder=ActivityEncoder)
|
||||||
|
|
||||||
if isinstance(book, models.Work):
|
if isinstance(book, models.Work):
|
||||||
book = book.default_edition
|
book = book.get_default_edition()
|
||||||
if not book:
|
if not book:
|
||||||
return HttpResponseNotFound()
|
return HttpResponseNotFound()
|
||||||
|
|
||||||
|
@ -646,7 +646,7 @@ def author_page(request, author_id):
|
||||||
data = {
|
data = {
|
||||||
'title': author.name,
|
'title': author.name,
|
||||||
'author': author,
|
'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)
|
return TemplateResponse(request, 'author.html', data)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue