예제 #1
0
파일: views.py 프로젝트: tim528/pong-board
def home_page(request):
    """Render view for home page."""
    recent_matches = Match.get_recent_matches(num_matches=20)
    leaderboard = PlayerRating.objects.all().order_by('-rating')
    match_form = MatchForm()
    player_form = PlayerForm()
    if request.method == 'POST':
        if 'winner' in request.POST:  # only occurs for match submissions
            match_form = MatchForm(request.POST)
            if match_form.is_valid():
                match_form.save()
                return redirect('/')
        elif 'first_name' in request.POST:  # only occurs for player submissions
            player_form = PlayerForm(request.POST)
            if player_form.is_valid():
                player_form.save()
                return redirect('/')
    return render(request,
                  'home.html',
                  context={
                      'recent_matches': recent_matches,
                      'match_form': match_form,
                      'player_form': player_form,
                      'leaderboard': leaderboard
                  })
예제 #2
0
 def test_get_recent_match(self):
     """Test that a match is retrieved."""
     match = Match.objects.create(winner=self.player1,
                                  winning_score=21,
                                  loser=self.player2,
                                  losing_score=19)
     fetched_matches = Match.get_recent_matches(num_matches=1)
     self.assertEqual(fetched_matches[0], match)
예제 #3
0
 def test_num_matches(self):
     """Test that only the specified number of matches are returned."""
     for _ in range(10):
         Match.objects.create(winner=self.player1,
                              winning_score=21,
                              loser=self.player2,
                              losing_score=19)
     fetched_matches = Match.get_recent_matches(num_matches=5)
     self.assertEqual(len(fetched_matches), 5)
예제 #4
0
 def test_descending_order(self):
     """Test that matches are returned in descending order."""
     match1 = Match.objects.create(winner=self.player1,
                                   winning_score=21,
                                   loser=self.player2,
                                   losing_score=19)
     match2 = Match.objects.create(winner=self.player1,
                                   winning_score=21,
                                   loser=self.player2,
                                   losing_score=19)
     fetched_matches = Match.get_recent_matches(num_matches=2)
     self.assertEqual(fetched_matches[0], match2)
     self.assertEqual(fetched_matches[1], match1)