def review(user_id, item_id, text, rating): """ Creates a review for an item if the user has previously not written a review for the item. Returns False if the review has been created successfully, otherwise returns an error message. user_id: id of the reviewer (integer) item_id: id of the item (integer) text: text of the review (string) rating: rating of the review (integer) """ if Review.objects.filter(user=user_id, item=item_id): return "You already wrote a review!" form = ReviewForm({ 'user': user_id, 'item': item_id, 'text': text, 'rating': rating, 'agrees': 0, 'thanks': 0 }) if form.is_valid(): form.save() return False return "Something was wrong with the review you submitted!"
def disagree(request, review_id): if request.method == 'POST': review = { 'user': request.session['_auth_user_id'], 'item': get_object_or_404(Review, pk=review_id).item_id, 'text': request.POST['text'], 'rating': int(request.POST['rating']), 'agrees': 0, 'thanks': 0 } form = ReviewForm(review) if (form.is_valid() and not Review.objects.filter(user=review['user'], item=review['item'])): form.save() return redirect(request.META.get('HTTP_REFERER', None))
def review(request, title, app, model): if request.method == 'POST': model_object = get_model(app, model) review_item = get_object_or_404(model_object, url=title) review = { 'user': request.session['_auth_user_id'], 'item': review_item.pk, 'text': request.POST['text'], 'rating': int(request.POST['rating']), 'agrees': 0, 'thanks': 0 } form = ReviewForm(review) if (form.is_valid() and not Review.objects.filter(user=review['user'], item=review['item'])): form.save() return redirect(request.META.get('HTTP_REFERER', None))
def profile(request, title): try: movie = Movie.objects.select_related('item').prefetch_related( 'actors', 'directors', 'genre').get(url=title) me = request.user.id friends = list( Forward.objects.filter(source_id=me).values_list('destination_id', flat=True)) global_exclude = friends + [me] reviews = movie.item.review_set.select_related( 'user', 'user__userprofile').all() try: sp = transaction.savepoint() friend_agrees = Agree.objects.select_related( 'review','review__user').filter(giver__in=friends) \ .exclude(review__user=me) \ .exclude(giver=me) \ .order_by('review') \ .distinct('review') transaction.savepoint_commit(sp) except IntegrityError: transaction.savepoint_rollback(sp) friend_agrees = [] context = { 'movie': movie, 'actors': movie.actors.all()[:5], 'directors': movie.directors.all(), 'genre': movie.genre.all(), 'friend_bookmarks': movie.item.bookmark_set.select_related( 'user', 'user__userprofile').filter( user__in=friends), 'my_reviews': reviews.filter( Q(user=me) | Q(agree__giver=me) | Q(thank__giver=me)), 'friend_reviews': reviews.filter(user__in=friends) \ .exclude(user=me) \ .exclude(agree__giver=me) \ .exclude(thank__giver=me), 'friend_agrees': friend_agrees, 'global_reviews': reviews.exclude( user__in=global_exclude ).exclude(agree__giver__in=global_exclude), 'form': ReviewForm(), 'links': movie.item.link_set.all(), 'has_reviewed': reviews.filter(user=me).count(), 'has_bookmarked': Bookmark.objects.filter(user=me, item=movie.pk).count() } except: raise Http404 return render_to_response('movie_profile.html', context, context_instance=RequestContext(request))