def update(self, request, *args, **kwargs): self.object = self.get_object_or_none() season_id = request.DATA.get('season', None) team_id = request.DATA.get('team', None) season = None team = None if season_id: try: season = common.Season.objects.get(id=season_id) common.add_player_to_season(player=self.object, season=season) except common.PermissionsException as e: return Response({'error': e.message}, status.HTTP_500_INTERNAL_SERVER_ERROR) except common.Season.DoesNotExist: return Response({}, status.HTTP_404_NOT_FOUND) if season_id and team_id: team = common.Team.objects.get(id=team_id) try: common.add_player_to_team(user=request.user, player=self.object, season=season, team=team) except common.PermissionsException as e: return Response({'error': e.message}, status.HTTP_500_INTERNAL_SERVER_ERROR) except common.Team.DoesNotExist: return Response({}, status.HTTP_404_NOT_FOUND) serializer = self.get_serializer(self.object) return Response(serializer.data)
def test_player_season_perm(self): # cannot add player to a team when they aren't in this season common.add_player_to_team( user=self.player2, player=self.player2, season=self.season_open, team=self.team, is_captain=True ) self.assertRaisesMessage( common.PermissionsException, common.PermissionsException.MSG_SEASON, common.add_player_to_team, user=self.player2, player=self.no_season_player, season=self.season_open, team=self.team, )
def add_players(ranked_players, pick_order): if not isinstance(ranked_players, list): ranked_players = ranked_players.iterator() # assuming queryset while True: for pick in pick_order: team = pick.team try: ranked_player = next(ranked_players) except StopIteration: return common.add_player_to_season(ranked_player.player, season=rankset.season) common.add_player_to_team( user=ranked_player.player, player=ranked_player.player, team=team, season=rankset.season ) if serpentine: pick_order.reverse()
def test_remove_player(self): self.assertFalse(self.player1.is_superuser()) self.assertFalse(self.player1.is_captain(self.team, self.season_open)) common.add_player_to_team(user=self.player1, player=self.player1, season=self.season_open, team=self.team) common.add_player_to_team( user=self.player2, player=self.player2, season=self.season_open, team=self.team, is_captain=True ) # cannot remove another if you are not a captain self.assertRaisesMessage( common.PermissionsException, common.PermissionsException.MSG_CAPTAIN_REMOVE, common.remove_player_from_team, user=self.player1, player=self.player2, season=self.season_open, team=self.team, ) self.assertEqual(2, len(common.find_players_on(self.team, self.season_open))) common.remove_player_from_team(user=self.player2, player=self.player1, season=self.season_open, team=self.team) self.assertEqual(1, len(common.find_players_on(self.team, self.season_open)))
def test_duplicate_player(self): # cannot add a player to a team twice common.add_player_to_team(user=self.player1, player=self.player1, season=self.season_open, team=self.team) self.assertEqual( 1, common.TeamPlayerSeason.objects.filter( player=self.player1, season=self.season_open, team=self.team ).count(), ) self.assertRaisesMessage( common.PermissionsException, common.PermissionsException.MSG_EXISTING, common.add_player_to_team, user=self.player1, player=self.player1, season=self.season_open, team=self.team, ) self.assertEqual( 1, common.TeamPlayerSeason.objects.filter( player=self.player1, season=self.season_open, team=self.team ).count(), )
def test_team_add_remove(self): common.add_player_to_team(user=self.player1, player=self.player1, season=self.season_open, team=self.team) self.assertEqual( 1, common.TeamPlayerSeason.objects.filter( player=self.player1, season=self.season_open, team=self.team ).count(), ) common.add_player_to_team( user=self.player2, player=self.player2, season=self.season_open, team=self.team, is_captain=True ) self.assertTrue(self.player2.is_captain(self.team, self.season_open)) self.assertEqual(2, len(common.find_players_on(self.team, self.season_open))) common.remove_player_from_team(user=self.player1, player=self.player1, season=self.season_open, team=self.team) self.assertEqual(1, len(common.find_players_on(self.team, self.season_open))) common.add_player_to_team(user=self.player1, player=self.player1, season=self.season_open, team=self.team) self.assertEqual(2, len(common.find_players_on(self.team, self.season_open))) common.remove_player_from_team(user=self.player2, player=self.player1, season=self.season_open, team=self.team) self.assertEqual(1, len(common.find_players_on(self.team, self.season_open)))