Create statuses from django form

This commit is contained in:
Mouse Reeve 2020-10-26 15:00:15 -07:00
parent 6243cf0e4a
commit 2afa111b70
3 changed files with 26 additions and 82 deletions

View file

@ -9,9 +9,8 @@ import requests
from bookwyrm import activitypub
from bookwyrm import models
from bookwyrm.broadcast import broadcast
from bookwyrm.status import create_review, create_status
from bookwyrm.status import create_quotation, create_comment
from bookwyrm.status import create_tag, create_notification, create_rating
from bookwyrm.status import create_status
from bookwyrm.status import create_tag, create_notification
from bookwyrm.status import create_generated_note
from bookwyrm.status import delete_status
from bookwyrm.remote_user import get_or_create_remote_user
@ -178,16 +177,18 @@ def handle_import_books(user, items):
broadcast(user, activity)
if item.rating or item.review:
review_title = "Review of {!r} on Goodreads".format(
item.book.title,
) if item.review else ""
handle_review(
user,
item.book,
review_title,
item.review,
item.rating,
)
pass
#review_title = "Review of {!r} on Goodreads".format(
# item.book.title,
#) if item.review else ""
# TODO
#handle_review(
# user,
# item.book,
# review_title,
# item.review,
# item.rating,
#)
for read in item.reads:
read.book = item.book
read.user = user
@ -209,37 +210,9 @@ def handle_delete_status(user, status):
broadcast(user, status.to_activity())
def handle_rate(user, book, rating):
''' a review that's just a rating '''
builder = create_rating
handle_status(user, book, builder, rating)
def handle_review(user, book, name, content, rating):
''' post a review '''
# validated and saves the review in the database so it has an id
builder = create_review
handle_status(user, book, builder, name, content, rating)
def handle_quotation(user, book, content, quote):
''' post a review '''
# validated and saves the review in the database so it has an id
builder = create_quotation
handle_status(user, book, builder, content, quote)
def handle_comment(user, book, content):
''' post a comment '''
# validated and saves the review in the database so it has an id
builder = create_comment
handle_status(user, book, builder, content)
def handle_status(user, book_id, builder, *args):
def handle_status(user, form):
''' generic handler for statuses '''
book = models.Edition.objects.get(id=book_id)
status = builder(user, book, *args)
status = form.save()
broadcast(user, status.to_create_activity(user), software='bookwyrm')

View file

@ -13,6 +13,7 @@ def delete_status(status):
status.deleted_date = datetime.now()
status.save()
def create_rating(user, book, rating):
''' a review that's just a rating '''
if not rating or rating < 1 or rating > 5:

View file

@ -296,67 +296,37 @@ def shelve(request):
def rate(request):
''' just a star rating for a book '''
form = forms.RatingForm(request.POST)
book_id = request.POST.get('book')
# TODO: better failure behavior
if not form.is_valid():
return redirect('/book/%s' % book_id)
rating = form.cleaned_data.get('rating')
# throws a value error if the book is not found
outgoing.handle_rate(request.user, book_id, rating)
return redirect('/book/%s' % book_id)
return handle_status(request, form)
@login_required
def review(request):
''' create a book review '''
form = forms.ReviewForm(request.POST)
book_id = request.POST.get('book')
if not form.is_valid():
return redirect('/book/%s' % book_id)
# TODO: validation, htmlification
name = form.cleaned_data.get('name')
content = form.cleaned_data.get('content')
rating = form.data.get('rating', None)
try:
rating = int(rating)
except ValueError:
rating = None
outgoing.handle_review(request.user, book_id, name, content, rating)
return redirect('/book/%s' % book_id)
return handle_status(request, form)
@login_required
def quotate(request):
''' create a book quotation '''
form = forms.QuotationForm(request.POST)
book_id = request.POST.get('book')
if not form.is_valid():
return redirect('/book/%s' % book_id)
quote = form.cleaned_data.get('quote')
content = form.cleaned_data.get('content')
outgoing.handle_quotation(request.user, book_id, content, quote)
return redirect('/book/%s' % book_id)
return handle_status(request, form)
@login_required
def comment(request):
''' create a book comment '''
form = forms.CommentForm(request.POST)
return handle_status(request, form)
def handle_status(request, form):
''' all the review/comment/quote etc functions are the same '''
book_id = request.POST.get('book')
# TODO: better failure behavior
if not form.is_valid():
return redirect('/book/%s' % book_id)
# TODO: validation, htmlification
content = form.data.get('content')
outgoing.handle_comment(request.user, book_id, content)
outgoing.handle_status(request.user, form)
return redirect('/book/%s' % book_id)