From 53a358f2fd29bfbd810c27d8bf31810413307896 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 4 Nov 2020 12:55:00 -0800 Subject: [PATCH] Activitypub serializable edition list --- bookwyrm/models/book.py | 23 +++++++++++++++++++---- bookwyrm/templates/book.html | 2 +- bookwyrm/templates/search_results.html | 2 +- bookwyrm/urls.py | 2 +- bookwyrm/views.py | 14 ++++++++++++-- 5 files changed, 34 insertions(+), 9 deletions(-) diff --git a/bookwyrm/models/book.py b/bookwyrm/models/book.py index 0067d33eb..04a8b2863 100644 --- a/bookwyrm/models/book.py +++ b/bookwyrm/models/book.py @@ -9,8 +9,8 @@ from bookwyrm import activitypub from bookwyrm.settings import DOMAIN from bookwyrm.utils.fields import ArrayField -from .base_model import ActivityMapping, ActivitypubMixin, BookWyrmModel - +from .base_model import ActivityMapping, BookWyrmModel +from .base_model import ActivitypubMixin, OrderedCollectionPageMixin class Book(ActivitypubMixin, BookWyrmModel): ''' a generic book, which can mean either an edition or a work ''' @@ -135,17 +135,32 @@ class Book(ActivitypubMixin, BookWyrmModel): ) -class Work(Book): +class Work(OrderedCollectionPageMixin, Book): ''' a work (an abstract concept of a book that manifests in an edition) ''' # library of congress catalog control number lccn = models.CharField(max_length=255, blank=True, null=True) - default_edition = models.ForeignKey('Edition', on_delete=models.PROTECT, null=True) + # this has to be nullable but should never be null + default_edition = models.ForeignKey( + 'Edition', + on_delete=models.PROTECT, + null=True + ) @property def editions_path(self): ''' it'd be nice to serialize the edition instead but, recursion ''' return [e.remote_id for e in self.edition_set.all()] + def to_edition_list(self, **kwargs): + ''' activitypub serialization for this work's editions ''' + remote_id = self.local_id + '/editions' + return self.to_ordered_collection( + self.edition_set, + remote_id=remote_id, + **kwargs + ) + + activity_serializer = activitypub.Work diff --git a/bookwyrm/templates/book.html b/bookwyrm/templates/book.html index f05fd7edd..637df52b4 100644 --- a/bookwyrm/templates/book.html +++ b/bookwyrm/templates/book.html @@ -51,7 +51,7 @@ {% include 'snippets/book_description.html' %} {% if book.parent_work.edition_set.count > 1 %} -

{{ book.parent_work.edition_set.count }} editions

+

{{ book.parent_work.edition_set.count }} editions

{% endif %} diff --git a/bookwyrm/templates/search_results.html b/bookwyrm/templates/search_results.html index 14e5fbbd1..cb997298a 100644 --- a/bookwyrm/templates/search_results.html +++ b/bookwyrm/templates/search_results.html @@ -18,7 +18,7 @@ {% endif %} - {% if book_results|slice:":1" and local_results.results %} + {% if book_results|slice:":1" and local_results.results and request.user.is_authenticated %}

Didn't find what you were looking for? diff --git a/bookwyrm/urls.py b/bookwyrm/urls.py index 78c99ae4a..48c7d405b 100644 --- a/bookwyrm/urls.py +++ b/bookwyrm/urls.py @@ -75,7 +75,7 @@ urlpatterns = [ # books re_path(r'%s(.json)?/?$' % book_path, views.book_page), re_path(r'%s/edit/?$' % book_path, views.edit_book_page), - re_path(r'^editions/(?P\d+)/?$', views.editions_page), + re_path(r'%s/editions(.json)?/?' % book_path, views.editions_page), re_path(r'^author/(?P[\w\-]+)(.json)?/?$', views.author_page), # TODO: tag needs a .json path diff --git a/bookwyrm/views.py b/bookwyrm/views.py index fc066f59e..6e98225c7 100644 --- a/bookwyrm/views.py +++ b/bookwyrm/views.py @@ -559,9 +559,19 @@ def edit_book_page(request, book_id): return TemplateResponse(request, 'edit_book.html', data) -def editions_page(request, work_id): +def editions_page(request, book_id): ''' list of editions of a book ''' - work = models.Work.objects.get(id=work_id) + try: + work = models.Work.objects.get(id=book_id) + except models.Work.DoesNotExist: + return HttpResponseNotFound() + + if is_api_request(request): + return JsonResponse( + work.to_edition_list(**request.GET), + encoder=ActivityEncoder + ) + editions = models.Edition.objects.filter(parent_work=work).all() data = { 'title': 'Editions of %s' % work.title,