def add(): if g.user.is_on_team(): flash(u'You are already on a team. Leave your current team first ' 'before creating a new one.', 'error') return redirect(url_for('my_teams')) team_names = [ t.name.lower() for t in Team.query.all() ] form = TeamForm() form.name.validators[0].values = team_names if form.validate_on_submit(): team = Team(name=form.name.data, tag=form.tag.data, url=form.url.data, date_created=datetime.datetime.utcnow(), join_password=form.join_password.data) db.session.add(team) db.session.commit() # hmm, need to commit the team to get the team id team_player = TeamPlayer(team_id=team.id, user_id=g.user.id, status=TeamPlayer.StatusFounder) db.session.add(team_player) db.session.commit() flash(u'The team was successfully created.', 'success') return redirect(url_for('my_teams')) return rt('teams/create.html', page={'top': 'teams', 'sub': 'add_team'}, adding=True, form=form)
def add(): if g.user.is_on_team(): flash( u'You are already on a team. Leave your current team first ' 'before creating a new one.', 'error') return redirect(url_for('my_teams')) team_names = [t.name.lower() for t in Team.query.all()] form = TeamForm() form.name.validators[0].values = team_names if form.validate_on_submit(): team = Team(name=form.name.data, tag=form.tag.data, url=form.url.data, date_created=datetime.datetime.utcnow(), join_password=form.join_password.data) db.session.add(team) db.session.commit() # hmm, need to commit the team to get the team id team_player = TeamPlayer(team_id=team.id, user_id=g.user.id, status=TeamPlayer.StatusFounder) db.session.add(team_player) db.session.commit() flash(u'The team was successfully created.', 'success') return redirect(url_for('my_teams')) return rt('teams/create.html', page={ 'top': 'teams', 'sub': 'add_team' }, adding=True, form=form)
def show(team_id=-1, action=''): page = { 'top' : 'team', 'sub' : 'main' } if team_id and type(team_id) == int: if team_id == -1: team_id = g.user.one_team.id page = {'top':'my_teams', 'sub':'all_my'} elif g.user.is_on_team(team_id): page = {'top':'my_teams', 'sub':'all_my'} if team_id == -1: if not g.user.is_guest: team_players = TeamPlayer.query.options(eagerload('team')).\ filter_by(user_id=g.user.id).\ order_by(TeamPlayer.status.asc()).\ all() teams = [ t.team for t in team_players ] return rt('teams/table.html', page={'top':'my_teams', 'sub':'all_my'}, teams=teams) return redirect(url_for('all')) if team_id > 0: team = Team.query.filter_by(id=team_id).first() else: team = None if not team: flash(u'Team not found', 'error') return redirect(url_for('all')) if action == 'join': if g.user.is_guest: flash(u'You must be signed in to join a team.', 'error') elif g.user.is_on_team(team_id): flash(u'You are already on this team.', 'info') elif g.user.is_on_team(): flash(u'You are already on a team. Leave your current team ' 'first before joining this team.', 'error') else: join_form = JoinTeamForm(request.form) def validate_password(form, field): if field.data != team.join_password: raise ValidationError(\ u"The password you entered is incorrect.") JoinTeamForm.validate_password = validate_password if join_form.validate_on_submit(): tp = TeamPlayer(user_id=g.user.id, team_id=team.id, date_joined=datetime.datetime.utcnow(), status=TeamPlayer.StatusNormal) db.session.add(tp) db.session.commit() flash(u'You have successfully joined this team.', 'success') return redirect(url_for('show', team_id=team.id)) elif action == 'leave': if g.user.is_guest: flash(u'You must be signed in to join a team.', 'error') elif not g.user.is_on_team(team_id): flash(u'You are not on this team.', 'error') else: leave_form = LeaveTeamForm(request.form) if leave_form.validate_on_submit(): # make sure there is still a founder on the team skip = False if team.id in g.user.founder_teams: other_founders = False for p in team.players: if p.status == TeamPlayer.StatusFounder and \ p.user_id != g.user.id: other_founders = True break if not other_founders: flash(u'You cannot leave this team because you '\ 'are the only founder. Add another '\ 'founder first.', 'error') skip = True if not skip: TeamPlayer.query.\ filter_by(team_id=team.id).\ filter_by(user_id=g.user.id).delete(False) MatchPlayer.query.\ filter(MatchPlayer.match_id.in_(\ db.session.query(Match.id).\ filter_by(team_id=team.id))).\ filter_by(user_id=g.user.id).delete(False) db.session.commit() flash(u'You have successfully left this team.', 'success') return redirect(url_for('show',team_id=team.id)) # only edit or delete for own team if a team leader elif g.user.is_team_leader(team_id): if action == 'edit' and \ request.values.get('edit_players') == '1': action = 'edit_players' team_names = [ t.name.lower() for t in Team.query.all() ] if team.name.lower() in team_names: team_names.remove(team.name.lower()) if action == 'edit': form = TeamForm(request.form, obj=team) players_form = TeamPlayersForm(ImmutableMultiDict(), obj=team) else: form = TeamForm(ImmutableMultiDict(), obj=team) players_form = TeamPlayersForm(request.form, obj=team) form.name.validators[0].values = team_names if action == 'edit': players = {} for p in team.players: players[p.user_id] = p.user.name if not g.user.is_founder(team_id): choices_no_founder = [ (s, n) for s, n in \ TeamPlayer.StatusChoices if s != TeamPlayer.StatusFounder ] for f in players_form.players: if f.status.data == TeamPlayer.StatusFounder: f.delete.disabled = True f.status.disabled = True f.status.choices = [ (TeamPlayer.StatusFounder, "Founder") ] else: f.status.choices = choices_no_founder if form.validate_on_submit(): form.populate_obj(team) db.session.commit() flash(u'The team was successfully updated.', 'success') return redirect(url_for('show', team_id=team.id, action='edit')) return rt('teams/create.html', team_id=team.id, page={'top':'my_teams', 'sub':'edit'}, team=team, players=players, form=form, players_form=players_form) elif action == 'edit_players': players = {} for p in team.players: players[p.user_id] = p.user.name if not g.user.is_founder(team_id): choices_no_founder = [ (s, n) for s, n in \ TeamPlayer.StatusChoices if s != TeamPlayer.StatusFounder ] for f in players_form.players: if f.status.data == TeamPlayer.StatusFounder: f.delete.disabled = True f.status.disabled = True f.status.choices = [ (TeamPlayer.StatusFounder, "Founder") ] else: f.status.choices = choices_no_founder if players_form.validate_on_submit(): founders = set([ p.user_id for p in team.players if p.status==TeamPlayer.StatusFounder ]) new_statuses = {} new_founders = set([f for f in founders]) editing_founders = False deleting_founders = False to_delete = [] for f in players_form.players: if f.delete.data: to_delete.append(f.user_id.data) if f.user_id.data in founders: new_founders.remove(f.user_id.data) deleting_founders = True elif f.status.data == TeamPlayer.StatusFounder: new_founders.add(f.user_id.data) else: try: new_founders.remove(f.user_id.data) except: pass new_statuses[f.user_id.data] = f.status.data if founders != new_founders: editing_founders = True save = True if len(new_founders) < 1: flash(u'There must be at least one founder on the ' 'team.', 'error') save = False if team_id not in g.user.founder_teams and \ (editing_founders or deleting_founders): flash(u'You must be a founder to edit or delete a ' 'founder.', 'error') save = False if save: for p in team.players: if p.user_id in new_statuses: p.status = new_statuses[p.user_id] if len(to_delete): TeamPlayer.query.filter_by(team_id=team_id).\ filter(TeamPlayer.user_id.in_(to_delete)).\ delete(False) MatchPlayer.query.\ filter_by(team_id=team_id).\ filter(MatchPlayer.user_id.in_(to_delete)).\ delete(False) db.session.commit() flash(u'The team was successfully updated.', 'success') return redirect(url_for('show', team_id=team.id, action='edit')) return rt('teams/create.html', page={'top':'my_teams', 'sub':'edit'}, team_id=team.id, team=team, players=players, form=form, players_form=players_form) elif action == 'delete': if request.method == 'POST': db.session.delete(team) db.session.commit() flash(u'The team was successfuly deleted.', 'success') return redirect(url_for('all')) flash(u"That team doesn't exist.", 'error') return redirect(url_for('show', team_id=team.id)) elif action in ('delete', 'edit'): flash(u'You must be a team leader to edit the team.', 'error') join_form = None leave_form = None if not g.user.is_guest: if not g.user.is_on_team(team_id): join_form = JoinTeamForm() else: leave_form = LeaveTeamForm() cmatches = CompletedMatch.query.filter_by(team_id=team_id).all() wins = 0 losses = 0 draws = 0 for c in cmatches: if c.wins > c.losses: wins += 1 elif c.wins < c.losses: losses += 1 else: draws += 1 players = team.players.join(User).\ order_by(TeamPlayer.status.asc()).\ order_by(User.name.asc()) return rt('teams/single.html', page=page, wins=wins, losses=losses, draws=draws, team=team, players=players, leave_form=leave_form, join_form=join_form) return redirect(url_for('all'))
def show(team_id=-1, action=''): page = {'top': 'team', 'sub': 'main'} if team_id and type(team_id) == int: if team_id == -1: team_id = g.user.one_team.id page = {'top': 'my_teams', 'sub': 'all_my'} elif g.user.is_on_team(team_id): page = {'top': 'my_teams', 'sub': 'all_my'} if team_id == -1: if not g.user.is_guest: team_players = TeamPlayer.query.options(eagerload('team')).\ filter_by(user_id=g.user.id).\ order_by(TeamPlayer.status.asc()).\ all() teams = [t.team for t in team_players] return rt('teams/table.html', page={ 'top': 'my_teams', 'sub': 'all_my' }, teams=teams) return redirect(url_for('all')) if team_id > 0: team = Team.query.filter_by(id=team_id).first() else: team = None if not team: flash(u'Team not found', 'error') return redirect(url_for('all')) if action == 'join': if g.user.is_guest: flash(u'You must be signed in to join a team.', 'error') elif g.user.is_on_team(team_id): flash(u'You are already on this team.', 'info') elif g.user.is_on_team(): flash( u'You are already on a team. Leave your current team ' 'first before joining this team.', 'error') else: join_form = JoinTeamForm(request.form) def validate_password(form, field): if field.data != team.join_password: raise ValidationError(\ u"The password you entered is incorrect.") JoinTeamForm.validate_password = validate_password if join_form.validate_on_submit(): tp = TeamPlayer(user_id=g.user.id, team_id=team.id, date_joined=datetime.datetime.utcnow(), status=TeamPlayer.StatusNormal) db.session.add(tp) db.session.commit() flash(u'You have successfully joined this team.', 'success') return redirect(url_for('show', team_id=team.id)) elif action == 'leave': if g.user.is_guest: flash(u'You must be signed in to join a team.', 'error') elif not g.user.is_on_team(team_id): flash(u'You are not on this team.', 'error') else: leave_form = LeaveTeamForm(request.form) if leave_form.validate_on_submit(): # make sure there is still a founder on the team skip = False if team.id in g.user.founder_teams: other_founders = False for p in team.players: if p.status == TeamPlayer.StatusFounder and \ p.user_id != g.user.id: other_founders = True break if not other_founders: flash(u'You cannot leave this team because you '\ 'are the only founder. Add another '\ 'founder first.', 'error') skip = True if not skip: TeamPlayer.query.\ filter_by(team_id=team.id).\ filter_by(user_id=g.user.id).delete(False) MatchPlayer.query.\ filter(MatchPlayer.match_id.in_(\ db.session.query(Match.id).\ filter_by(team_id=team.id))).\ filter_by(user_id=g.user.id).delete(False) db.session.commit() flash(u'You have successfully left this team.', 'success') return redirect(url_for('show', team_id=team.id)) # only edit or delete for own team if a team leader elif g.user.is_team_leader(team_id): if action == 'edit' and \ request.values.get('edit_players') == '1': action = 'edit_players' team_names = [t.name.lower() for t in Team.query.all()] if team.name.lower() in team_names: team_names.remove(team.name.lower()) if action == 'edit': form = TeamForm(request.form, obj=team) players_form = TeamPlayersForm(ImmutableMultiDict(), obj=team) else: form = TeamForm(ImmutableMultiDict(), obj=team) players_form = TeamPlayersForm(request.form, obj=team) form.name.validators[0].values = team_names if action == 'edit': players = {} for p in team.players: players[p.user_id] = p.user.name if not g.user.is_founder(team_id): choices_no_founder = [ (s, n) for s, n in \ TeamPlayer.StatusChoices if s != TeamPlayer.StatusFounder ] for f in players_form.players: if f.status.data == TeamPlayer.StatusFounder: f.delete.disabled = True f.status.disabled = True f.status.choices = [(TeamPlayer.StatusFounder, "Founder")] else: f.status.choices = choices_no_founder if form.validate_on_submit(): form.populate_obj(team) db.session.commit() flash(u'The team was successfully updated.', 'success') return redirect( url_for('show', team_id=team.id, action='edit')) return rt('teams/create.html', team_id=team.id, page={ 'top': 'my_teams', 'sub': 'edit' }, team=team, players=players, form=form, players_form=players_form) elif action == 'edit_players': players = {} for p in team.players: players[p.user_id] = p.user.name if not g.user.is_founder(team_id): choices_no_founder = [ (s, n) for s, n in \ TeamPlayer.StatusChoices if s != TeamPlayer.StatusFounder ] for f in players_form.players: if f.status.data == TeamPlayer.StatusFounder: f.delete.disabled = True f.status.disabled = True f.status.choices = [(TeamPlayer.StatusFounder, "Founder")] else: f.status.choices = choices_no_founder if players_form.validate_on_submit(): founders = set([ p.user_id for p in team.players if p.status == TeamPlayer.StatusFounder ]) new_statuses = {} new_founders = set([f for f in founders]) editing_founders = False deleting_founders = False to_delete = [] for f in players_form.players: if f.delete.data: to_delete.append(f.user_id.data) if f.user_id.data in founders: new_founders.remove(f.user_id.data) deleting_founders = True elif f.status.data == TeamPlayer.StatusFounder: new_founders.add(f.user_id.data) else: try: new_founders.remove(f.user_id.data) except: pass new_statuses[f.user_id.data] = f.status.data if founders != new_founders: editing_founders = True save = True if len(new_founders) < 1: flash( u'There must be at least one founder on the ' 'team.', 'error') save = False if team_id not in g.user.founder_teams and \ (editing_founders or deleting_founders): flash( u'You must be a founder to edit or delete a ' 'founder.', 'error') save = False if save: for p in team.players: if p.user_id in new_statuses: p.status = new_statuses[p.user_id] if len(to_delete): TeamPlayer.query.filter_by(team_id=team_id).\ filter(TeamPlayer.user_id.in_(to_delete)).\ delete(False) MatchPlayer.query.\ filter_by(team_id=team_id).\ filter(MatchPlayer.user_id.in_(to_delete)).\ delete(False) db.session.commit() flash(u'The team was successfully updated.', 'success') return redirect( url_for('show', team_id=team.id, action='edit')) return rt('teams/create.html', page={ 'top': 'my_teams', 'sub': 'edit' }, team_id=team.id, team=team, players=players, form=form, players_form=players_form) elif action == 'delete': if request.method == 'POST': db.session.delete(team) db.session.commit() flash(u'The team was successfuly deleted.', 'success') return redirect(url_for('all')) flash(u"That team doesn't exist.", 'error') return redirect(url_for('show', team_id=team.id)) elif action in ('delete', 'edit'): flash(u'You must be a team leader to edit the team.', 'error') join_form = None leave_form = None if not g.user.is_guest: if not g.user.is_on_team(team_id): join_form = JoinTeamForm() else: leave_form = LeaveTeamForm() cmatches = CompletedMatch.query.filter_by(team_id=team_id).all() wins = 0 losses = 0 draws = 0 for c in cmatches: if c.wins > c.losses: wins += 1 elif c.wins < c.losses: losses += 1 else: draws += 1 players = team.players.join(User).\ order_by(TeamPlayer.status.asc()).\ order_by(User.name.asc()) return rt('teams/single.html', page=page, wins=wins, losses=losses, draws=draws, team=team, players=players, leave_form=leave_form, join_form=join_form) return redirect(url_for('all'))