Beispiel #1
0
def create_matchup(request, champion_id):
    champion_one = get_object_or_404(Champion, pk=champion_id)
    try:
        champion_two = get_object_or_404(Champion, pk=request.POST['champion_two'])
    except (KeyError, Champion.DoesNotExist):
        champion_list = Champion.objects.exclude(id=champion_one.id).order_by('name')
        return render_to_response('champions/create_matchup.html', {'champion_list': champion_list, 'champion_one': champion_one}, context_instance=RequestContext(request))
    else:
        if champion_one.id == champion_two.id:
            return HttpResponseRedirect(reverse('champions.views.detail', args=(champion_one.id,)))
        try:
            matchup = Matchup.objects.get(champ_one=champion_one, champ_two=champion_two)
            v = Vote(champion=champion_one, matchup=matchup)
            v.save()
        except ObjectDoesNotExist:
            try:
                matchup = Matchup.objects.get(champ_one=champion_two, champ_two=champion_one)
                v = Vote(champion=champion_one, matchup=matchup)
                v.save()
            except ObjectDoesNotExist:
                matchup = Matchup(champ_one=champion_one, champ_two=champion_two)
                matchup.save()
                v = Vote(champion=champion_one, matchup=matchup)
                v.save()
        return HttpResponseRedirect(reverse('champions.views.detail', args=(champion_one.id,)))
Beispiel #2
0
def vote_matchup(request, matchup_id):
    m = get_object_or_404(Matchup, pk=matchup_id)
    try:
        champion = get_object_or_404(Champion, pk=request.POST['choice'])
    except (KeyError, Champion.DoesNotExist):
        # Redisplay the poll voting form.
        return render_to_response('champions/matchup_detail.html', {
            'matchup': m,
            'champ_one_votes': Vote.objects.filter(matchup=m, champion=m.champ_one).count(), 
            'champ_two_votes': Vote.objects.filter(matchup=m, champion=m.champ_two).count(),
            'error_message': "You didn't select a choice.",
        }, context_instance=RequestContext(request))
    else:
        vote = Vote(champion=champion, matchup=m)
        vote.save()
        # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button.
        return HttpResponseRedirect(reverse('champions.views.detail', args=(champion.id,)))