def post(self): user = users.get_current_user() account = globals.get_or_create_account(user) commissioner_account_key = account.key current_league = league_key( commissioner_account_key.get().league).get() #Get data from the post header name = self.request.get('name') snake = self.request.get('snake_draft') == 'on' if not current_league or current_league.draft_current_position == 0: if name != globals.draft_started_sentinel: #Create/Update the league new_league = League.get_or_insert( commissioner_account_key.id()) new_league.name = name new_league.snake_draft = snake new_league.draft_current_position = 0 new_league.put() add_to_league(user.user_id(), new_league.key.id()) self.redirect('/') else: globals.display_error_page( self, self.request.referer, error_messages.league_already_started_leaving)
def get(self, week_number): """ Update the active teams for the user and redirects them to /viewAlliance/ for the week number Expects a post parameter: 'action' to be one of the following: - bench: Takes a team off the active lineup - putin: Adds a team to the lineup - drop: Drops a team from the user's roster Expects a post parameter: 'team_number' to be the number of the team to perform this action on :parameter week_number: Taken from the url, in string form """ #The choice_key of the request action = self.request.get('action') team_number = self.request.get('team_number') user = users.get_current_user() #Current user's id, used to identify their data user_id = user.user_id() account = globals.get_or_create_account(user) league_id = account.league choice = choice_key(account_key(user_id), league_id).get() roster = [] for team in choice.current_team_roster: roster.append(int(team)) #Only allow changes to the lineup if the week is editable if is_week_editable(week_number): error = False active_lineup = lineup_key(choice_key(account.key, league_id), week_number).get() if action == "bench": active_lineup.active_teams.remove(int(team_number)) elif action == "putin": if len(active_lineup.active_teams) < maximum_active_teams: if int(team_number) in roster: active_lineup.active_teams.append(int(team_number)) else: error = True else: error = True globals.display_error_page( self, self.request.referer, error_messages.maximum_active_teams_reached) elif action == "drop": if not str(team_number) in get_top_teams( globals.number_of_locked_teams): choice = choice_key(account.key, league_id).get() choice.current_team_roster.remove(int(team_number)) if int(team_number) in active_lineup.active_teams: active_lineup.active_teams.remove(int(team_number)) choice.put() active_lineup.put() if not error: self.redirect(self.request.referer)
def get(self): user_id = users.get_current_user().user_id() if league_key(account_key( user_id).get().league).get().draft_current_position == 0: remove_from_league(user_id) self.redirect('/') else: globals.display_error_page( self, self.request.referer, error_messages.league_already_started_leaving)
def get(self, week_number): """ Update the active teams for the user and redirects them to /viewAlliance/ for the week number Expects a post parameter: 'action' to be one of the following: - bench: Takes a team off the active lineup - putin: Adds a team to the lineup - drop: Drops a team from the user's roster Expects a post parameter: 'team_number' to be the number of the team to perform this action on :parameter week_number: Taken from the url, in string form """ #The choice_key of the request action = self.request.get('action') team_number = self.request.get('team_number') user = users.get_current_user() #Current user's id, used to identify their data user_id = user.user_id() account = globals.get_or_create_account(user) league_id = account.league choice = choice_key(account_key(user_id), league_id).get() roster = [] for team in choice.current_team_roster: roster.append(int(team)) #Only allow changes to the lineup if the week is editable if is_week_editable(week_number): error = False active_lineup = lineup_key(choice_key(account.key, league_id), week_number).get() if action == "bench": active_lineup.active_teams.remove(int(team_number)) elif action == "putin": if len(active_lineup.active_teams) < maximum_active_teams: if int(team_number) in roster: active_lineup.active_teams.append(int(team_number)) else: error = True else: error = True globals.display_error_page(self, self.request.referer, error_messages.maximum_active_teams_reached) elif action == "drop": if not str(team_number) in get_top_teams(globals.number_of_locked_teams): choice = choice_key(account.key, league_id).get() choice.current_team_roster.remove(int(team_number)) if int(team_number) in active_lineup.active_teams: active_lineup.active_teams.remove(int(team_number)) choice.put() active_lineup.put() if not error: self.redirect(self.request.referer)
def get(self, page): """ Display a certain page of the free agent list :param page: The page to display The free agent list is a list of teams that have not been drafted by any player. They are sorted by the total points of each team Users have the option to pick up teams or "flag" them to get updates about them """ # Checks for active Google account session user = users.get_current_user() logout_url = users.create_logout_url('/') #Display update text for the status of the last choice update update_text = self.request.get('updated') account = globals.get_or_create_account(user) league_id = account.league if league_id != '0': if not page: page = 1 else: page = int(page) if league_key(league_id).get().draft_current_position == 0: league_name = league_key(league_id).get().name else: league_name = globals.draft_started_sentinel free_agent_list = get_free_agent_list(league_id, page) #Send html data to browser template_values = { 'user': user.nickname(), 'logout_url': logout_url, 'league_name': league_name, 'update_text': update_text, 'free_agent_list': free_agent_list, 'page': page, 'max_page': get_max_free_agent_pages(league_id), } template = JINJA_ENVIRONMENT.get_template('templates/falist.html') self.response.write(template.render(template_values)) else: globals.display_error_page( self, self.request.referer, error_messages.need_to_be_a_member_of_a_league)
def get(self, page): """ Display a certain page of the free agent list :param page: The page to display The free agent list is a list of teams that have not been drafted by any player. They are sorted by the total points of each team Users have the option to pick up teams or "flag" them to get updates about them """ # Checks for active Google account session user = users.get_current_user() logout_url = users.create_logout_url('/') #Display update text for the status of the last choice update update_text = self.request.get('updated') account = globals.get_or_create_account(user) league_id = account.league if league_id != '0': if not page: page = 1 else: page = int(page) if league_key(league_id).get().draft_current_position == 0: league_name = league_key(league_id).get().name else: league_name = globals.draft_started_sentinel free_agent_list = get_free_agent_list(league_id, page) #Send html data to browser template_values = { 'user': user.nickname(), 'logout_url': logout_url, 'league_name': league_name, 'update_text': update_text, 'free_agent_list': free_agent_list, 'page': page, 'max_page': get_max_free_agent_pages(league_id), } template = JINJA_ENVIRONMENT.get_template('templates/falist.html') self.response.write(template.render(template_values)) else: globals.display_error_page(self, self.request.referer, error_messages.need_to_be_a_member_of_a_league)
def get(self, league_id): """ Adds a user to the specified league :param league_id: Collected from url, the league to join """ user_id = users.get_current_user().user_id() current_league = league_key(account_key(user_id).get().league).get() if not current_league or current_league.draft_current_position == 0: if league_key(league_id).get().draft_current_position == 0: remove_from_league(user_id) #Remove from old league add_to_league(user_id, league_id) #Add to new one self.redirect('/') else: globals.display_error_page( self, self.request.referer, error_messages.league_already_started) else: globals.display_error_page( self, self.request.referer, error_messages.league_already_started_leaving)
def get(self): """ When visited by the league commissioner, the draft is started The commissioner is then redirected to the draft page """ # Checks for active Google account session user = users.get_current_user() #Current user's id, used to identify their data user_id = user.user_id() account = globals.get_or_create_account(user) league_id = account.league league = league_key(league_id).get() league_comissioner = league.key.id() if league_comissioner == user_id: league_player_query = Account.query(Account.league == league_id) league_players = league_player_query.fetch() if len(league_players) > 1: if league.draft_current_position == 0: start_draft(league_id) setup_for_next_pick(league_id) self.redirect('/draft/') else: globals.display_error_page(self, self.request.referer, error_messages.draft_already_completed) else: globals.display_error_page(self, self.request.referer, error_messages.league_too_small) else: globals.display_error_page(self, self.request.referer,error_messages.access_denied)
def get(self): """ When visited by the league commissioner, the draft is started The commissioner is then redirected to the draft page """ # Checks for active Google account session user = users.get_current_user() #Current user's id, used to identify their data user_id = user.user_id() account = globals.get_or_create_account(user) league_id = account.league league = league_key(league_id).get() league_comissioner = league.key.id() if league_comissioner == user_id: league_player_query = Account.query(Account.league == league_id) league_players = league_player_query.fetch() if len(league_players) > 1: if league.draft_current_position == 0: start_draft(league_id) setup_for_next_pick(league_id) self.redirect('/draft/') else: globals.display_error_page( self, self.request.referer, error_messages.draft_already_completed) else: globals.display_error_page(self, self.request.referer, error_messages.league_too_small) else: globals.display_error_page(self, self.request.referer, error_messages.access_denied)
def get(self): """ The draft page contains the draft board, a timer, and a map; all that is necessary for the draft process """ # Checks for active Google account session user = users.get_current_user() logout_url = users.create_logout_url('/') account = globals.get_or_create_account(user) league_id = account.league if league_id != '0': #Make check to see if the time for the current pick has expired current_time = datetime.datetime.utcnow() current_timeout = league_key(league_id).get().draft_current_timeout draft_pick = draft_pick_key(league_key(league_id), league_key(league_id).get().draft_current_position).get() if current_timeout: if current_time > current_timeout: # The time has expired draft_pick.team = 0 # Set the pick to indicate it was forefited draft_pick.put() setup_for_next_pick(league_id) # Move the pick along to the next person #Display update text for the status of the last choice update update_text = self.request.get('updated') if self.request.get('updated') == "Good": update_text = "Team added successfully" league_player_query = Account.query(Account.league == league_id) players_for_the_sake_of_number = league_player_query.fetch() league_players = [] if draft_pick_key(league_key(league_id), 1).get(): # != None for i in range(1, len(players_for_the_sake_of_number) + 1): pick = draft_pick_key(league_key(league_id), i).get() league_players.append(ndb.Key(urlsafe=pick.player).get()) else: league_players = players_for_the_sake_of_number draft_board = [] player_list = [] for player in league_players: player_list.append(player.nickname) number_of_picks = len(league_players) * globals.draft_rounds for position in range(1, number_of_picks + 1): pick_query = DraftPick.query().filter(DraftPick.display_number == position) query_results = pick_query.fetch(1) pick = DraftPick() if len(query_results) != 0: pick = query_results[0] username = (((position % len(league_players)) - 1) % len(league_players)) draft_round = int((position - 1) / len(league_players)) if username == 0: draft_board.append([]) for i in range(0, len(league_players)): draft_board[draft_round].append('-') if pick and pick.team != None: draft_board[draft_round][username] = str(pick.team) if pick.team == 0: draft_board[draft_round][username] = "<i>Forfeited</i>" else: draft_board[draft_round][username] = "<i>TBD</i>" if league_id != '0': if league_key(league_id).get().draft_current_position == 0: league_name = league_key(league_id).get().name else: league_name = globals.draft_started_sentinel else: league_name = "" users_turn = False picking_user = "" draft_pick = draft_pick_key(league_key(league_id), league_key(league_id).get().draft_current_position).get() if draft_pick: users_turn = (draft_pick.player == account.key.urlsafe()) picking_user = ndb.Key(urlsafe=draft_pick.player).get().nickname current_unix_timeout = None if current_timeout: current_unix_timeout = calendar.timegm(current_timeout.timetuple()) current_position = league_key(league_id).get().draft_current_position if current_position == 0: draft_status = "Pre" elif current_position == -1: draft_status = "Post" else: draft_status = "Mid" team_map_data = get_lat_lng_json(league_id) #Send html data to browser template_values = { 'user': user.nickname(), 'logout_url': logout_url, 'draft_board': draft_board, 'player_list': player_list, 'update_text': update_text, 'league_name': league_name, 'users_turn': users_turn, 'picking_user': picking_user, 'current_unix_timeout': current_unix_timeout, 'draft_status': draft_status, 'team_map_data': team_map_data, } template = JINJA_ENVIRONMENT.get_template('templates/draft_main.html') self.response.write(template.render(template_values)) else: globals.display_error_page(self, self.request.referer,error_messages.need_to_be_a_member_of_a_league)
def get(self): """ The draft page contains the draft board, a timer, and a map; all that is necessary for the draft process """ # Checks for active Google account session user = users.get_current_user() logout_url = users.create_logout_url('/') account = globals.get_or_create_account(user) league_id = account.league if league_id != '0': #Make check to see if the time for the current pick has expired current_time = datetime.datetime.utcnow() current_timeout = league_key(league_id).get().draft_current_timeout draft_pick = draft_pick_key( league_key(league_id), league_key(league_id).get().draft_current_position).get() if current_timeout: if current_time > current_timeout: # The time has expired draft_pick.team = 0 # Set the pick to indicate it was forefited draft_pick.put() setup_for_next_pick( league_id) # Move the pick along to the next person #Display update text for the status of the last choice update update_text = self.request.get('updated') if self.request.get('updated') == "Good": update_text = "Team added successfully" league_player_query = Account.query(Account.league == league_id) players_for_the_sake_of_number = league_player_query.fetch() league_players = [] if draft_pick_key(league_key(league_id), 1).get(): # != None for i in range(1, len(players_for_the_sake_of_number) + 1): pick = draft_pick_key(league_key(league_id), i).get() league_players.append(ndb.Key(urlsafe=pick.player).get()) else: league_players = players_for_the_sake_of_number draft_board = [] player_list = [] for player in league_players: player_list.append(player.nickname) number_of_picks = len(league_players) * globals.draft_rounds for position in range(1, number_of_picks + 1): pick_query = DraftPick.query().filter( DraftPick.display_number == position) query_results = pick_query.fetch(1) pick = DraftPick() if len(query_results) != 0: pick = query_results[0] username = (((position % len(league_players)) - 1) % len(league_players)) draft_round = int((position - 1) / len(league_players)) if username == 0: draft_board.append([]) for i in range(0, len(league_players)): draft_board[draft_round].append('-') if pick and pick.team != None: draft_board[draft_round][username] = str(pick.team) if pick.team == 0: draft_board[draft_round][username] = "<i>Forfeited</i>" else: draft_board[draft_round][username] = "<i>TBD</i>" if league_id != '0': if league_key(league_id).get().draft_current_position == 0: league_name = league_key(league_id).get().name else: league_name = globals.draft_started_sentinel else: league_name = "" users_turn = False picking_user = "" draft_pick = draft_pick_key( league_key(league_id), league_key(league_id).get().draft_current_position).get() if draft_pick: users_turn = (draft_pick.player == account.key.urlsafe()) picking_user = ndb.Key( urlsafe=draft_pick.player).get().nickname current_unix_timeout = None if current_timeout: current_unix_timeout = calendar.timegm( current_timeout.timetuple()) current_position = league_key( league_id).get().draft_current_position if current_position == 0: draft_status = "Pre" elif current_position == -1: draft_status = "Post" else: draft_status = "Mid" team_map_data = get_lat_lng_json(league_id) #Send html data to browser template_values = { 'user': user.nickname(), 'logout_url': logout_url, 'draft_board': draft_board, 'player_list': player_list, 'update_text': update_text, 'league_name': league_name, 'users_turn': users_turn, 'picking_user': picking_user, 'current_unix_timeout': current_unix_timeout, 'draft_status': draft_status, 'team_map_data': team_map_data, } template = JINJA_ENVIRONMENT.get_template( 'templates/draft_main.html') self.response.write(template.render(template_values)) else: globals.display_error_page( self, self.request.referer, error_messages.need_to_be_a_member_of_a_league)
def get(self, week_number): """ Handle the requests to see data for all alliances. Displays a past_alliance or alliance_management tab appropriately. :parameter week_number: Week number taken from the url, string form For each team on the bench and active lineup: displays information about each team's past performance Also displays opponent's active and bench lineup """ # Checks for active Google account session user = users.get_current_user() #Current user's id, used to identify their data user_id = user.user_id() logout_url = users.create_logout_url('/') account = globals.get_or_create_account(user) league_id = account.league draft_over = league_key(league_id).get().draft_current_position == -1 if league_id != '0': if league_key(league_id).get().draft_current_position == 0: league_name = league_key(league_id).get().name else: league_name = globals.draft_started_sentinel else: league_name = "" if draft_over: team_lists = get_team_lists(user_id, week_number) point_totals = [] for team_list in team_lists: point_total = 0 for team in team_list: point_total += team['total_points'] point_totals.append(point_total) opponent_name = "" opponent_point_totals = [] opponent_team_lists = [] team_listss = [team_lists] if get_opponent(user_id, week_number) != globals.schedule_bye_week: opponent_team_lists = get_team_lists(get_opponent(user_id, week_number), week_number) opponent_point_totals = [] for team_list in team_lists: opponent_point_total = 0 for team in team_list: opponent_point_total += team['total_points'] opponent_point_totals.append(opponent_point_total) opponent_name = get_opponent_name(user_id, week_number) team_listss.append(opponent_team_lists) #Send html data to browser template_values = { 'user': user.nickname(), 'logout_url': logout_url, 'league_name': league_name, 'week_number': int(week_number), 'point_totals': [point_totals, opponent_point_totals], 'team_listss': team_listss, 'opponent_name': opponent_name, } if is_week_editable(week_number): template = JINJA_ENVIRONMENT.get_template('templates/alliance_management.html') else: template = JINJA_ENVIRONMENT.get_template('templates/past_alliances.html') self.response.write(template.render(template_values)) else: globals.display_error_page(self, self.request.referer, error_messages.draft_needs_to_be_completed)
def get(self): """ The main dashboard for league + personal info Contains information on the following: - The league schedule, including bye weeks and who plays who - The leader board, showing bench points and league points for each player, ranked - The current user's lineup for each week, including the points scored for past weeks - The current user's current roster """ # Checks for active Google account session user = users.get_current_user() #Current user's id, used to identify their data user_id = user.user_id() logout_url = users.create_logout_url('/') #Make global call to get user information account = globals.get_or_create_account(user) league_id = account.league if league_id != '0': draft_over = league_key(league_id).get().draft_current_position == -1 #Only allow access to this page after the draft has completed if draft_over: #Proccess league info if league_id != '0': if league_key(league_id).get().draft_current_position == 0: league_name = league_key(league_id).get().name else: league_name = globals.draft_started_sentinel else: league_name = "" total_points = 0 week_table = [] for weeknum in range(1, globals.number_of_official_weeks + 1): teams = get_team_lists(user_id, weeknum)[0] points = 0 lineup = [] for team in teams: event_key = get_team_schedule(int(team['number']))[int(weeknum) - 1]['event_key']#-1 to convert to 0-based index if event_key: #Check if the team is competing that week points += get_team_points_at_event(team['number'], event_key) lineup.append(team['number']) if is_week_editable(weeknum): points = "<i>No Data</i>" else: total_points += points week_row = {'week': str(weeknum), 'active_lineup': lineup, 'points': points} week_table.append(week_row) leader_board = get_leader_board(league_id) league_schedule = get_readable_schedule(league_id) current_roster = get_current_roster(user_id) user_schedule = get_readable_user_schedule(user_id) template_values = { 'user': user.nickname(), 'logout_url': logout_url, 'league_name': league_name, 'week_table': week_table, 'total_points': total_points, 'leader_board': leader_board, 'schedule': league_schedule, 'roster': current_roster, 'week_number': globals.debug_current_editable_week, 'user_schedule': user_schedule } template = JINJA_ENVIRONMENT.get_template('templates/alliance_management_portal.html') self.response.write(template.render(template_values)) else: globals.display_error_page(self, self.request.referer, error_messages.draft_needs_to_be_completed) else: globals.display_error_page(self, self.request.referer, error_messages.need_to_be_a_member_of_a_league)
def get(self, week_number): """ Handle the requests to see data for all alliances. Displays a past_alliance or alliance_management tab appropriately. :parameter week_number: Week number taken from the url, string form For each team on the bench and active lineup: displays information about each team's past performance Also displays opponent's active and bench lineup """ # Checks for active Google account session user = users.get_current_user() #Current user's id, used to identify their data user_id = user.user_id() logout_url = users.create_logout_url('/') account = globals.get_or_create_account(user) league_id = account.league draft_over = league_key(league_id).get().draft_current_position == -1 if league_id != '0': if league_key(league_id).get().draft_current_position == 0: league_name = league_key(league_id).get().name else: league_name = globals.draft_started_sentinel else: league_name = "" if draft_over: team_lists = get_team_lists(user_id, week_number) point_totals = [] for team_list in team_lists: point_total = 0 for team in team_list: point_total += team['total_points'] point_totals.append(point_total) opponent_name = "" opponent_point_totals = [] opponent_team_lists = [] team_listss = [team_lists] if get_opponent(user_id, week_number) != globals.schedule_bye_week: opponent_team_lists = get_team_lists( get_opponent(user_id, week_number), week_number) opponent_point_totals = [] for team_list in team_lists: opponent_point_total = 0 for team in team_list: opponent_point_total += team['total_points'] opponent_point_totals.append(opponent_point_total) opponent_name = get_opponent_name(user_id, week_number) team_listss.append(opponent_team_lists) #Send html data to browser template_values = { 'user': user.nickname(), 'logout_url': logout_url, 'league_name': league_name, 'week_number': int(week_number), 'point_totals': [point_totals, opponent_point_totals], 'team_listss': team_listss, 'opponent_name': opponent_name, } if is_week_editable(week_number): template = JINJA_ENVIRONMENT.get_template( 'templates/alliance_management.html') else: template = JINJA_ENVIRONMENT.get_template( 'templates/past_alliances.html') self.response.write(template.render(template_values)) else: globals.display_error_page( self, self.request.referer, error_messages.draft_needs_to_be_completed)
def get(self): """ The main dashboard for league + personal info Contains information on the following: - The league schedule, including bye weeks and who plays who - The leader board, showing bench points and league points for each player, ranked - The current user's lineup for each week, including the points scored for past weeks - The current user's current roster """ # Checks for active Google account session user = users.get_current_user() #Current user's id, used to identify their data user_id = user.user_id() logout_url = users.create_logout_url('/') #Make global call to get user information account = globals.get_or_create_account(user) league_id = account.league if league_id != '0': draft_over = league_key( league_id).get().draft_current_position == -1 #Only allow access to this page after the draft has completed if draft_over: #Proccess league info if league_id != '0': if league_key(league_id).get().draft_current_position == 0: league_name = league_key(league_id).get().name else: league_name = globals.draft_started_sentinel else: league_name = "" total_points = 0 week_table = [] for weeknum in range(1, globals.number_of_official_weeks + 1): teams = get_team_lists(user_id, weeknum)[0] points = 0 lineup = [] for team in teams: event_key = get_team_schedule(int( team['number']))[int(weeknum) - 1][ 'event_key'] #-1 to convert to 0-based index if event_key: #Check if the team is competing that week points += get_team_points_at_event( team['number'], event_key) lineup.append(team['number']) if is_week_editable(weeknum): points = "<i>No Data</i>" else: total_points += points week_row = { 'week': str(weeknum), 'active_lineup': lineup, 'points': points } week_table.append(week_row) leader_board = get_leader_board(league_id) league_schedule = get_readable_schedule(league_id) current_roster = get_current_roster(user_id) user_schedule = get_readable_user_schedule(user_id) template_values = { 'user': user.nickname(), 'logout_url': logout_url, 'league_name': league_name, 'week_table': week_table, 'total_points': total_points, 'leader_board': leader_board, 'schedule': league_schedule, 'roster': current_roster, 'week_number': globals.debug_current_editable_week, 'user_schedule': user_schedule } template = JINJA_ENVIRONMENT.get_template( 'templates/alliance_management_portal.html') self.response.write(template.render(template_values)) else: globals.display_error_page( self, self.request.referer, error_messages.draft_needs_to_be_completed) else: globals.display_error_page( self, self.request.referer, error_messages.need_to_be_a_member_of_a_league)