Esempio n. 1
0
def add_opponent(team_id=0):
    if not g.user.is_team_leader(team_id):
        flash(u'You must be a team leader to add an opponent.', 'error')
        return redirect(url_for('teams.my_teams'))

    opponents = Opponent.query.\
            filter_by(team_id=team_id).\
            order_by(Opponent.name.asc()).\
            all()

    api = request.values.get('api') == '1'

    form = OpponentForm()
    form.name.validators[0].values = [o.name for o in opponents]
    if form.validate_on_submit():
        opponent = Opponent(team_id=team_id,
                            name=form.name.data,
                            tag=form.tag.data)
        db.session.add(opponent)
        db.session.commit()
        flash(u'The opponent was successfully added.', 'success')

        if api:
            return jsonify(success=True,
                           opponent_id=opponent.id,
                           csrf=form.csrf_token.data,
                           opponent_name=opponent.name)

        if form.f.data == 'add_match':
            return redirect(url_for('matches.add',
                                    new_opponent_id=opponent.id))
        elif form.f.data:
            return redirect(
                url_for('matches.show',
                        action='edit',
                        match_id=int(form.f.data),
                        new_opponent_id=opponent.id))

        return redirect(url_for('opponents', team_id=team_id))

    form.f.data = request.values.get('f') or ''

    return rt('team_admin/opponent_form.html',
              team={
                  'id': team_id,
                  'name': g.user.teams[team_id].name
              },
              page={
                  'top': 'my_teams',
                  'sub': 'opponents'
              },
              adding=True,
              form=form)
Esempio n. 2
0
def opponent(team_id, opponent_id, action):
    if not g.user.is_team_leader(team_id):
        flash(u'You must be a team leader to add an opponent.', 'error')
        return redirect(url_for('teams.my_teams'))

    opponent = Opponent.query.filter_by(id=opponent_id).first()

    if not opponent or not g.user.is_team_leader(opponent.team_id) \
            or team_id != opponent.team_id:
        flash(u'Opponent not found.', 'error')
        return redirect(url_for('teams.my_teams'))

    if action == 'edit':
        opponents = Opponent.query.\
                filter_by(team_id=team_id).\
                order_by(Opponent.name.asc()).\
                all()

        form = OpponentForm(request.form, obj=opponent)
        form.name.validators[0].values = [ o.name for o in opponents if o.id\
                != opponent_id ]
        if form.validate_on_submit():
            form.populate_obj(opponent)
            db.session.commit()
            flash(u'The opponent was successfully updated.', 'success')
        else:
            return rt('team_admin/opponent_form.html',
                      page={
                          'top': 'my_teams',
                          'sub': 'opponents'
                      },
                      team={
                          'id': team_id,
                          'name': g.user.teams[team_id].name
                      },
                      opponent_id=opponent_id,
                      adding=False,
                      form=form)
    elif action == 'delete':
        if request.method == 'POST':
            db.session.delete(opponent)
            db.session.commit()
            flash(u'The opponent was successfully deleted.', 'success')

    return redirect(url_for('opponents', team_id=team_id))
Esempio n. 3
0
def add_opponent(team_id=0):
    if not g.user.is_team_leader(team_id):
        flash(u'You must be a team leader to add an opponent.', 'error')
        return redirect(url_for('teams.my_teams'))

    opponents = Opponent.query.\
            filter_by(team_id=team_id).\
            order_by(Opponent.name.asc()).\
            all()

    api = request.values.get('api') == '1'

    form = OpponentForm()
    form.name.validators[0].values = [ o.name for o in opponents ]
    if form.validate_on_submit():
        opponent = Opponent(team_id=team_id,
                            name=form.name.data,
                            tag=form.tag.data)
        db.session.add(opponent)
        db.session.commit()
        flash(u'The opponent was successfully added.', 'success')

        if api:
            return jsonify(success=True, opponent_id=opponent.id,
                    csrf=form.csrf_token.data,
                    opponent_name=opponent.name)

        if form.f.data == 'add_match':
            return redirect(url_for('matches.add', new_opponent_id=opponent.id))
        elif form.f.data:
            return redirect(url_for('matches.show', action='edit',
                match_id=int(form.f.data), new_opponent_id=opponent.id))

        return redirect(url_for('opponents',team_id=team_id))

    form.f.data = request.values.get('f') or ''

    return rt('team_admin/opponent_form.html',
            team={'id' : team_id, 'name':g.user.teams[team_id].name},
            page={'top':'my_teams', 'sub':'opponents'},
            adding=True, form=form)
Esempio n. 4
0
def opponent(team_id, opponent_id, action):
    if not g.user.is_team_leader(team_id):
        flash(u'You must be a team leader to add an opponent.', 'error')
        return redirect(url_for('teams.my_teams'))

    opponent = Opponent.query.filter_by(id=opponent_id).first()

    if not opponent or not g.user.is_team_leader(opponent.team_id) \
            or team_id != opponent.team_id:
        flash(u'Opponent not found.', 'error')
        return redirect(url_for('teams.my_teams'))

    if action == 'edit':
        opponents = Opponent.query.\
                filter_by(team_id=team_id).\
                order_by(Opponent.name.asc()).\
                all()

        form = OpponentForm(request.form, obj=opponent)
        form.name.validators[0].values = [ o.name for o in opponents if o.id\
                != opponent_id ]
        if form.validate_on_submit():
            form.populate_obj(opponent)
            db.session.commit()
            flash(u'The opponent was successfully updated.', 'success')
        else:
            return rt('team_admin/opponent_form.html',
                    page={'top':'my_teams', 'sub':'opponents'},
                    team={'id' : team_id, 'name':g.user.teams[team_id].name},
                    opponent_id=opponent_id,
                    adding=False, form=form)
    elif action == 'delete':
        if request.method == 'POST':
            db.session.delete(opponent)
            db.session.commit()
            flash(u'The opponent was successfully deleted.', 'success')

    return redirect(url_for('opponents', team_id=team_id))
Esempio n. 5
0
def show(match_id, action):
    match = Match.query.filter_by(id=match_id).first()
    if not match:
        flash(u"That match doesn't exist.", "error")
        return redirect(url_for('my_matches'))

    if not g.user.is_on_team(match.team_id):
        flash(u'You must be on the team to view its match availability.',
              "error")
        return redirect(url_for('all'))

    if match.date < datetime.datetime.utcnow():
        up_prev = 'previous'
    else:
        up_prev = 'upcoming'

    if action == 'edit':
        if not g.user.is_team_leader(match.team_id):
            flash(u'You must be a team leader to edit this match.', 'error')
            return redirect(url_for('my_matches'))

        servers = Server.query.\
                filter_by(team_id=match.team_id).\
                order_by('server_name')

        form = MatchForm(request.form, obj=match)
        form.competition_id.choices = [
            (c.id, c.name)
            for c in Competition.query.order_by('competition_name')
        ]
        form.server_id.choices = [(s.id, s.name) for s in servers]
        form.team_id.choices = [(t, g.user.teams[t].name)
                                for t in g.user.team_leader_teams]
        form.opponent_id.choices = [ (o.id, o.name) for o in \
                Opponent.query.filter_by(team_id=match.team_id).\
                order_by(Opponent.name.asc()).all() ]

        players = {}
        for p in match.players:
            players[p.user_id] = p.user.name

        if request.method == 'GET':
            if request.values.get('new_opponent_id'):
                form.opponent_id.data = int(
                    request.values.get('new_opponent_id'))
            elif request.values.get('new_server_id'):
                form.server_id.data = int(request.values.get('new_server_id'))

            form.date.data = to_user_timezone(form.date.data)
            form.date.label.text += ' (in %s)' \
                    % format_datetime(form.date.data, 'zzzz')

        if form.validate_on_submit():
            form.date.data = to_utc(form.date.data)

            to_delete = []
            for f in form.players:
                if f.delete.data:
                    to_delete.append(f.user_id.data)

            form.populate_obj(match)

            if len(to_delete):
                MatchPlayer.query.\
                        filter_by(match_id=match_id).\
                        filter(MatchPlayer.user_id.in_(to_delete)).delete(False)

            db.session.commit()
            flash(u'The match was successfully updated.', 'success')

            if request.values.get('from') == 'single':
                return redirect(url_for('show', match_id=match_id))

            return redirect(url_for('my_matches'))

        oform = OpponentForm()
        sform = ServerForm()
        tzform = UserTimeZoneForm(obj=g.user)

        return rt('matches/form.html',
                  form=form,
                  players=players,
                  page={
                      'top': 'my_matches',
                      'sub': up_prev
                  },
                  when=up_prev,
                  adding=False,
                  user_now=to_user_timezone(datetime.datetime.utcnow()),
                  tzform=tzform,
                  oform=oform,
                  sform=sform,
                  team_id=g.user.team_leader_teams[0],
                  match_id=match.id)
    elif action == 'delete':
        if not g.user.is_team_leader(match.team_id):
            flash(u'You must be a team leader to edit this match.', 'error')
            return redirect(url_for('my_matches'))

        if request.method == 'POST':
            db.session.delete(match)
            db.session.commit()
            flash(u'The match was successfully deleted.', 'success')

        return redirect(url_for('my_matches'))
    elif action == 'status':
        if not g.user.is_on_team(match.team_id):
            flash(u'You must be on this team to change your status.', 'error')
            return redirect(url_for('my_matches'))

        if up_prev == 'previous':
            flash(u'You cannot change your status on a past match.', 'error')
            return redirect(url_for('my_previous_matches'))

        form = MatchPlayerStatusForm(request.form)

        if form.validate_on_submit():
            if request.values.get('s') == 'available':
                player_status = MatchPlayer.StatusAvailable
            elif request.values.get('s') == 'maybe':
                player_status = MatchPlayer.StatusMaybe
            elif request.values.get('s') == 'unavailable':
                player_status = MatchPlayer.StatusUnavailable
            else:
                player_status = None

            if player_status is None:
                flash(u'That status is not valid!', 'error')
            else:
                try:
                    mu = MatchPlayer.query.filter_by(match_id=match.id,
                                                     user_id=g.user.id).one()
                    mu.status = player_status
                except NoResultFound:
                    mu = MatchPlayer(match_id=match.id,
                                     user_id=g.user.id,
                                     status=player_status)
                except Exception, e:
                    app.logger.error('Error finding MatchPlayer: %s, %s' % \
                            (g.user.id, e))
                    flash(u'Something bad happened. Please try again.',
                          'error')
                    return redirect(url_for('my_matches'))

                mu.date_updated = datetime.datetime.utcnow()
                db.session.add(mu)
                db.session.commit()

                if request.values.get('api') == '1':
                    psp = mu.pretty_status
                    return jsonify(success=True,
                                   csrf=form.csrf_token.data,
                                   match_id=match.id,
                                   user_id=g.user.id,
                                   user_name=g.user.name,
                                   player_status=player_status,
                                   player_status_pretty=psp)

                if request.values.get('from') == 'single':
                    return redirect(url_for('show', match_id=match_id))
Esempio n. 6
0
            # now notify players of new match via forum post
            if form.post_on_forums.data:
                forum_post = ForumBotQueuedPost(
                    team_id=match.team_id,
                    game_id=match.competition.game_id,
                    match_id=match.id,
                    subject=team_subject,
                    message=team_message)
                db.session.add(forum_post)
                db.session.commit()

            flash(u'The match was successfully added.', 'success')
            return redirect(url_for('my_matches'))

    oform = OpponentForm()
    sform = ServerForm()
    tzform = UserTimeZoneForm(obj=g.user)

    return rt('matches/form.html',
              page={
                  'top': 'my_matches',
                  'sub': 'add_match'
              },
              adding=True,
              team_id=g.user.team_leader_teams[0],
              user_now=to_user_timezone(datetime.datetime.utcnow()),
              tzform=tzform,
              oform=oform,
              sform=sform,
              form=form)