def review(request): ''' create a book review note ''' form = forms.ReviewForm(request.POST) book_identifier = request.POST.get('book') # TODO: better failure behavior if not form.is_valid(): return redirect('/book/%s' % book_identifier) # TODO: validation, htmlification name = form.data.get('name') content = form.data.get('content') rating = int(form.data.get('rating')) outgoing.handle_review(request.user, book_identifier, name, content, rating) return redirect('/book/%s' % book_identifier)
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)
def review(request): ''' create a book review ''' form = forms.ReviewForm(request.POST) book_identifier = request.POST.get('book') # TODO: better failure behavior if not form.is_valid(): return redirect('/book/%s' % book_identifier) # 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 # throws a value error if the book is not found book = get_or_create_book(book_identifier) outgoing.handle_review(request.user, book, name, content, rating) return redirect('/book/%s' % book_identifier)
def import_data(request): ''' ingest a goodreads csv ''' form = forms.ImportForm(request.POST, request.FILES) if form.is_valid(): results = [] reviews = [] failures = [] for item in GoodreadsCsv( TextIOWrapper(request.FILES['csv_file'], encoding=request.encoding)): try: item.resolve() except HTTPError: pass if item.book: results.append(item) if item.rating or item.review: reviews.append(item) else: failures.append(item) outgoing.handle_import_books(request.user, results) for item in reviews: review_title = "Review of {!r} on Goodreads".format( item.book.title, ) if item.review else "" outgoing.handle_review( request.user, item.book, review_title, item.review, item.rating, ) return TemplateResponse(request, 'import_results.html', { 'success_count': len(results), 'failures': failures, }) return HttpResponseBadRequest()