def roster(): players = { 'captain': current_roster('captain'), 'assistant': current_roster('assistant'), 'player': current_roster('player'), 'sub': current_roster('sub'), 'retired': current_roster('retired') } return render_template('roster.html', players=players)
def leaderboard(year_str): try: board = request.args['board'] except: board = None pass roster = current_roster('full') if year_str.lower() == 'all time': stats = [ sum( PlayerSeasonStats.query.join(Player).filter( Player.nickname == p.nickname).all()) for p in roster ] else: if year_str == 'current': year_str = current_season().season_name elif year_str == 'last': year_str = current_season(last=1).season_name else: year_str = year_str.replace( '-', '/') # This is to allow date name to be 'url-friendly' stats = PlayerSeasonStats.query.join(Season).filter_by(season_name=year_str).\ join(Player).filter(Player.nickname.in_([p.nickname for p in roster])).all() return render_template('leaderboard.html', roster=roster, stats=stats, year_str=year_str, board=board)
def vote(): period = current_app.config['VOTE_PERIOD'] closed = date.today() - timedelta(period) match = Match.query.filter(Match.date <= date.today(), Match.date >= closed).first() if match: if not match.poll: match.create_poll() roster = { 'active': current_roster('active'), 'inactive': current_roster('sub') } return render_template('vote.html', roster=roster, match=match)
def send_summary_email(token): user, payload = User.verify_user_token(token, task='send_summary_email') if not user: flash('Invalid token', 'danger') return redirect(url_for('main.index')) if not 'match' in payload: flash('Invalid token payload', 'danger') return redirect(url_for('main.index')) match_id = payload['match'] match = Match.query.filter_by(id=match_id).first() hs = match.high_scores.with_entities(HighScore.player_id, func.count(HighScore.player_id))\ .group_by(HighScore.player_id)\ .order_by(func.count(HighScore.player_id).desc()).all() t_hs = [(Player.query.filter_by(id=t[0]).first(), t[1]) for t in hs if t[1] == hs[0][1]] ls = match.low_scores.with_entities(LowScore.player_id, func.count(LowScore.player_id))\ .group_by(LowScore.player_id)\ .order_by(func.count(LowScore.player_id).desc()).all() t_ls = [(Player.query.filter_by(id=t[0]).first(), t[1]) for t in ls if t[1] == ls[0][1]] ts = sorted([(p,sum([pg.stars for pg in PlayerGame.query.filter_by(player=p)\ .join(Game).join(Match).filter(Match.id==match.id).all()])) for p in match.get_roster()], key = lambda x:x[1], reverse = True) t_ts = [t for t in ts if t[1] == ts[0][1]] performers = {'stars': t_ts, 'hs': t_hs, 'ls': t_ls} users = [p.user for p in current_roster('active') if p.user is not None] if current_app.config['RENDER_EMAIL']: return render_template('email/event_summary.html', match=match, performers=performers) summary_email(users=users, match=match, performers=performers) match.summary_email_sent = date.today() db.session.add(match) db.session.commit() flash('Summary email sent!') return redirect(url_for('main.enter_score', id=match.id))
def send_reminder_email(match_id, token): user, _ = User.verify_user_token(token, task='send_reminder_email') if not user: return redirect(url_for('main.index')) match = Match.query.filter_by(id=match_id).first() users = [p.user for p in current_roster('active') if p.user is not None] status = [ u.player.checked_matches_association.filter_by( match_id=match.id).first().status for u in users ] reminder_email(users=users, match=match, status=status) match.reminder_email_sent = date.today() db.session.add(match) db.session.commit() flash('Reminder email sent!') return redirect(url_for('main.captain', _anchor='checkin'))
def index(): all_players = current_roster('active') page = request.args.get('page', 1, type=int) last_match = Match.query.filter(Match.date < date.today()).order_by( Match.date.desc()).first() schedule = Match.query.filter(Match.date >= date.today()).order_by( Match.date).paginate(page, current_app.config['MATCH_PER_PAGE'], False) next_url = url_for('main.index', page=schedule.next_num) \ if schedule.has_next else None prev_url = url_for('main.index', page=schedule.prev_num) \ if schedule.has_prev else None news = News.query.order_by(News.timestamp.desc()).limit(4).all() leader_board_list = [ LeaderBoardCard('Stars',PlayerSeasonStats.query.join(Player).\ filter(PlayerSeasonStats.season==current_season()).\ with_entities(Player.nickname,PlayerSeasonStats.total_stars).\ order_by(PlayerSeasonStats.total_stars.desc()).limit(4).all()), LeaderBoardCard('High Scores',PlayerSeasonStats.query.join(Player).\ filter(PlayerSeasonStats.season==current_season()).\ with_entities(Player.nickname,PlayerSeasonStats.total_high_scores).\ order_by(PlayerSeasonStats.total_high_scores.desc()).limit(4).all()), LeaderBoardCard('Low Scores',PlayerSeasonStats.query.join(Player).\ filter(PlayerSeasonStats.season==current_season()).\ with_entities(Player.nickname,PlayerSeasonStats.total_low_scores).\ order_by(PlayerSeasonStats.total_low_scores.desc()).limit(4).all()) ] return render_template('index.html', title=None, schedule=schedule.items, next_url=next_url, prev_url=prev_url, all_players=all_players, last_match=last_match, leader_board_list=leader_board_list, news=news)
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) roster = current_roster('ordered') player_choices = [(p.nickname, p.nickname) for p in roster] self.player.choices = player_choices
def player_edit(nickname): player = Player.query.filter_by(nickname=nickname).first() form = EditPlayerForm(nickname) all_players = current_roster('full') if form.submit_new.data and form.validate(): newplayer = Player(first_name=form.first_name.data, last_name=form.last_name.data, nickname=form.nickname.data, tagline=form.tagline.data) if Player.query.filter_by(nickname=newplayer.nickname).first() is None: db.session.add(newplayer) db.session.flush() player_id = newplayer.id db.session.commit() player = Player.query.filter_by(id=player_id).first() player.create_checkins() flash('Player {} ({} {}) added!'.format(newplayer.nickname, newplayer.first_name, newplayer.last_name)) return redirect(url_for('main.player_edit')) flash( 'Player {} ({} {}) already exists!'.format(newplayer.nickname, newplayer.first_name, newplayer.last_name), 'warning') if form.submit_edit.data and form.validate() and player is not None: player.first_name = form.first_name.data player.last_name = form.last_name.data player.nickname = form.nickname.data player.tagline = form.tagline.data db.session.add(player) db.session.commit() flash('Player {} ({} {}) modified!'.format(player.nickname, player.first_name, player.last_name)) return redirect(url_for('main.player_edit', nickname=player.nickname)) if form.submit_delete.data and player is not None: player.destroy_checkins() stats = PlayerSeasonStats.query.filter_by(player=player).all() for m in [player] + stats: db.session.delete(m) db.session.commit() flash( 'Player {} ({} {}) and all associated stats deleted!'.format( player.nickname, player.first_name, player.last_name), 'danger') return redirect(url_for('main.player_edit')) elif request.method == 'GET' and player is not None: form.first_name.data = player.first_name form.last_name.data = player.last_name form.nickname.data = player.nickname form.tagline.data = player.tagline return render_template('edit_player.html', title='Player Editor', form=form, player=player, all_players=all_players)
def captain(): reminder_form = ReminderSetForm() roster_form = RosterForm() players = current_roster('full') if request.method == 'POST' and roster_form.validate( ) and roster_form.submit.data: for player_form in roster_form.roster: if player_form.player.data is not None: p = Player.query.filter_by( nickname=player_form.player.data).first() if p.user: if p.user.check_role(['admin']): #Can not change admin pass elif current_user == p.user: #Can not change self pass else: p.role = player_form.role.data else: p.role = player_form.role.data db.session.add(p) db.session.commit() flash('Updated active roster!') return redirect(url_for('main.captain')) if request.method == 'POST' and reminder_form.add_btn.data: reminder_form.reminders.append_entry() if request.method == 'POST' and reminder_form.validate( ) and reminder_form.submit_reminder.data: for rem in reminder_form.reminders: if rem.rem_id.data: r = ReminderSettings.query.filter_by( id=rem.rem_id.data).first() if r: r.category = rem.category.data r.days_in_advance = rem.dia.data else: r = ReminderSettings(category=rem.category.data, days_in_advance=rem.dia.data) else: r = ReminderSettings(category=rem.category.data, days_in_advance=rem.dia.data) db.session.add(r) db.session.commit() flash('Updated email reminders') return redirect(url_for('main.captain')) if request.method == 'POST' and reminder_form.rem_btn.data: for rem in reminder_form.reminders: if rem.delete_reminder.data and rem.rem_id.data: r = ReminderSettings.query.filter_by( id=rem.rem_id.data).first() if r: db.session.delete(r) db.session.commit() flash('Updated email reminders') return redirect(url_for('main.captain')) elif request.method == 'GET': roster_form.fill_roster(players) reminder_form.load_reminders() upcoming_matches = Match.query.filter(Match.date >= date.today()).order_by( Match.date).all() return render_template('captain.html', roster_form=roster_form, reminder_form=reminder_form, players=players, upcoming_matches=upcoming_matches)
def enter_score(id): match = Match.query.filter_by(id=id).first() form = EnterScoresForm(obj=match) all_seasons = Season.query.order_by(Season.start_date.desc()).all() hl_form = HLScoreForm() if form.submit_details.data and form.validate() and match is not None: match.win = form.win.data match.overtime = form.overtime.data match.team_score = form.team_score.data match.opponent_score = form.opponent_score.data match.food = form.food.data match.match_summary = form.match_summary.data if form.scoresheet.data: file = form.scoresheet.data if file and scoresheets.file_allowed(file, file.filename): if match.scoresheet: b, k = url_parse_s3(match.scoresheet) response = delete_file_s3(b, k) file.filename = secure_filename(file.filename) output = upload_file_s3(file, current_app.config["S3_BUCKET"], folder='scoresheets') if output: match.scoresheet = output else: flash('Error uploading file', 'warning') else: flash('File not allowed', 'warning') else: if form.remove_scoresheet.data: if match.scoresheet: b, k = url_parse_s3(match.scoresheet) response = delete_file_s3(b, k) match.scoresheet = None db.session.add(match) db.session.commit() for p in Player.query.all(): p.update_player_stats(season=match.season) flash('Match {} {} {} details edited!'.format( match.date.strftime('%Y-%m-%d'), match.opponent.name, match.home_away)) return redirect(url_for('main.enter_score', id=match.id)) if form.submit_scores.data and form.validate() and match is not None: match.win = form.win.data match.overtime = form.overtime.data match.team_score = form.team_score.data match.opponent_score = form.opponent_score.data match.food = form.food.data match.match_summary = form.match_summary.data db.session.add(match) db.session.commit() match.delete_all_games() count = 1 for j, f in enumerate(form.d701): i = count + j game = Game(match=match, win=f.win.data, game_num=i, game_type='doubles 701') p1 = Player.query.filter_by(nickname=f.p1.data).first() p2 = Player.query.filter_by(nickname=f.p2.data).first() pg1 = PlayerGame(player=p1, game=game, stars=int(f.p1_stars.data)) pg2 = PlayerGame(player=p2, game=game, stars=int(f.p2_stars.data)) db.session.add_all([game, pg1, pg2]) db.session.commit() count = count + len(form.d701) for j, f in enumerate(form.d501): i = count + j game = Game(match=match, win=f.win.data, game_num=i, game_type='doubles 501') p1 = Player.query.filter_by(nickname=f.p1.data).first() p2 = Player.query.filter_by(nickname=f.p2.data).first() pg1 = PlayerGame(player=p1, game=game, stars=int(f.p1_stars.data)) pg2 = PlayerGame(player=p2, game=game, stars=int(f.p2_stars.data)) db.session.add_all([game, pg1, pg2]) db.session.commit() count = count + len(form.d501) for j, f in enumerate(form.s501): i = count + j game = Game(match=match, win=f.win.data, game_num=i, game_type='singles 501') p1 = Player.query.filter_by(nickname=f.p1.data).first() pg1 = PlayerGame(player=p1, game=game, stars=int(f.p1_stars.data)) db.session.add_all([game, pg1]) db.session.commit() for p in Player.query.all(): p.update_player_stats(season=match.season) p.update_activity() update_all_team_stats() match.update_match_stats() flash( 'Match {} {} {} scores entered successfully!'.format( match.date.strftime('%Y-%m-%d'), match.opponent.name, match.home_away), 'success') return redirect(url_for('main.enter_score', id=match.id)) if form.add_btn.data: # add one doubles and 2 singles form.d501.append_entry() form.s501.append_entry() form.s501.append_entry() form.load_games(match) return render_template('enter_score.html', title='Enter Scores', form=form, hl_form=hl_form, match=match) if form.rem_btn.data: # remove one doubles and two singles form.d501.pop_entry() form.s501.pop_entry() form.s501.pop_entry() form.load_games(match) return render_template('enter_score.html', title='Enter Scores', form=form, hl_form=hl_form, match=match) if hl_form.add_btn.data: new_row = hl_form.hl_scores.append_entry() roster_choices = current_roster('ordered') new_row.player.choices = [(p.nickname, p.nickname) for p in roster_choices] form.load_games(match) return render_template('enter_score.html', title='Enter Scores', form=form, hl_form=hl_form, match=match) if hl_form.rem_btn.data: hl_form.hl_scores.pop_entry() form.load_games(match) return render_template('enter_score.html', title='Enter Scores', form=form, hl_form=hl_form, match=match) if hl_form.submit_hl_scores.data and match is not None: print(hl_form.validate()) for fieldName, errorMessages in hl_form.errors.items(): for err in errorMessages: print(err) match.delete_all_books() hl_form.save_scores(match) for p in match.get_roster(): p.update_player_stats(season=match.season) p.update_activity() update_all_team_stats() flash( 'Match {} {} {} high/low scores entered successfully!'.format( match.date.strftime('%Y-%m-%d'), match.opponent.name, match.home_away), 'success') return redirect(url_for('main.enter_score', id=match.id)) if request.method == 'GET' and match is not None: form.load_games(match) hl_form.load_scores(match) if request.method == 'POST' and match is not None: form.load_games(match) hl_form.load_scores(match) return render_template('enter_score.html', title='Enter Scores', form=form, hl_form=hl_form, match=match, all_seasons=all_seasons)