def downvote_review(request, review_id): review = get_object_or_404(Review, id=review_id) try: vote = Vote.objects.get(user=request.user, review=review) except Vote.DoesNotExist: vote = Vote(user=request.user, review=review) vote.rating = -1 vote.save() return redirect(review_detail, review.id)
def new_review(request): class ReviewForm(ModelForm): def __init__(self, user, *args, **kwargs): self.user = user super(ReviewForm, self).__init__(*args, **kwargs) class Meta: model = Review exclude = ('user',) form = ReviewForm(request.user, request.POST or None, request.FILES or None) if form.is_valid(): review = form.save(commit=False) review.user = request.user review.save() # upvote newly created review vote = Vote() vote.user = request.user vote.review = review vote.rating = +1 vote.save() return redirect(review_detail, review.id) return direct_to_template(request, 'book_reviews/review_form.html', {'form': form})