Example #1
0
def poll(request, uuid):
    #topic = PollTopic.objects.all()
    #options = topic[0].options.split(',')
    #votes = topic[0].votes.split(',')
    try:
        topic = PollTopic.objects.get(id=uuid)
        options = topic.options.split(',')
        votes = topic.votes.split(',')
        voters = topic.voters.split(',')
        CommentList = Comment.objects.filter(Poll=topic)
    except PollTopic.DoesNotExist:
        raise Http404('Topic does not exist')

    voted = False
    voters = voters[:-1]
    for voter in voters:
        if request.user.username == voter.split(':')[0]:
            voted = True
            break

    votesPercentage = []
    total = 0
    for vote in votes:
        total += int(vote)

    if (total != 0):
        for vote in votes:
            votesPercentage.append(int(round(int(vote) / total * 100)))
    else:
        for vote in votes:
            votesPercentage.append(0)

    #owner = topic[0].owner.username
    owner = topic.owner.user.username
    poll_info = zip(options, votesPercentage)

    context = {
        'title': topic.title,
        'owner': owner,
        'options': options,
        'votes': poll_info,
        'uuid': uuid,
        'voted': voted,
        'CommentList': CommentList,
        'form': CommentForm(),
        'tags': topic.get_tags(),
    }

    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.cleaned_data['text']
            PollId = form.cleaned_data['PollId']
            Poll = (PollTopic.objects.filter(id=PollId))[0]
            newComment = Comment(text=comment, Poll=Poll, user=request.user)
            newComment.save()
            return redirect('poll', uuid)

    return render(request, 'poll.html', context=context)
Example #2
0
def profile(request):
    account = request.user.account
    topics = PollTopic.objects.filter(owner=account).order_by("-date")[:10]
    num_own_polls = topics.count()
    titles = []
    images = [] 
    owners = []
    uuids = []
    voters =[]
    dates = []
    bestPerc = []
    bestOpt = []
    commentListList=[]
    commentNums = []
    
    for topic in topics:
        titles.append(topic.title)
        images.append(topic.img)
        owners.append(topic.owner.user.username)
        uuids.append(topic.id)
        voters.append(len(topic.voters.split(','))-1)
        dates.append(topic.date)
        option, percentage= getBestOptionPoll(topic)
        bestOpt.append(option)
        bestPerc.append(percentage)
        commentListList.append(Comment.getPollComments(topic))
        commentNums.append(len(Comment.objects.filter(Poll=topic)))
        

    polls = zip(uuids, titles, images, owners, voters, dates,bestOpt,bestPerc,commentListList,commentNums)
    context = {
        'username': account.user.username,
        'firstname': account.user.first_name,
        'lastname': account.user.last_name,
        'email': account.user.email,
        'bio' : account.bio,
        'img' : account.img,
        'polls': polls,
        'num_own_polls': num_own_polls,
        'now': timezone.now(),
        'tagList':account.get_tag_list(),
        'form':CommentForm(),
    }
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.cleaned_data['text']
            PollId = form.cleaned_data['PollId']
            Poll = (PollTopic.objects.filter(id = PollId))[0]
            newComment = Comment(text = comment, Poll = Poll, user = request.user)
            newComment.save()
            return redirect('profile')

    return render(request, 'profile.html', context=context)
Example #3
0
def room(request, uuid,):
    debateTopic = Debates.objects.filter(id=uuid)[0]
    commentList = Comment.getDebateComments(debateTopic)
    context = {
        'room_name_json': mark_safe(json.dumps(uuid)),
        'user':request.user,
        'debateTopic':debateTopic,
        'tagList':debateTopic.get_tag_list(),
        'commentList':commentList,
    }
    return render(request, 'chat/room.html', context)
Example #4
0
def post_comment(request):
    comment = request.GET.get('comment', None)
    userId = request.GET.get('user', None)
    user = User.objects.filter(id=userId)[0]
    Id = request.GET.get('id', None)
    data_type = request.GET.get('data_type', None)
    if (data_type == "PollTopic"):
        new_comment = Comment(user=user,
                              Poll=(PollTopic.objects.filter(id=Id)[0]),
                              text=comment)
    else:
        new_comment = Comment(user=user,
                              debate=PastDebates.objects.filter(id=Id)[0],
                              text=comment)
    date = datetime.now()
    formatedDate = date.strftime("%b. %d, %Y, %I:%M %p")
    new_comment.save()
    print(datetime.now())
    return JsonResponse({
        'username': user.username,
        'url': user.account.img.url,
        'date': formatedDate
    })
Example #5
0
def profile(request):
    account = request.user.account
    topics = PollTopic.objects.filter(owner=account).order_by("-date")[:10]
    num_own_polls = topics.count()
    titles = []
    images = []
    owners = []
    uuids = []
    voters = []
    dates = []
    bestPerc = []
    bestOpt = []
    commentListList = []
    commentNums = []
    colorsPoll = []
    color = False

    for topic in topics:
        titles.append(topic.title)
        images.append(topic.img)
        owners.append(topic.owner.user.username)
        uuids.append(topic.id)
        voters.append(len(topic.voters.split(',')) - 1)
        dates.append(topic.date)
        option, percentage = getBestOptionPoll(topic)
        bestOpt.append(option)
        bestPerc.append(percentage)
        commentListList.append(Comment.getPollComments(topic))
        commentNums.append(len(Comment.objects.filter(Poll=topic)))
        colorsPoll.append(color)
        if color:
            color = False
        else:
            color = True
        print(color)

    polls = zip(uuids, titles, images, owners, voters, dates, bestOpt,
                bestPerc, commentListList, commentNums, colorsPoll)

    discussionTopics = Discussion.objects.filter(
        initial_user=account.user).order_by("-date")
    num_own_discussion = discussionTopics.count()

    pastDebates = PastDebates.objects.filter(
        user1=account).order_by("-date")[:10]
    pastUuids = []
    pastUser1 = []
    pastUser2 = []
    pastUser1Position = []
    pastUser2Position = []
    pastUser1Votes = []
    pastUser2Votes = []
    pastCategories = []
    pastTopics = []
    pastDates = []
    colorsDebate = []
    count_winning = 0
    count_loss = 0

    for pastDebate in pastDebates:
        pastUuids.append(pastDebate.id)
        pastUser1.append(User.objects.filter(username=pastDebate.user1)[0])
        pastUser2.append(User.objects.filter(username=pastDebate.user2)[0])
        pastUser1Votes.append(pastDebate.user1votes)
        pastUser2Votes.append(pastDebate.user2votes)
        if (pastDebate.user1votes > pastDebate.user2votes):
            count_winning += 1
        else:
            count_loss += 1

        pastCategories.append(pastDebate.get_tag_list())
        pastTopics.append(pastDebate.topic)
        pastDates.append(pastDebate.date)
        colorsDebate.append(color)
        if color:
            color = False
        else:
            color = True

    viewPast = zip(pastUuids, pastUser1, pastUser2, pastUser1Votes,
                   pastUser2Votes, pastCategories, pastTopics, pastDates,
                   colorsDebate)

    context = {
        'username': account.user.username,
        'firstname': account.user.first_name,
        'lastname': account.user.last_name,
        'email': account.user.email,
        'bio': account.bio,
        'img': account.img,
        'polls': polls,
        'num_own_polls': num_own_polls,
        'num_own_discussion': num_own_discussion,
        'now': timezone.now(),
        'tagList': account.get_tag_list(),
        'form': CommentForm(),
        'count_winning': count_winning,
        'viewPast': viewPast,
        'count_loss': count_loss,
    }
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.cleaned_data['text']
            PollId = form.cleaned_data['PollId']
            Poll = (PollTopic.objects.filter(id=PollId))[0]
            newComment = Comment(text=comment, Poll=Poll, user=request.user)
            newComment.save()
            return redirect('profile')

    return render(request, 'profile.html', context=context)
Example #6
0
def pastChat(request, uuid):
    if Debates.objects.filter(id=uuid):
        Debates.objects.filter(id=uuid).delete()
    if request.method == 'POST':
        vote_form = whichVote(request.POST, request.FILES)
        pastDebate = PastDebates.objects.get(id=uuid)
        numUser = vote_form.data['vote']
        numUser = numUser[:1]
        if numUser == '1':
            pastDebate.user1votes = pastDebate.user1votes + 1
            pastDebate.save()
        else:
            pastDebate.user2votes = pastDebate.user2votes + 1
            pastDebate.save()
        return HttpResponseRedirect("/discourz/debate")
    else:
        pastDebate = []
        otherUsername = ''
        topic = ''
        user1 = ''
        user2 = ''
        user1votes = 0
        user2votes = 0
        category = ''
        try:
            pastDebate = PastDebates.objects.get(id=uuid)
            topic = pastDebate.topic
            user1 = pastDebate.user1
            user2 = pastDebate.user2
            user1votes = pastDebate.user1votes
            user2votes = pastDebate.user2votes
            tags = pastDebate.get_tag_list()
        except PastDebates.DoesNotExist:
            raise Http404('Topic does not exist')

        usernames = []
        messages = []

        commentList = Comment.getDebateComments(pastDebate)

        context = {
            'topic': topic,
            'user1': user1,
            'user2': user2,
            'user1votes': user1votes,
            'user2votes': user2votes,
            'tags': tags,
            'uuid': uuid,
            'firebaseKey': pastDebate.firebaseKey,
            'CommentList': commentList,
            'form': CommentForm(),
        }

        if request.method == 'POST':
            form = CommentForm(request.POST)
            if form.is_valid():
                comment = form.cleaned_data['text']
                PollId = form.cleaned_data['PollId']
                Debate = (PastDebates.objects.filter(id=uuid))[0]
                newComment = Comment(text=comment,
                                     debate=Debate,
                                     user=request.user)
                newComment.save()
                return redirect('pastChat', uuid)

        return render(request, 'pastChatTemplate.html', context=context)