Esempio n. 1
0
def likeAnswer(request, id):
    args = {}
    args.update(csrf(request))
    if request.POST:
        if 'like' in request.POST:
            a = Answer.objects.get(id=id)
            user = auth.get_user(request)
            if a.author != user:
                f = LikeToAnswer.objects.all().filter(like_author=user).filter(to_answer=a)
                if not f.exists():
                    l = LikeToAnswer(like_author=user, to_answer=a, value=1)
                    l.save()
                    a.rate += 1
                    a.save()
                else:
                    args['danger'] = "You already like or dislike it"
            else:
                args['danger'] = "You cannot like yourself"
        elif 'dislike' in request.POST:
            a = Answer.objects.get(id=id)
            user = auth.get_user(request)
            if a.author != user:
                f = LikeToAnswer.objects.all().filter(like_author=user).filter(to_answer=a)
                if not f.exists():
                    l = LikeToAnswer(like_author=user, to_answer=a, value=-1)
                    l.save()
                    a.rate -= 1
                    a.save()
                else:
                    args['danger'] = "You already like or dislike it"
            else:
                args['danger'] = "You cannot like yourself"
        return redirect(request.META.get('HTTP_REFERER'))
    else:
        return redirect(request.META.get('HTTP_REFERER'))
    def handle(self, *args, **options):
        all_users = UserProfile.objects.all()
        all_questions = Question.objects.all()
        all_answers = Answer.objects.all()

        for question in all_questions:
            for user in all_users:
                b = random.randint(0, 2)
                if b == 0:
                    likeToQuestion = LikeToQuestion(like_author=user, to_question=question, value=-1)
                    likeToQuestion.save()
                elif b == 2:
                    likeToQuestion = LikeToQuestion(like_author=user, to_question=question, value=1)
                    likeToQuestion.save()


        for answer in all_answers:
            for user in all_users:
                b = random.randint(0, 2)
                if b == 0:
                    likeToAnswer = LikeToAnswer(like_author=user, to_answer=answer, value=-1)
                    likeToAnswer.save()
                elif b == 2:
                    likeToAnswer = LikeToAnswer(like_author=user, to_answer=answer, value=1)
                    likeToAnswer.save()


        for user in all_users:
            for user1 in all_users:
                if user != user1:
                    b = random.randint(0, 2)
                    if b == 0:
                        likeToUser = LikeToUser(like_author=user, to_user=user1, value=-1)
                        likeToUser.save()
                    elif b == 2:
                        likeToUser = LikeToUser(like_author=user, to_user=user1, value=1)
                        likeToUser.save()