mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-26 19:41:11 +00:00
create readthroughs
This commit is contained in:
parent
7bf39d3bf7
commit
c64acf559b
5 changed files with 200 additions and 111 deletions
|
@ -1,5 +1,4 @@
|
|||
''' handles all the activity coming out of the server '''
|
||||
from datetime import datetime
|
||||
import re
|
||||
|
||||
from django.db import IntegrityError, transaction
|
||||
|
@ -121,6 +120,19 @@ def handle_shelve(user, book, shelf):
|
|||
|
||||
broadcast(user, shelve.to_add_activity(user))
|
||||
|
||||
|
||||
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)
|
||||
activity = row.to_remove_activity(user)
|
||||
row.delete()
|
||||
|
||||
broadcast(user, activity)
|
||||
|
||||
|
||||
def handle_reading_status(user, shelf, book, privacy):
|
||||
''' post about a user reading a book '''
|
||||
# tell the world about this cool thing that happened
|
||||
try:
|
||||
message = {
|
||||
|
@ -132,41 +144,17 @@ def handle_shelve(user, book, shelf):
|
|||
# it's a non-standard shelf, don't worry about it
|
||||
return
|
||||
|
||||
status = create_generated_note(user, message, mention_books=[book])
|
||||
status = create_generated_note(
|
||||
user,
|
||||
message,
|
||||
mention_books=[book],
|
||||
privacy=privacy
|
||||
)
|
||||
status.save()
|
||||
|
||||
if shelf.identifier == 'reading':
|
||||
read = models.ReadThrough(
|
||||
user=user,
|
||||
book=book,
|
||||
start_date=datetime.now())
|
||||
read.save()
|
||||
elif shelf.identifier == 'read':
|
||||
read = models.ReadThrough.objects.filter(
|
||||
user=user,
|
||||
book=book,
|
||||
finish_date=None).order_by('-created_date').first()
|
||||
if not read:
|
||||
read = models.ReadThrough(
|
||||
user=user,
|
||||
book=book,
|
||||
start_date=datetime.now())
|
||||
read.finish_date = datetime.now()
|
||||
read.save()
|
||||
|
||||
broadcast(user, status.to_create_activity(user))
|
||||
|
||||
|
||||
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)
|
||||
activity = row.to_remove_activity(user)
|
||||
row.delete()
|
||||
|
||||
broadcast(user, activity)
|
||||
|
||||
|
||||
def handle_imported_book(user, item, include_reviews, privacy):
|
||||
''' process a goodreads csv and then post about it '''
|
||||
if isinstance(item.book, models.Work):
|
||||
|
|
|
@ -14,7 +14,7 @@ def delete_status(status):
|
|||
status.save()
|
||||
|
||||
|
||||
def create_generated_note(user, content, mention_books=None):
|
||||
def create_generated_note(user, content, mention_books=None, privacy='public'):
|
||||
''' a note created by the app about user activity '''
|
||||
# sanitize input html
|
||||
parser = InputHtmlParser()
|
||||
|
@ -24,6 +24,7 @@ def create_generated_note(user, content, mention_books=None):
|
|||
status = models.GeneratedNote.objects.create(
|
||||
user=user,
|
||||
content=content,
|
||||
privacy=privacy
|
||||
)
|
||||
|
||||
if mention_books:
|
||||
|
|
|
@ -30,15 +30,23 @@
|
|||
|
||||
<div class="dropdown-menu">
|
||||
<ul class="dropdown-content">
|
||||
<form class="dropdown-item" name="shelve" action="/shelve/" method="post">
|
||||
{% csrf_token %}
|
||||
<input type="hidden" name="book" value="{{ book.id }}">
|
||||
{% for shelf in request.user.shelf_set.all %}
|
||||
<li>
|
||||
<button class="button is-small" name="shelf" type="submit" value="{{ shelf.identifier }}" {% if shelf in book.shelf_set.all %} disabled {% endif %}>{{ shelf.name }} {% if shelf in book.shelf_set.all %} ✓ {% endif %}</button>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</form>
|
||||
{% for shelf in request.user.shelf_set.all %}
|
||||
<li>
|
||||
{% if shelf.identifier == 'reading' and active_shelf.identifier != 'reading' %}
|
||||
<div class="dropdown-item">
|
||||
<label class="button is-small" for="start-reading-{{ uuid }}">
|
||||
{{ shelf.name }}
|
||||
</label>
|
||||
</div>
|
||||
{% else %}
|
||||
<form class="dropdown-item" name="shelve" action="/shelve/" method="post">
|
||||
{% csrf_token %}
|
||||
<input type="hidden" name="book" value="{{ book.id }}">
|
||||
<button class="button is-small" name="shelf" type="submit" value="{{ shelf.identifier }}" {% if shelf in book.shelf_set.all %} disabled {% endif %}>{{ shelf.name }} {% if shelf in book.shelf_set.all %} ✓ {% endif %}</button>
|
||||
</form>
|
||||
{% endif %}
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -55,14 +63,14 @@
|
|||
</header>
|
||||
<form name="start-reading" action="/start-reading" method="post">
|
||||
<section class="modal-card-body">
|
||||
{% csrf_token %}
|
||||
<div class="field">
|
||||
<label class="label" for="start_date">
|
||||
Started reading
|
||||
<input type="date" name="start_date" class="input" id="id_start_date-{{ uuid }}" value="{% now "Y-m-d" %}">
|
||||
</label>
|
||||
</div>
|
||||
</form>
|
||||
{% csrf_token %}
|
||||
<input type="hidden" name="book" value="{{ book.id }}">
|
||||
<div class="field">
|
||||
<label class="label" for="start_date">
|
||||
Started reading
|
||||
<input type="date" name="start_date" class="input" id="id_start_date-{{ uuid }}" value="{% now "Y-m-d" %}">
|
||||
</label>
|
||||
</div>
|
||||
</section>
|
||||
<footer class="modal-card-foot">
|
||||
<div class="columns">
|
||||
|
@ -74,7 +82,7 @@
|
|||
</label>
|
||||
</div>
|
||||
<div class="column">
|
||||
<button class="button is-success">Save</button>
|
||||
<button type="submit" class="button is-success">Save</button>
|
||||
<button class="button">Cancel</button>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -97,21 +105,21 @@
|
|||
{% active_read_through book user as readthrough %}
|
||||
<form name="finish-reading" action="/finish-reading" method="post">
|
||||
<section class="modal-card-body">
|
||||
{% csrf_token %}
|
||||
<input type="hidden" name="id" value="{{ readthrough.id }}">
|
||||
<div class="field">
|
||||
<label class="label" for="start_date">
|
||||
Started reading
|
||||
<input type="date" name="start_date" class="input" id="id_start_date-{{ readthrough.id }}" value="{{ readthrough.start_date | date:"Y-m-d" }}">
|
||||
</label>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label class="label" for="finish_date">
|
||||
Finished reading
|
||||
<input type="date" name="finish_date" class="input" id="id_finish_date-{{ readthrough.id }}" value="{% now "Y-m-d" %}">
|
||||
</label>
|
||||
</div>
|
||||
</form>
|
||||
{% csrf_token %}
|
||||
<input type="hidden" name="book" value="{{ book.id }}">
|
||||
<input type="hidden" name="id" value="{{ readthrough.id }}">
|
||||
<div class="field">
|
||||
<label class="label" for="start_date">
|
||||
Started reading
|
||||
<input type="date" name="start_date" class="input" id="id_start_date-{{ readthrough.id }}" value="{{ readthrough.start_date | date:"Y-m-d" }}">
|
||||
</label>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label class="label" for="finish_date">
|
||||
Finished reading
|
||||
<input type="date" name="finish_date" class="input" id="id_finish_date-{{ readthrough.id }}" value="{% now "Y-m-d" %}">
|
||||
</label>
|
||||
</div>
|
||||
</section>
|
||||
<footer class="modal-card-foot">
|
||||
<div class="columns">
|
||||
|
@ -123,7 +131,7 @@
|
|||
</label>
|
||||
</div>
|
||||
<div class="column">
|
||||
<button class="button is-success">Save</button>
|
||||
<button type="submit" class="button is-success">Save</button>
|
||||
<button class="button">Cancel</button>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -121,6 +121,8 @@ urlpatterns = [
|
|||
|
||||
re_path(r'^shelve/?$', actions.shelve),
|
||||
re_path(r'^unshelve/?$', actions.unshelve),
|
||||
re_path(r'^start-reading/?$', actions.start_reading),
|
||||
re_path(r'^finish-reading/?$', actions.finish_reading),
|
||||
|
||||
re_path(r'^follow/?$', actions.follow),
|
||||
re_path(r'^unfollow/?$', actions.unfollow),
|
||||
|
|
|
@ -272,51 +272,6 @@ def upload_cover(request, book_id):
|
|||
return redirect('/book/%s' % book.id)
|
||||
|
||||
|
||||
@login_required
|
||||
def edit_readthrough(request):
|
||||
''' can't use the form because the dates are too finnicky '''
|
||||
try:
|
||||
readthrough = models.ReadThrough.objects.get(id=request.POST.get('id'))
|
||||
except models.ReadThrough.DoesNotExist:
|
||||
return HttpResponseNotFound()
|
||||
|
||||
# don't let people edit other people's data
|
||||
if request.user != readthrough.user:
|
||||
return HttpResponseBadRequest()
|
||||
|
||||
# convert dates into a legible format
|
||||
start_date = request.POST.get('start_date')
|
||||
try:
|
||||
start_date = dateutil.parser.parse(start_date)
|
||||
except ParserError:
|
||||
start_date = None
|
||||
readthrough.start_date = start_date
|
||||
finish_date = request.POST.get('finish_date')
|
||||
try:
|
||||
finish_date = dateutil.parser.parse(finish_date)
|
||||
except ParserError:
|
||||
finish_date = None
|
||||
readthrough.finish_date = finish_date
|
||||
readthrough.save()
|
||||
return redirect(request.headers.get('Referer', '/'))
|
||||
|
||||
|
||||
@login_required
|
||||
def delete_readthrough(request):
|
||||
''' remove a readthrough '''
|
||||
try:
|
||||
readthrough = models.ReadThrough.objects.get(id=request.POST.get('id'))
|
||||
except models.ReadThrough.DoesNotExist:
|
||||
return HttpResponseNotFound()
|
||||
|
||||
# don't let people edit other people's data
|
||||
if request.user != readthrough.user:
|
||||
return HttpResponseBadRequest()
|
||||
|
||||
readthrough.delete()
|
||||
return redirect(request.headers.get('Referer', '/'))
|
||||
|
||||
|
||||
@login_required
|
||||
def shelve(request):
|
||||
''' put a on a user's shelf '''
|
||||
|
@ -351,6 +306,107 @@ def unshelve(request):
|
|||
return redirect(request.headers.get('Referer', '/'))
|
||||
|
||||
|
||||
@login_required
|
||||
def start_reading(request):
|
||||
''' begin reading a book '''
|
||||
book = books_manager.get_edition(request.POST['book'])
|
||||
shelf = models.Shelf.objects.filter(
|
||||
identifier='reading',
|
||||
user=request.user
|
||||
).first()
|
||||
|
||||
# create a readthrough
|
||||
readthrough = update_readthrough(request, book=book)
|
||||
if readthrough.start_date:
|
||||
readthrough.save()
|
||||
|
||||
# shelve the book
|
||||
if request.POST.get('reshelve', True):
|
||||
try:
|
||||
current_shelf = models.Shelf.objects.get(
|
||||
user=request.user,
|
||||
edition=book
|
||||
)
|
||||
outgoing.handle_unshelve(request.user, book, current_shelf)
|
||||
except models.Shelf.DoesNotExist:
|
||||
# this just means it isn't currently on the user's shelves
|
||||
pass
|
||||
outgoing.handle_shelve(request.user, book, shelf)
|
||||
|
||||
# post about it (if you want)
|
||||
if request.POST.get('post-status'):
|
||||
privacy = request.POST.get('privacy')
|
||||
outgoing.handle_reading_status(request.user, shelf, book, privacy)
|
||||
|
||||
return redirect(request.headers.get('Referer', '/'))
|
||||
|
||||
|
||||
@login_required
|
||||
def finish_reading(request):
|
||||
''' a user completed a book, yay '''
|
||||
book = books_manager.get_edition(request.POST['book'])
|
||||
shelf = models.Shelf.objects.filter(
|
||||
identifier='read',
|
||||
user=request.user
|
||||
).first()
|
||||
|
||||
# update or create a readthrough
|
||||
readthrough = update_readthrough(request, book=book)
|
||||
if readthrough.start_date or readthrough.finish_date:
|
||||
readthrough.save()
|
||||
|
||||
# shelve the book
|
||||
if request.POST.get('reshelve', True):
|
||||
try:
|
||||
current_shelf = models.Shelf.objects.get(
|
||||
user=request.user,
|
||||
edition=book
|
||||
)
|
||||
outgoing.handle_unshelve(request.user, book, current_shelf)
|
||||
except models.Shelf.DoesNotExist:
|
||||
# this just means it isn't currently on the user's shelves
|
||||
pass
|
||||
outgoing.handle_shelve(request.user, book, shelf)
|
||||
|
||||
# post about it (if you want)
|
||||
if request.POST.get('post-status'):
|
||||
privacy = request.POST.get('privacy')
|
||||
outgoing.handle_reading_status(request.user, shelf, book, privacy)
|
||||
|
||||
return redirect(request.headers.get('Referer', '/'))
|
||||
|
||||
|
||||
@login_required
|
||||
def edit_readthrough(request):
|
||||
''' can't use the form because the dates are too finnicky '''
|
||||
readthrough = update_readthrough(request, create=False)
|
||||
if not readthrough:
|
||||
return HttpResponseNotFound()
|
||||
|
||||
# don't let people edit other people's data
|
||||
if request.user != readthrough.user:
|
||||
return HttpResponseBadRequest()
|
||||
readthrough.save()
|
||||
|
||||
return redirect(request.headers.get('Referer', '/'))
|
||||
|
||||
|
||||
@login_required
|
||||
def delete_readthrough(request):
|
||||
''' remove a readthrough '''
|
||||
try:
|
||||
readthrough = models.ReadThrough.objects.get(id=request.POST.get('id'))
|
||||
except models.ReadThrough.DoesNotExist:
|
||||
return HttpResponseNotFound()
|
||||
|
||||
# don't let people edit other people's data
|
||||
if request.user != readthrough.user:
|
||||
return HttpResponseBadRequest()
|
||||
|
||||
readthrough.delete()
|
||||
return redirect(request.headers.get('Referer', '/'))
|
||||
|
||||
|
||||
@login_required
|
||||
def rate(request):
|
||||
''' just a star rating for a book '''
|
||||
|
@ -578,3 +634,37 @@ def create_invite(request):
|
|||
invite.save()
|
||||
|
||||
return redirect('/invite')
|
||||
|
||||
|
||||
def update_readthrough(request, book=None, create=True):
|
||||
''' updates but does not save dates on a readthrough '''
|
||||
try:
|
||||
read_id = request.POST.get('id')
|
||||
if not read_id:
|
||||
raise models.ReadThrough.DoesNotExist
|
||||
readthrough = models.ReadThrough.objects.get(id=read_id)
|
||||
except models.ReadThrough.DoesNotExist:
|
||||
if not create or not book:
|
||||
return None
|
||||
readthrough = models.ReadThrough(
|
||||
user=request.user,
|
||||
book=book,
|
||||
)
|
||||
|
||||
start_date = request.POST.get('start_date')
|
||||
if start_date:
|
||||
try:
|
||||
start_date = dateutil.parser.parse(start_date)
|
||||
readthrough.start_date = start_date
|
||||
except ParserError:
|
||||
pass
|
||||
|
||||
finish_date = request.POST.get('finish_date')
|
||||
if finish_date:
|
||||
try:
|
||||
finish_date = dateutil.parser.parse(finish_date)
|
||||
readthrough.finish_date = finish_date
|
||||
except ParserError:
|
||||
pass
|
||||
|
||||
return readthrough
|
||||
|
|
Loading…
Reference in a new issue