forked from mirrors/bookwyrm
Re-shelve books
This commit is contained in:
parent
f3330ab6e7
commit
dd478a3587
4 changed files with 65 additions and 8 deletions
|
@ -111,6 +111,7 @@ class ShelveActivity(Activity):
|
||||||
shelf = models.ForeignKey('Shelf', on_delete=models.PROTECT)
|
shelf = models.ForeignKey('Shelf', on_delete=models.PROTECT)
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
|
if not self.activity_type:
|
||||||
self.activity_type = 'Add'
|
self.activity_type = 'Add'
|
||||||
self.fedireads_type = 'Shelve'
|
self.fedireads_type = 'Shelve'
|
||||||
super().save(*args, **kwargs)
|
super().save(*args, **kwargs)
|
||||||
|
|
|
@ -108,6 +108,51 @@ def handle_shelve(user, book, shelf):
|
||||||
broadcast(user, activity, recipients)
|
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):
|
def handle_review(user, book, name, content, rating):
|
||||||
''' post a review '''
|
''' post a review '''
|
||||||
review_uuid = uuid4()
|
review_uuid = uuid4()
|
||||||
|
|
|
@ -11,7 +11,10 @@
|
||||||
<img class="book-cover small" src="{% if book.cover %}/images/{{ book.cover }}{% else %}/static/images/no_cover.jpg{% endif %}">
|
<img class="book-cover small" src="{% if book.cover %}/images/{{ book.cover }}{% else %}/static/images/no_cover.jpg{% endif %}">
|
||||||
<p class="title"><a href="{{ book.openlibrary_key }}">{{ book.data.title }}</a></p>
|
<p class="title"><a href="{{ book.openlibrary_key }}">{{ book.data.title }}</a></p>
|
||||||
<p>by <a href="" class="author">{{ book.authors.first.data.name }}</a></p>
|
<p>by <a href="" class="author">{{ book.authors.first.data.name }}</a></p>
|
||||||
<button>start reading</button>
|
<form name="shelve" action="/shelve/{{ user.localname }}_currently-reading/{{ book.id }}" method="post">
|
||||||
|
<input type="hidden" name="book" value="book.id"></input>
|
||||||
|
<button type="submit">Start reading</button>
|
||||||
|
</form>
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
@ -21,7 +24,10 @@
|
||||||
<img class="book-cover small" src="{% if book.cover %}/images/{{ book.cover }}{% else %}/static/images/no_cover.jpg{% endif %}">
|
<img class="book-cover small" src="{% if book.cover %}/images/{{ book.cover }}{% else %}/static/images/no_cover.jpg{% endif %}">
|
||||||
<p class="title"><a href="{{ book.openlibrary_key }}">{{ book.data.title }}</a></p>
|
<p class="title"><a href="{{ book.openlibrary_key }}">{{ book.data.title }}</a></p>
|
||||||
<p>by <a href="" class="author">{{ book.authors.first.data.name }}</a></p>
|
<p>by <a href="" class="author">{{ book.authors.first.data.name }}</a></p>
|
||||||
<button>done reading</button>
|
<form name="shelve" action="/shelve/{{ user.localname }}_read/{{ book.id }}" method="post">
|
||||||
|
<input type="hidden" name="book" value="book.id"></input>
|
||||||
|
<button type="submit">I'm done!</button>
|
||||||
|
</form>
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
|
@ -38,7 +44,7 @@
|
||||||
<a href="" class="author">{{ book.authors.first.data.name }}</a>
|
<a href="" class="author">{{ book.authors.first.data.name }}</a>
|
||||||
</p>
|
</p>
|
||||||
{% if not book in user_books.all %}
|
{% if not book in user_books.all %}
|
||||||
<form name="shelve" action="/shelve/{{ request.user.localname }}_to-read/{{ book.id }}" method="post">
|
<form name="shelve" action="/shelve/{{ user.localname }}_to-read/{{ book.id }}" method="post">
|
||||||
<input type="hidden" name="book" value="book.id"></input>
|
<input type="hidden" name="book" value="book.id"></input>
|
||||||
<button type="submit">Want to read</button>
|
<button type="submit">Want to read</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
|
@ -180,12 +180,17 @@ def book_page(request, book_identifier):
|
||||||
|
|
||||||
@csrf_exempt
|
@csrf_exempt
|
||||||
@login_required
|
@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 '''
|
''' put a book on a user's shelf '''
|
||||||
# TODO: handle "reshelving"
|
|
||||||
book = models.Book.objects.get(id=book_id)
|
book = models.Book.objects.get(id=book_id)
|
||||||
shelf = models.Shelf.objects.get(identifier=shelf_id)
|
desired_shelf = models.Shelf.objects.get(identifier=shelf_id)
|
||||||
api.handle_shelve(request.user, book, shelf)
|
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('/')
|
return redirect('/')
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue