Example #1
0
def topics_discuss(request, ring_id, slug):
    ring = get_object_or_404(Ring.objects.all(), pk=ring_id)
    punch = Punch(ring = ring)
    punches = ring.punch_set.order_by('datetime')    
    is_vote_only = False
    template_title = _(u'View & Vote') 
    punch_form = None    
    is_continue = False
    if ring.red.filter(pk=request.user.pk).exists() or ring.blue.filter(pk=request.user.pk).exists():
        is_continue = True
    if ring.rule == 'public':
        template_title = _(u'Open Topic')
    elif is_continue:        
        template_title = _(u'Continue Discussion')                
    else:
        is_vote_only = True
    
    if request.method == 'POST':  
        punch_form = PunchForm(request.POST)
        if punch_form.is_valid():
            punch = punch_form.save(commit=False)  
            if ring.red.filter(pk=request.user.pk).exists():
                punch.side = 'red'
            elif ring.blue.filter(pk=request.user.pk).exists():
                punch.side = 'blue'          
            save_punch(request, ring, punch)
            request.user.score = get_score_for_user(request.user)
            request.user.save()            
            internal_link = reverse('discuss-topic', kwargs={'ring_id':str(punch.ring.pk), 'slug':punch.ring.slug}) 
            link = '%s%s' % (settings.SITE_HOST, internal_link)
            for punch in ring.punch_set.all():
                if punch.speaker != request.user: 
                    email_user({'username': punch.speaker, 'link':link, 'first_name':punch.speaker.first_name,
                         'topic':punch.ring.topic}, 'registration/response.txt', _(u'Someone has responded to your opinion on Duelify!'), [punch.speaker.email])
            return HttpResponseRedirect(internal_link)
    else:
        #Is the user allowed to contribute to this topic?
        if not is_vote_only:            
            if ring.red.filter(pk=request.user.pk).exists():
                punch.side = 'red'
            elif ring.blue.filter(pk=request.user.pk).exists():
                punch.side = 'blue'
            punch_form = PunchForm(instance=punch)
    
    is_without_opponent = False
    winner_sofar = _(u'Winner so far in this discussion:')    
    winner_color = ''
    if ring.red.filter(pk=request.user.pk).exists() or ring.blue.filter(pk=request.user.pk).exists():
        if ring.red.count() == 0 or ring.blue.count() == 0: 
            winner = _(u'You have started this discussion and no one is opposing your view yet.')
            winner_sofar = ''
            is_without_opponent = True
    else:
        if ring.rule == 'public':
            winner = _(u'No one is opposing this view yet. Can you do it?')
            winner_sofar = ''
            is_without_opponent = True
        else:
            winner = _(u'This is a personal duel between two authors. Please vote for who is right.')
            winner_sofar = ''
            is_without_opponent = True
    
    red_votes = blue_votes = 0 
    for punch in ring.punch_set.all():
        if punch.side == 'red':            
            red_votes = red_votes + punch.get_votes()
        else:            
            blue_votes = blue_votes + punch.get_votes()
    if blue_votes > red_votes:
        winner_color = 'blue'
        if ring.rule == 'public':
            winner = _(u'Blue side is winning!')
        else:
            winner = ring.blue.all()[:1].get().get_full_name()
    elif blue_votes < red_votes:
        winner_color = 'red'
        if ring.rule == 'public':
            winner = _(u'Red side is winning!')
        else:
            winner = ring.red.all()[:1].get().get_full_name()
    elif blue_votes == red_votes and not is_without_opponent:
        winner_color = ''
        winner = _(u'No winner can yet be concluded. The race is on.')
        winner_sofar = '' 
    rings=None
    if not request.user.is_anonymous():
        rings = Ring.objects.filter(Q(red=request.user)|Q(blue=request.user))    
    variables = {'punch_form':punch_form, 'template_title': template_title, 'ring':ring, 'punches':punches, 'winner_sofar':winner_sofar, 
                 'winner':winner, 'winner_color':winner_color, 'is_continue':is_continue, 'rings':rings}
    return render(request, 'discuss_topic.html', variables)