moviewyrm/bookwyrm/view_actions.py

373 lines
11 KiB
Python
Raw Normal View History

2020-02-22 22:02:03 +00:00
''' views for actions you can take in the application '''
2020-10-30 17:40:05 +00:00
import dateutil.parser
from dateutil.parser import ParserError
2021-01-13 17:54:35 +00:00
from django.contrib.auth.decorators import login_required
2020-03-28 22:06:16 +00:00
from django.http import HttpResponseBadRequest, HttpResponseNotFound
2020-11-11 04:11:21 +00:00
from django.shortcuts import get_object_or_404, redirect
from django.utils import timezone
from django.views.decorators.http import require_POST
2020-02-22 22:02:03 +00:00
2021-01-12 19:28:03 +00:00
from bookwyrm import forms, models, outgoing
from bookwyrm.broadcast import broadcast
2021-01-12 16:08:43 +00:00
from bookwyrm.vviews import get_user_from_username, get_edition
2020-03-15 21:15:36 +00:00
2020-11-10 22:52:04 +00:00
@login_required
2020-11-28 16:22:25 +00:00
@require_POST
2020-11-10 22:52:04 +00:00
def create_shelf(request):
''' user generated shelves '''
form = forms.ShelfForm(request.POST)
2020-11-11 04:11:21 +00:00
if not form.is_valid():
return redirect(request.headers.get('Referer', '/'))
shelf = form.save()
return redirect('/user/%s/shelf/%s' % \
(request.user.localname, shelf.identifier))
@login_required
2020-11-28 16:22:25 +00:00
@require_POST
2020-11-11 04:11:21 +00:00
def edit_shelf(request, shelf_id):
''' user generated shelves '''
shelf = get_object_or_404(models.Shelf, id=shelf_id)
2020-11-11 04:33:46 +00:00
if request.user != shelf.user:
return HttpResponseBadRequest()
2021-01-11 22:32:04 +00:00
if not shelf.editable and request.POST.get('name') != shelf.name:
return HttpResponseBadRequest()
2020-11-11 04:11:21 +00:00
form = forms.ShelfForm(request.POST, instance=shelf)
2020-11-10 22:52:04 +00:00
if not form.is_valid():
2021-01-11 22:23:56 +00:00
return redirect(shelf.local_path)
2020-11-10 22:52:04 +00:00
shelf = form.save()
2021-01-11 22:23:56 +00:00
return redirect(shelf.local_path)
2020-11-10 22:52:04 +00:00
2020-11-11 04:33:46 +00:00
@login_required
2020-11-28 16:22:25 +00:00
@require_POST
2020-11-11 04:33:46 +00:00
def delete_shelf(request, shelf_id):
''' user generated shelves '''
shelf = get_object_or_404(models.Shelf, id=shelf_id)
if request.user != shelf.user or not shelf.editable:
return HttpResponseBadRequest()
shelf.delete()
return redirect('/user/%s/shelves' % request.user.localname)
2020-02-22 22:02:03 +00:00
@login_required
2020-11-28 16:22:25 +00:00
@require_POST
2020-02-22 22:02:03 +00:00
def shelve(request):
2020-03-28 22:06:16 +00:00
''' put a on a user's shelf '''
2021-01-02 16:14:28 +00:00
book = get_edition(request.POST['book'])
2020-03-30 22:03:21 +00:00
2020-02-22 22:02:03 +00:00
desired_shelf = models.Shelf.objects.filter(
identifier=request.POST['shelf'],
user=request.user
).first()
if request.POST.get('reshelve', True):
try:
current_shelf = models.Shelf.objects.get(
user=request.user,
2020-03-30 22:03:21 +00:00
edition=book
2020-02-22 22:02:03 +00:00
)
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, desired_shelf)
2020-11-06 17:07:04 +00:00
# post about "want to read" shelves
if desired_shelf.identifier == 'to-read':
outgoing.handle_reading_status(
request.user,
desired_shelf,
book,
privacy=desired_shelf.privacy
2020-11-06 17:07:04 +00:00
)
2020-02-22 22:02:03 +00:00
return redirect('/')
2020-10-28 23:52:23 +00:00
@login_required
2020-11-28 16:22:25 +00:00
@require_POST
2020-10-28 23:52:23 +00:00
def unshelve(request):
''' put a on a user's shelf '''
book = models.Edition.objects.get(id=request.POST['book'])
current_shelf = models.Shelf.objects.get(id=request.POST['shelf'])
outgoing.handle_unshelve(request.user, book, current_shelf)
return redirect(request.headers.get('Referer', '/'))
2020-11-06 16:51:50 +00:00
@login_required
2020-11-28 16:22:25 +00:00
@require_POST
2020-11-11 05:45:22 +00:00
def start_reading(request, book_id):
2020-11-06 16:51:50 +00:00
''' begin reading a book '''
2021-01-02 16:14:28 +00:00
book = get_edition(book_id)
2020-11-06 16:51:50 +00:00
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(
2020-11-06 17:07:04 +00:00
user=request.user,
2020-11-06 16:51:50 +00:00
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
2020-11-28 16:22:25 +00:00
@require_POST
2020-11-11 05:45:22 +00:00
def finish_reading(request, book_id):
2020-11-06 16:51:50 +00:00
''' a user completed a book, yay '''
2021-01-02 16:14:28 +00:00
book = get_edition(book_id)
2020-11-06 16:51:50 +00:00
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
2020-11-28 16:22:25 +00:00
@require_POST
2020-11-06 16:51:50 +00:00
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
2020-11-28 16:22:25 +00:00
@require_POST
2020-11-06 16:51:50 +00:00
def delete_readthrough(request):
''' remove a readthrough '''
2020-11-11 05:13:13 +00:00
readthrough = get_object_or_404(
models.ReadThrough, id=request.POST.get('id'))
2020-11-06 16:51:50 +00:00
# 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
@require_POST
def create_readthrough(request):
''' can't use the form because the dates are too finnicky '''
book = get_object_or_404(models.Edition, id=request.POST.get('book'))
readthrough = update_readthrough(request, create=True, book=book)
if not readthrough:
return redirect(book.local_path)
readthrough.save()
return redirect(request.headers.get('Referer', '/'))
2020-02-22 22:02:03 +00:00
@login_required
2020-11-28 16:22:25 +00:00
@require_POST
2020-02-22 22:02:03 +00:00
def tag(request):
''' tag a book '''
# I'm not using a form here because sometimes "name" is sent as a hidden
# field which doesn't validate
name = request.POST.get('name')
book_id = request.POST.get('book')
2020-11-11 05:13:13 +00:00
book = get_object_or_404(models.Edition, id=book_id)
2020-11-06 22:25:48 +00:00
tag_obj, created = models.Tag.objects.get_or_create(
name=name,
)
2020-12-31 16:30:51 +00:00
user_tag, _ = models.UserTag.objects.get_or_create(
user=request.user,
2020-11-06 22:25:48 +00:00
book=book,
tag=tag_obj,
2020-11-06 22:25:48 +00:00
)
2020-02-22 22:02:03 +00:00
2020-11-06 22:25:48 +00:00
if created:
2020-12-31 16:30:51 +00:00
broadcast(request.user, user_tag.to_add_activity(request.user))
return redirect('/book/%s' % book_id)
2020-02-22 22:02:03 +00:00
@login_required
2020-11-28 16:22:25 +00:00
@require_POST
2020-02-22 22:02:03 +00:00
def untag(request):
''' untag a book '''
name = request.POST.get('name')
2021-01-01 15:27:56 +00:00
tag_obj = get_object_or_404(models.Tag, name=name)
book_id = request.POST.get('book')
2020-12-31 16:42:09 +00:00
book = get_object_or_404(models.Edition, id=book_id)
2021-01-01 15:27:56 +00:00
user_tag = get_object_or_404(
models.UserTag, tag=tag_obj, book=book, user=request.user)
tag_activity = user_tag.to_remove_activity(request.user)
user_tag.delete()
2020-02-22 22:02:03 +00:00
2020-12-31 16:42:09 +00:00
broadcast(request.user, tag_activity)
return redirect('/book/%s' % book_id)
2020-02-22 22:02:03 +00:00
@login_required
2020-11-28 16:22:25 +00:00
@require_POST
2020-02-22 22:02:03 +00:00
def follow(request):
''' follow another user, here or abroad '''
username = request.POST['user']
try:
to_follow = get_user_from_username(username)
except models.User.DoesNotExist:
return HttpResponseBadRequest()
outgoing.handle_follow(request.user, to_follow)
2020-02-22 22:02:03 +00:00
user_slug = to_follow.localname if to_follow.localname \
else to_follow.username
return redirect('/user/%s' % user_slug)
@login_required
2020-11-28 16:22:25 +00:00
@require_POST
2020-02-22 22:02:03 +00:00
def unfollow(request):
''' unfollow a user '''
username = request.POST['user']
try:
to_unfollow = get_user_from_username(username)
except models.User.DoesNotExist:
return HttpResponseBadRequest()
outgoing.handle_unfollow(request.user, to_unfollow)
2020-02-22 22:02:03 +00:00
user_slug = to_unfollow.localname if to_unfollow.localname \
else to_unfollow.username
return redirect('/user/%s' % user_slug)
@login_required
2020-11-28 16:22:25 +00:00
@require_POST
def accept_follow_request(request):
2020-03-15 21:15:36 +00:00
''' a user accepts a follow request '''
username = request.POST['user']
try:
requester = get_user_from_username(username)
except models.User.DoesNotExist:
return HttpResponseBadRequest()
try:
2020-03-15 21:15:36 +00:00
follow_request = models.UserFollowRequest.objects.get(
user_subject=requester,
user_object=request.user
)
except models.UserFollowRequest.DoesNotExist:
# Request already dealt with.
pass
else:
2020-10-16 21:14:07 +00:00
outgoing.handle_accept(follow_request)
return redirect('/user/%s' % request.user.localname)
2020-03-15 21:15:36 +00:00
@login_required
2020-11-28 16:22:25 +00:00
@require_POST
def delete_follow_request(request):
2020-03-15 21:15:36 +00:00
''' a user rejects a follow request '''
username = request.POST['user']
try:
requester = get_user_from_username(username)
except models.User.DoesNotExist:
return HttpResponseBadRequest()
try:
2020-03-15 21:15:36 +00:00
follow_request = models.UserFollowRequest.objects.get(
user_subject=requester,
user_object=request.user
)
except models.UserFollowRequest.DoesNotExist:
return HttpResponseBadRequest()
2020-10-16 21:28:25 +00:00
outgoing.handle_reject(follow_request)
return redirect('/user/%s' % request.user.localname)
2020-03-27 16:33:31 +00:00
2020-11-06 16:51:50 +00:00
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:
2021-01-11 00:11:02 +00:00
start_date = timezone.make_aware(dateutil.parser.parse(start_date))
2020-11-06 16:51:50 +00:00
readthrough.start_date = start_date
except ParserError:
pass
finish_date = request.POST.get('finish_date')
if finish_date:
try:
2021-01-11 00:11:02 +00:00
finish_date = timezone.make_aware(
dateutil.parser.parse(finish_date))
2020-11-06 16:51:50 +00:00
readthrough.finish_date = finish_date
except ParserError:
pass
if not readthrough.start_date and not readthrough.finish_date:
return None
2020-11-06 16:51:50 +00:00
return readthrough