def vote(request): answer_id = request.POST['answer'] answer = Answer.objects.get(pk=answer_id) vote = request.POST['vote'] user = request.user activity = Activity.objects.filter(Q(activity_type=Activity.UP_VOTE) | Q(activity_type=Activity.DOWN_VOTE), user=user, answer=answer_id) if activity: activity.delete() if vote in [Activity.UP_VOTE, Activity.DOWN_VOTE]: activity = Activity(activity_type=vote, user=user, answer=answer_id) activity.save() return HttpResponse(answer.calculate_votes())
def favorite(request): question_id = request.POST['question'] question = Question.objects.get(pk=question_id) user = request.user activity = Activity.objects.filter(activity_type=Activity.FAVORITE, user=user, question=question_id) if activity: activity.delete() user.profile.unotify_favorited(question) else: activity = Activity(activity_type=Activity.FAVORITE, user=user, question=question_id) activity.save() user.profile.notify_favorited(question) return HttpResponse(question.calculate_favorites())
def like(request): feed_id = request.POST['feed'] feed = Feed.objects.get(pk=feed_id) user = request.user like = Activity.objects.filter(activity_type=Activity.LIKE, feed=feed_id, user=user) if like: user.profile.unotify_liked(feed) like.delete() else: like = Activity(activity_type=Activity.LIKE, feed=feed_id, user=user) like.save() user.profile.notify_liked(feed) return HttpResponse(feed.calculate_likes())