コード例 #1
0
def discussion_add_edit(request, ring_id=None, slug=None):       
    if ring_id is None:
        ring = Ring(category = Category.objects.get(pk=1), datetime=timezone.now())
        punch = Punch(ring=ring, side='blue')
        template_title = _(u'Start a new topic')
    else:
        ring = get_object_or_404(Ring.objects.all(), pk=ring_id)
        punch = Punch(ring=ring)
        template_title = _(u'Edit existing topic')        
    if request.method == 'POST':        
        ring_form = RingForm(request.POST)        
        punch_form = PunchForm(request.POST)
        if ring_form.is_valid() and punch_form.is_valid():
            ring = ring_form.save(commit=False)
            punch = punch_form.save(commit=False)
            
            datetime = timezone.now()
            ring.datetime = datetime
            
            ring.save()
            if punch.side == 'red':
                ring.red.add(request.user)                   
            else:
                ring.blue.add(request.user)                
            ring.save()      
            if punch.id is None:                  
                punch.side='blue'
            punch.ring = ring
            punch.speaker = request.user 
            punch.datetime = datetime            
            punch.save()
                        
            request.user.score = get_score_for_user(request.user)
            request.user.save()                       
            if ring_form.cleaned_data['blue_invite']:
                invitation = DuelInvitation(                                    
                                        email=ring_form.cleaned_data['blue_invite'],
                                        code=get_user_model().objects.make_random_password(20),
                                        sender=request.user,
                                        ring=ring
                                        )
                invitation.save()
                try:
                    invitation.send()                
                    messages.warning(request, _(u'An invitation was sent to %(email)s.') % {'email' : invitation.email})
                except Exception:                
                    messages.error(request, _(u'An error happened when sending the invitation.'))    
            #log_contact(request, contact, 'contact_add_edit', secondary, profile, is_edit)
            else:
                messages.warning(request, _(u'Your open topic has been started. Lets wait and see if someone bites...'))            
            return HttpResponseRedirect(reverse('discuss-topic', kwargs={'ring_id':str(ring.pk), 'slug':ring.slug}))
    else:
        ring_form = RingForm(instance=ring)
        punch_form = PunchForm(instance=punch, is_first=True)
    rings = Ring.objects.filter(Q(red=request.user)|Q(blue=request.user))
    variables = {'ring_form':ring_form, 'punch_form':punch_form, 'template_title': template_title, 'rings':rings }
    return render(request, 'discussion.html', variables)
コード例 #2
0
def punch_edit(request, punch_id):
    punch = Punch.objects.get(pk=punch_id)
    if punch.speaker != request.user:
        return HttpResponseForbidden()
    ring = punch.ring
    template_title = _(u'Edit your opinion')
    if request.method == 'POST':
        punch_form = PunchForm(request.POST, instance=punch)
        if punch_form.is_valid():
            punch_updated = punch_form.save(commit=False)
            save_punch(request, ring, punch_updated)
            return HttpResponseRedirect(reverse('discuss-topic', kwargs={'ring_id':str(ring.pk), 'slug':ring.slug}))
    else:
        if punch.ring.punch_set.all()[:1].get() == punch:
            punch_form = PunchForm(instance=punch, is_first=True)
        else:            
            punch_form = PunchForm(instance=punch)
    variables = {'punch_form':punch_form, 'template_title': template_title, 'reverse':reverse('discuss-topic', kwargs={'ring_id':str(ring.pk), 'slug':ring.slug})}
    return render(request, 'punch.html', variables)
コード例 #3
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)