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 activitypub
from bookwyrm import models from bookwyrm import models
from bookwyrm.broadcast import broadcast from bookwyrm.broadcast import broadcast
from bookwyrm.status import create_review, create_status from bookwyrm.status import create_status
from bookwyrm.status import create_quotation, create_comment from bookwyrm.status import create_tag, create_notification
from bookwyrm.status import create_tag, create_notification, create_rating
from bookwyrm.status import create_generated_note from bookwyrm.status import create_generated_note
from bookwyrm.status import delete_status from bookwyrm.status import delete_status
from bookwyrm.remote_user import get_or_create_remote_user from bookwyrm.remote_user import get_or_create_remote_user
@ -178,16 +177,18 @@ def handle_import_books(user, items):
broadcast(user, activity) broadcast(user, activity)
if item.rating or item.review: if item.rating or item.review:
review_title = "Review of {!r} on Goodreads".format( pass
item.book.title, #review_title = "Review of {!r} on Goodreads".format(
) if item.review else "" # item.book.title,
handle_review( #) if item.review else ""
user, # TODO
item.book, #handle_review(
review_title, # user,
item.review, # item.book,
item.rating, # review_title,
) # item.review,
# item.rating,
#)
for read in item.reads: for read in item.reads:
read.book = item.book read.book = item.book
read.user = user read.user = user
@ -209,37 +210,9 @@ def handle_delete_status(user, status):
broadcast(user, status.to_activity()) broadcast(user, status.to_activity())
def handle_rate(user, book, rating): def handle_status(user, form):
''' 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):
''' generic handler for statuses ''' ''' generic handler for statuses '''
book = models.Edition.objects.get(id=book_id) status = form.save()
status = builder(user, book, *args)
broadcast(user, status.to_create_activity(user), software='bookwyrm') 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.deleted_date = datetime.now()
status.save() status.save()
def create_rating(user, book, rating): def create_rating(user, book, rating):
''' a review that's just a rating ''' ''' a review that's just a rating '''
if not rating or rating < 1 or rating > 5: if not rating or rating < 1 or rating > 5:

View file

@ -296,67 +296,37 @@ def shelve(request):
def rate(request): def rate(request):
''' just a star rating for a book ''' ''' just a star rating for a book '''
form = forms.RatingForm(request.POST) form = forms.RatingForm(request.POST)
book_id = request.POST.get('book') return handle_status(request, form)
# 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)
@login_required @login_required
def review(request): def review(request):
''' create a book review ''' ''' create a book review '''
form = forms.ReviewForm(request.POST) form = forms.ReviewForm(request.POST)
book_id = request.POST.get('book') return handle_status(request, form)
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)
@login_required @login_required
def quotate(request): def quotate(request):
''' create a book quotation ''' ''' create a book quotation '''
form = forms.QuotationForm(request.POST) form = forms.QuotationForm(request.POST)
book_id = request.POST.get('book') return handle_status(request, form)
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)
@login_required @login_required
def comment(request): def comment(request):
''' create a book comment ''' ''' create a book comment '''
form = forms.CommentForm(request.POST) 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') book_id = request.POST.get('book')
# TODO: better failure behavior
if not form.is_valid(): if not form.is_valid():
return redirect('/book/%s' % book_id) return redirect('/book/%s' % book_id)
# TODO: validation, htmlification outgoing.handle_status(request.user, form)
content = form.data.get('content')
outgoing.handle_comment(request.user, book_id, content)
return redirect('/book/%s' % book_id) return redirect('/book/%s' % book_id)