def start_tournament(tournament_id): """ Starts a tournament :param tournament_id: Id of a tournament to start :return: True if success, False otherwise """ try: the_tournament = Tournament.objects.get(id=tournament_id) t_users = [x.user for x in Registration.objects.filter(tournament=the_tournament)] except ObjectDoesNotExist: return False if len(t_users) < the_tournament.max_participants: ## Add free slots pass for i in range(the_tournament.max_participants - 1): m = Match() m.tournament = the_tournament m.code = i if i < the_tournament.max_participants / 2: m.left_user = t_users[i * 2] m.right_user = t_users[i * 2 + 1] m.save() # Let the game begin! the_tournament.has_started = True the_tournament.save() for reg in Registration.objects.filter(tournament=the_tournament): reg.delete() return True