From dd478a3587585ba2d0317b57b4a693291c1499ed Mon Sep 17 00:00:00 2001
From: Mouse Reeve
Date: Wed, 29 Jan 2020 00:22:48 -0800
Subject: [PATCH] Re-shelve books
---
fedireads/models.py | 3 ++-
fedireads/outgoing.py | 45 +++++++++++++++++++++++++++++++++++
fedireads/templates/feed.html | 12 +++++++---
fedireads/views.py | 13 ++++++----
4 files changed, 65 insertions(+), 8 deletions(-)
diff --git a/fedireads/models.py b/fedireads/models.py
index 5720d886..7feecce6 100644
--- a/fedireads/models.py
+++ b/fedireads/models.py
@@ -111,7 +111,8 @@ class ShelveActivity(Activity):
shelf = models.ForeignKey('Shelf', on_delete=models.PROTECT)
def save(self, *args, **kwargs):
- self.activity_type = 'Add'
+ if not self.activity_type:
+ self.activity_type = 'Add'
self.fedireads_type = 'Shelve'
super().save(*args, **kwargs)
diff --git a/fedireads/outgoing.py b/fedireads/outgoing.py
index 99c77540..ec9b8ea0 100644
--- a/fedireads/outgoing.py
+++ b/fedireads/outgoing.py
@@ -108,6 +108,51 @@ def handle_shelve(user, book, shelf):
broadcast(user, activity, recipients)
+def handle_unshelve(user, book, shelf):
+ ''' a local user is getting a book put on their shelf '''
+ # update the database
+ row = models.ShelfBook.objects.get(book=book, shelf=shelf)
+ row.delete()
+
+ # send out the activitypub action
+ summary = '%s removed %s from %s' % (
+ user.username,
+ book.data['title'],
+ shelf.name
+ )
+
+ uuid = uuid4()
+ activity = {
+ '@context': 'https://www.w3.org/ns/activitystreams',
+ 'id': str(uuid),
+ 'summary': summary,
+ 'type': 'Remove',
+ 'actor': user.actor,
+ 'object': {
+ 'type': 'Document',
+ 'name': book.data['title'],
+ 'url': book.openlibrary_key
+ },
+ 'target': {
+ 'type': 'Collection',
+ 'name': shelf.name,
+ 'id': shelf.activitypub_id
+ }
+ }
+ recipients = get_recipients(user, 'public')
+
+ models.ShelveActivity(
+ uuid=uuid,
+ user=user,
+ content=activity,
+ shelf=shelf,
+ book=book,
+ activity_type='Remove',
+ ).save()
+
+ broadcast(user, activity, recipients)
+
+
def handle_review(user, book, name, content, rating):
''' post a review '''
review_uuid = uuid4()
diff --git a/fedireads/templates/feed.html b/fedireads/templates/feed.html
index 7fdc44e4..51ae43f1 100644
--- a/fedireads/templates/feed.html
+++ b/fedireads/templates/feed.html
@@ -11,7 +11,10 @@
{{ book.data.title }}
by {{ book.authors.first.data.name }}
-
+
{% endfor %}
{% endif %}
@@ -21,7 +24,10 @@
{{ book.data.title }}
by {{ book.authors.first.data.name }}
-
+
{% endfor %}
@@ -38,7 +44,7 @@
{{ book.authors.first.data.name }}
{% if not book in user_books.all %}
-
diff --git a/fedireads/views.py b/fedireads/views.py
index 2cc9aadd..271176be 100644
--- a/fedireads/views.py
+++ b/fedireads/views.py
@@ -180,12 +180,17 @@ def book_page(request, book_identifier):
@csrf_exempt
@login_required
-def shelve(request, shelf_id, book_id):
+def shelve(request, shelf_id, book_id, reshelve=True):
''' put a book on a user's shelf '''
- # TODO: handle "reshelving"
book = models.Book.objects.get(id=book_id)
- shelf = models.Shelf.objects.get(identifier=shelf_id)
- api.handle_shelve(request.user, book, shelf)
+ desired_shelf = models.Shelf.objects.get(identifier=shelf_id)
+ if reshelve:
+ try:
+ current_shelf = models.Shelf.objects.get(user=request.user, book=book)
+ api.handle_unshelve(request.user, book, current_shelf)
+ except models.Shelf.DoesNotExist:
+ pass
+ api.handle_shelve(request.user, book, desired_shelf)
return redirect('/')