From 72eb94315a3636e88dddc825c1705c798cd7bc23 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Fri, 11 Dec 2020 16:39:58 -0800 Subject: [PATCH 1/7] Adds shelf info to book page - includes change shelf button - WIP button for switching to the current edition --- bookwyrm/templates/book.html | 97 ++++--------------- bookwyrm/templates/snippets/readthrough.html | 80 +++++++++++++++ .../snippets/switch_edition_button.html | 3 + bookwyrm/views.py | 21 ++-- 4 files changed, 116 insertions(+), 85 deletions(-) create mode 100644 bookwyrm/templates/snippets/readthrough.html create mode 100644 bookwyrm/templates/snippets/switch_edition_button.html diff --git a/bookwyrm/templates/book.html b/bookwyrm/templates/book.html index 51fbdafc..eee22898 100644 --- a/bookwyrm/templates/book.html +++ b/bookwyrm/templates/book.html @@ -91,87 +91,26 @@ {% endif %} - {% for readthrough in readthroughs %} -
- - -
- -
- - -
- + {# user's relationship to the book #}
- - + {% for shelf in user_shelves %} +

+ This edition is on your {{ shelf.shelf.name }} shelf. + {% include 'snippets/shelf_selector.html' with current=shelf.shelf %} +

+ {% endfor %} + + {% for shelf in other_edition_shelves %} +

+ A different edition of this book is on your {{ shelf.shelf.name }} shelf. + {% include 'snippets/switch_edition_button.html' with desired_edition=book %} +

+ {% endfor %} + + {% for readthrough in readthroughs %} + {% include 'snippets/readthrough.html' with readthrough=readthrough %} + {% endfor %}
- {% endfor %} {% if request.user.is_authenticated %}
diff --git a/bookwyrm/templates/snippets/readthrough.html b/bookwyrm/templates/snippets/readthrough.html new file mode 100644 index 00000000..4d6ca03a --- /dev/null +++ b/bookwyrm/templates/snippets/readthrough.html @@ -0,0 +1,80 @@ +{% load humanize %} +
+ + +
+ +
+ + +
+ +
+ + +
diff --git a/bookwyrm/templates/snippets/switch_edition_button.html b/bookwyrm/templates/snippets/switch_edition_button.html new file mode 100644 index 00000000..3fa09f4f --- /dev/null +++ b/bookwyrm/templates/snippets/switch_edition_button.html @@ -0,0 +1,3 @@ +
+ +
diff --git a/bookwyrm/views.py b/bookwyrm/views.py index e0feaee7..7c27eade 100644 --- a/bookwyrm/views.py +++ b/bookwyrm/views.py @@ -5,8 +5,7 @@ from django.contrib.auth.decorators import login_required, permission_required from django.contrib.postgres.search import TrigramSimilarity from django.core.paginator import Paginator from django.db.models import Avg, Q -from django.http import HttpResponseBadRequest, HttpResponseNotFound,\ - JsonResponse +from django.http import HttpResponseNotFound, JsonResponse from django.core.exceptions import PermissionDenied from django.shortcuts import get_object_or_404, redirect from django.template.response import TemplateResponse @@ -558,8 +557,7 @@ def book_page(request, book_id): prev_page = '/book/%s/?page=%d' % \ (book_id, reviews_page.previous_page_number()) - user_tags = [] - readthroughs = [] + user_tags = readthroughs = user_shelves = other_edition_shelves = [] if request.user.is_authenticated: user_tags = models.Tag.objects.filter( book=book, user=request.user @@ -570,6 +568,16 @@ def book_page(request, book_id): book=book, ).order_by('start_date') + user_shelves = models.ShelfBook.objects.filter( + added_by=request.user, book=book + ) + + other_edition_shelves = models.ShelfBook.objects.filter( + ~Q(book=book), + added_by=request.user, + book__parent_work=book.parent_work, + ) + rating = reviews.aggregate(Avg('rating')) tags = models.Tag.objects.filter( book=book @@ -585,6 +593,8 @@ def book_page(request, book_id): 'rating': rating['rating__avg'], 'tags': tags, 'user_tags': user_tags, + 'user_shelves': user_shelves, + 'other_edition_shelves': other_edition_shelves, 'readthroughs': readthroughs, 'path': '/book/%s' % book_id, 'info_fields': [ @@ -628,10 +638,9 @@ def editions_page(request, book_id): encoder=ActivityEncoder ) - editions = models.Edition.objects.filter(parent_work=work).all() data = { 'title': 'Editions of %s' % work.title, - 'editions': editions, + 'editions': work.edition_set.all(), 'work': work, } return TemplateResponse(request, 'editions.html', data) From 2d7f8ada6139a39a6962840908b5473f778b5e6e Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Fri, 11 Dec 2020 16:57:38 -0800 Subject: [PATCH 2/7] Functional switch editions button --- bookwyrm/models/status.py | 2 +- bookwyrm/templates/book.html | 2 +- .../snippets/switch_edition_button.html | 8 +++--- bookwyrm/urls.py | 17 ++++++------ bookwyrm/view_actions.py | 27 +++++++++++++++++++ 5 files changed, 43 insertions(+), 13 deletions(-) diff --git a/bookwyrm/models/status.py b/bookwyrm/models/status.py index 9d45379c..07e25119 100644 --- a/bookwyrm/models/status.py +++ b/bookwyrm/models/status.py @@ -283,7 +283,7 @@ class Boost(Status): class ReadThrough(BookWyrmModel): ''' Store progress through a book in the database. ''' user = models.ForeignKey('User', on_delete=models.PROTECT) - book = models.ForeignKey('Book', on_delete=models.PROTECT) + book = models.ForeignKey('Edition', on_delete=models.PROTECT) pages_read = models.IntegerField( null=True, blank=True) diff --git a/bookwyrm/templates/book.html b/bookwyrm/templates/book.html index eee22898..c7112016 100644 --- a/bookwyrm/templates/book.html +++ b/bookwyrm/templates/book.html @@ -103,7 +103,7 @@ {% for shelf in other_edition_shelves %}

A different edition of this book is on your {{ shelf.shelf.name }} shelf. - {% include 'snippets/switch_edition_button.html' with desired_edition=book %} + {% include 'snippets/switch_edition_button.html' with edition=book %}

{% endfor %} diff --git a/bookwyrm/templates/snippets/switch_edition_button.html b/bookwyrm/templates/snippets/switch_edition_button.html index 3fa09f4f..9771535a 100644 --- a/bookwyrm/templates/snippets/switch_edition_button.html +++ b/bookwyrm/templates/snippets/switch_edition_button.html @@ -1,3 +1,5 @@ -
- -
+
+ {% csrf_token %} + + +
diff --git a/bookwyrm/urls.py b/bookwyrm/urls.py index a9792038..01d6c56f 100644 --- a/bookwyrm/urls.py +++ b/bookwyrm/urls.py @@ -97,15 +97,16 @@ urlpatterns = [ re_path(r'^edit-profile/?$', actions.edit_profile), - re_path(r'^import-data/?', actions.import_data), - re_path(r'^retry-import/?', actions.retry_import), - re_path(r'^resolve-book/?', actions.resolve_book), - re_path(r'^edit-book/(?P\d+)/?', actions.edit_book), - re_path(r'^upload-cover/(?P\d+)/?', actions.upload_cover), - re_path(r'^add-description/(?P\d+)/?', actions.add_description), + re_path(r'^import-data/?$', actions.import_data), + re_path(r'^retry-import/?$', actions.retry_import), + re_path(r'^resolve-book/?$', actions.resolve_book), + re_path(r'^edit-book/(?P\d+)/?$', actions.edit_book), + re_path(r'^upload-cover/(?P\d+)/?$', actions.upload_cover), + re_path(r'^add-description/(?P\d+)/?$', actions.add_description), - re_path(r'^edit-readthrough/?', actions.edit_readthrough), - re_path(r'^delete-readthrough/?', actions.delete_readthrough), + re_path(r'^switch-edition/?$', actions.switch_edition), + re_path(r'^edit-readthrough/?$', actions.edit_readthrough), + re_path(r'^delete-readthrough/?$', actions.delete_readthrough), re_path(r'^rate/?$', actions.rate), re_path(r'^review/?$', actions.review), diff --git a/bookwyrm/view_actions.py b/bookwyrm/view_actions.py index bb862007..bd8baf73 100644 --- a/bookwyrm/view_actions.py +++ b/bookwyrm/view_actions.py @@ -10,6 +10,7 @@ from django.contrib.auth import authenticate, login, logout from django.contrib.auth.decorators import login_required, permission_required from django.core.exceptions import PermissionDenied from django.core.files.base import ContentFile +from django.db import transaction from django.http import HttpResponseBadRequest, HttpResponseNotFound from django.shortcuts import get_object_or_404, redirect from django.template.response import TemplateResponse @@ -244,6 +245,32 @@ def edit_book(request, book_id): return redirect('/book/%s' % book.id) +@login_required +@require_POST +@transaction.atomic +def switch_edition(request): + ''' switch your copy of a book to a different edition ''' + edition_id = request.POST.get('edition') + new_edition = get_object_or_404(models.Edition, id=edition_id) + shelfbooks = models.ShelfBook.objects.filter( + book__parent_work=new_edition.parent_work, + added_by=request.user + ) + for shelfbook in shelfbooks.all(): + shelfbook.book = new_edition + shelfbook.save() + + readthroughs = models.ReadThrough.objects.filter( + book__parent_work=new_edition.parent_work, + user=request.user + ) + for readthrough in readthroughs.all(): + readthrough.book = new_edition + readthrough.save() + + return redirect('/book/%d' % new_edition.id) + + @login_required @require_POST def upload_cover(request, book_id): From af823cf6458090e343c0b7b8f471a5d38873b187 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Tue, 15 Dec 2020 17:53:20 -0800 Subject: [PATCH 3/7] Merge migration --- bookwyrm/migrations/0023_merge_20201216_0112.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 bookwyrm/migrations/0023_merge_20201216_0112.py diff --git a/bookwyrm/migrations/0023_merge_20201216_0112.py b/bookwyrm/migrations/0023_merge_20201216_0112.py new file mode 100644 index 00000000..e3af4849 --- /dev/null +++ b/bookwyrm/migrations/0023_merge_20201216_0112.py @@ -0,0 +1,14 @@ +# Generated by Django 3.0.7 on 2020-12-16 01:12 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('bookwyrm', '0017_auto_20201212_0059'), + ('bookwyrm', '0022_auto_20201212_1744'), + ] + + operations = [ + ] From 729e50de63f52e9b863c7815b970242880db0f00 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 16 Dec 2020 09:15:26 -0800 Subject: [PATCH 4/7] Show consistent book status regardless of edition --- bookwyrm/templates/snippets/shelve_button.html | 18 +++++++++--------- bookwyrm/templatetags/bookwyrm_tags.py | 9 +++++---- bookwyrm/views.py | 2 +- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/bookwyrm/templates/snippets/shelve_button.html b/bookwyrm/templates/snippets/shelve_button.html index d452169e..afbdc970 100644 --- a/bookwyrm/templates/snippets/shelve_button.html +++ b/bookwyrm/templates/snippets/shelve_button.html @@ -4,24 +4,24 @@ {% with book.id|uuid as uuid %} {% active_shelf book as active_shelf %}
- {% if active_shelf.identifier == 'read' %} + {% if active_shelf.shelf.identifier == 'read' %} - {% elif active_shelf.identifier == 'reading' %} + {% elif active_shelf.shelf.identifier == 'reading' %} - {% include 'snippets/finish_reading_modal.html' %} - {% elif active_shelf.identifier == 'to-read' %} + {% include 'snippets/finish_reading_modal.html' with book=active_shelf.book %} + {% elif active_shelf.shelf.identifier == 'to-read' %} - {% include 'snippets/start_reading_modal.html' %} + {% include 'snippets/start_reading_modal.html' with book=active_shelf.book %} {% else %}
{% csrf_token %} - +
@@ -40,17 +40,17 @@
+ {% endif %} {% endwith %} {% endif %} diff --git a/bookwyrm/templates/snippets/switch_edition_button.html b/bookwyrm/templates/snippets/switch_edition_button.html index 9771535a..685aed7c 100644 --- a/bookwyrm/templates/snippets/switch_edition_button.html +++ b/bookwyrm/templates/snippets/switch_edition_button.html @@ -1,5 +1,5 @@ {% csrf_token %} - +