def review_book(request, pk): """ Review an individual book """ book = get_object_or_404(Book, pk=pk) form = ReviewForm if request.method == 'POST': # process form form = ReviewForm(request.POST) if form.is_valid(): book.is_favourite = form.cleaned_data['is_favourite'] book.review = form.cleaned_data['review'] book.reviewed_by = request.user book.save() return redirect('review-books') context = { 'book': book, 'form': form, } return render(request, "review-book.html", context)
def test_review_too_short(self): form = ReviewForm(data={ 'is_favourite': False, 'review': 'Too short', }) self.assertFalse(form.is_valid()) self.assertTrue(form.has_error('review',code='min_length'))
def add_review(request): profile = UserProfile.objects.get(user=request.user) my_books = Book.objects.exclude(UserProfile=profile) if request.method == 'POST': form_1 = BookForm(request.POST) form_instance = ReviewForm(request.POST) if(form_instance.is_valid() and form_1.is_valid()): title = form_1.cleaned_data["title"] genre = form_1.cleaned_data["genre"] author = form_1.cleaned_data["author"] length = form_1.cleaned_data["length"] completed = form_1.cleaned_data["completed"] bk = Book.objects.create(title=title, genre=genre, author=author, length=length, completed=completed) bk.UserProfile.add(profile) description = form_instance.cleaned_data["description"] stars = form_instance.cleaned_data["stars"] recommendation = form_instance.cleaned_data["recommendation"] readability = form_instance.cleaned_data["readability"] rev = Review.objects.create(UserProfile=profile, description=description, stars=stars, recommendation=recommendation, readability=readability, book=bk) return HttpResponseRedirect('../reviews') else: form_1 = BookForm() form_instance = ReviewForm() context = { 'books': my_books, "form": form_instance, "profile": profile, "user": request.user, "form1": form_1, "is_user": checkAuth(request), } return render(request, 'add_review.html', context=context)
def reviews(request, book_id, user_id): detailurl = request.build_absolute_uri( reverse('books:detail', args=[book_id])) if request.method == 'POST': review_form = ReviewForm(request.POST) print(review_form.errors) if review_form.is_valid(): date = DateFormat(datetime.now()).format(get_format('DATE_FORMAT')) review = review_form.cleaned_data['review'] rating = review_form.cleaned_data['rating'] book = Book.objects.get(pk=book_id) reviewer = request.user new_review = Review(date=date, review=review, rating=rating, reviewed_book=book, reviewer=reviewer) new_review.save() return HttpResponseRedirect(detailurl) else: review_form = ReviewForm() return render(request, 'books/review_book.html', { 'detailurl': detailurl, 'review': review_form })
def test_no_review(self): form = ReviewForm(data={ 'is_favourite': False, }) self.assertFalse(form.is_valid()) self.assertTrue(form.has_error('review', code='required'))
def test_no_review(self): # first we test to see what happens when we do not fill in a review form = ReviewForm(data={ 'is_favourite': False, }) self.assertFalse(form.is_valid()) self.assertTrue(form.has_error('review', code='required'))
def test_review_too_short(self): form = ReviewForm(data={ 'is_favourite': False, 'review': 'Too short', }) self.assertFalse(form.is_valid()) self.assertTrue(form.has_error('review', code='min_length'))
def test_review_too_short(self): # next we check what happens when we fill in a review that is too short form = ReviewForm(data={ 'is_favourite': False, 'review': 'Too short!', }) self.assertFalse(form.is_valid()) self.assertTrue(form.has_error('review', code='min_length'))
def test_no_review(self): """ Function doc """ form = ReviewForm(data={ 'is_favourite': False, }) # print(form.is_valid()) - should print 'False' so assertFalse self.assertFalse(form.is_valid()) self.assertTrue(form.has_error('review', code='required'))
def test_review_too_short(self): """ Function doc """ form = ReviewForm(data={ 'is_favourite': False, 'review': 'Too short!', }) # print(form.is_valid()) - should print 'False' so assertFalse self.assertFalse(form.is_valid()) self.assertTrue(form.has_error('review', code='min_length'))
def comment(book_id): form = ReviewForm() if form.validate_on_submit(): comment = Review(review=form.review.data, comment=form.comment.data, book_id=book_id, writer=current_user) db.session.add(comment) db.session.commit() flash('your comment has been added', 'success') return redirect(url_for('detail', book_id=book_id)) return render_template('comment.html', title='Comment', form=form, legend='Comment')
def detail(request, pk): book = get_object_or_404(Book, pk=pk) if request.method == 'GET': form = ReviewForm() return render(request, 'detail.html', {'book': book, 'form': form}) #if request.method == 'POST': form = ReviewForm(request.POST) if form.is_valid(): review_instance = form.save(commit=False) review_instance.book = book review_instance.save() book.number_of_reviews = book.review_set.all().count() book.save() action.send(book, verb='was reviewed', reviewer=review_instance.name_of_reviewer, text=review_instance.text) return HttpResponseRedirect(reverse('detail', args=(book.pk,))) return render(request, 'detail.html', {'book': book, 'form': form})
def create_comment(request, pk): if request.method == 'POST': form = ReviewForm(request.POST) if form.is_valid(): instance = form.save(commit=False) instance.user = request.user instance.book = Book.objects.get(pk=pk) if instance.would_recommend: print('lul') instance.book.positive_votes = F('positive_votes') + 1 else: print('wat') instance.book.negative_votes = F('negative_votes') + 1 instance.book.save() instance.save() return redirect('book_detail', pk=pk)
def review_edit(request, book_id, review_id=None): book = get_object_or_404(Book, id=book_id) if review_id is None: review = None else: review = get_object_or_404(Review, id=review_id) if request.method == 'POST': form = ReviewForm(request.POST)
def add_review(request, book_id): book = Book.objects.get(id=book_id) if Review.objects.filter(writer=request.user.reviewwriter, book=book): # Avoid duplicate reviews here, one per author and book return redirect(reverse('book', kwargs={'book_id': book_id})) if request.method == 'POST': # Data has been submitted form = ReviewForm(request.POST) if form.is_valid(): cd = form.cleaned_data Review.objects.create(rating=cd['rating'], opinion=cd['opinion'], writer=request.user.reviewwriter, book=book) return redirect(reverse('book', kwargs={'book_id': book_id})) else: # First time, don't validate form = ReviewForm() return render(request, 'add_review.html', {'form': form})
def one_book(request, id): book = Book.objects.get(id=id) reviews = Review.objects.filter(book=book, visible=True) form = ReviewForm(request.POST) if request.method == 'POST': user = request.user print('USER !!!!!!!', user) if not request.user.is_authenticated: return HttpResponse('Пожалуйста, авторизируйтесь, чтобы оставить комментарий') if form.is_valid(): title = form.cleaned_data['title'] text = form.cleaned_data['text'] new_form = form.save(commit = False) book = Book.objects.get(id=id) new_form.user = request.user new_form.book = book print('**********', datetime.now(), book) new_form.save() return HttpResponse('Ваша рецензия была отправлена на модерацию и будет в скором времени опубликована. Спасибо!') else: form = ReviewForm() return render(request, 'books/one_book.html', {'book': book, 'form': form, 'reviews': reviews})
def edit_review(request, id): profile = UserProfile.objects.get(user=request.user) review = Review.objects.get(id=id) if(request.method=='POST'): review_form = ReviewForm(request.POST, instance=review) if review_form.is_valid(): review_form.save() return HttpResponseRedirect("../../") else: review_form = ReviewForm(instance=review) context = { "form": review_form, "user": request.user, "profile": profile, "is_user": checkAuth(request), } return render(request, 'edit_review.html', context=context)
def test_no_review(self): form = ReviewForm(data={"is_favourite": False}) self.assertFalse(form.is_valid()) self.assertTrue(form.has_error("review", code="required"))
def test_review_too_short(self): form = ReviewForm(data={"is_favourite": False, "review": "Too short!"}) self.assertFalse(form.is_valid()) self.assertTrue(form.has_error("review", code="min_length"))