Пример #1
0
def update_bot(username, botname):
    if username != login.current_user.nickname:
        abort(400)  # Should not happen

    form = UpdateBotForm()
    user = session.query(User).filter_by(nickname=username).one_or_none()
    bot = session.query(Bot).filter_by(user=user, name=botname).one_or_none()

    if user is None:
        flash('User {} does not exist.')
        redirect(url_for('users'))

    if bot is None:
        flash('{} does not exist or does not belong to {}'.format(
            botname, username))
        return redirect(url_for('profile'))

    if not form.compile_cmd.data and not form.run_cmd.data:
        form.compile_cmd.data = bot.compile_cmd
        form.run_cmd.data = bot.run_cmd

    if form.validate_on_submit():
        bot.compile_cmd = form.compile_cmd.data
        bot.run_cmd = form.run_cmd.data
        bot.compiled = False if bot.compile_cmd else bot.compiled

        files = request.files.getlist('files')
        parent = p.join(config.BOT_CODE_DIR, user.nickname, bot.name)
        make_files(files, parent)

        flash('Update bot "%s" succesfully!' % bot.name)
        return redirect(url_for('profile'))

    return render_template('bots/update.html', form=form)
Пример #2
0
def update_bot(username, botname):
    if username != login.current_user.nickname:
        abort(400)  # Should not happen

    form = UpdateBotForm()
    user = session.query(User).filter_by(nickname=username).one_or_none()
    bot = session.query(Bot).filter_by(user=user, name=botname).one_or_none()

    if user is None:
        flash('User {} does not exist.')
        return redirect(url_for('users'))

    if bot is None:
        flash('{} does not exist or does not belong to {}'
              .format(botname, username))
        return redirect(url_for('profile'))

    if not form.compile_cmd.data and not form.run_cmd.data:
        form.compile_cmd.data = bot.compile_cmd
        form.run_cmd.data = bot.run_cmd

    if form.validate_on_submit():
        bot.compile_cmd = form.compile_cmd.data
        bot.run_cmd = form.run_cmd.data
        bot.compiled = False if bot.compile_cmd else bot.compiled

        files = request.files.getlist('files')
        parent = p.join(config.BOT_CODE_DIR, user.nickname, bot.name)
        make_files(files, parent)
        db.merge(bot)
        flash('Update bot "%s" succesfully!' % bot.name)
        return redirect(url_for('profile'))

    return render_template('bots/update.html', form=form)
Пример #3
0
def bot_page(username, botname):

    user = session.query(User).filter_by(nickname=username).one_or_none()
    if user is None:
        flash('User {} does not exist.')
        return redirect(url_for('users'))

    bot = session.query(Bot).filter_by(user=user, name=botname).one_or_none()
    if bot is None:
        flash('{} does not exist or does not belong to {}'.format(
            botname, username))
        return redirect(url_for('user_page', username=username))

    return render_template('bots/bot.html', bot=bot)
Пример #4
0
    def __call__(self, _, field):

        user = session.query(self.class_).filter(
            getattr(self.class_, self.attribute) == field.data
        ).first()

        if user is not None:
            raise ValidationError("{} already in use.".format(self.attribute))
Пример #5
0
def find_participants(n=2):
    bots = list(db.query(Bot).order_by(func.random()).limit(n))

    if len(bots) != n:
        raise LookupError('Not enough bots found in database')

    logging.info('Letting %s fight', bots)
    return bots
Пример #6
0
def user_page(username):
    user = session.query(User).filter_by(nickname=username).one_or_none()

    if user is None:
        flash('User with name {} does not exist.'.format(username))
        return redirect(url_for('users'))

    return render_template('users/user.html', user=user)
Пример #7
0
    def __call__(self, _, field):

        user = session.query(self.class_).filter(
            getattr(self.class_, self.attribute) == field.data
        ).first()

        if user is not None:
            raise ValidationError("{} already in use.".format(self.attribute))
Пример #8
0
def user_page(username):
    user = session.query(User).filter_by(nickname=username).one_or_none()

    if user is None:
        flash('User with name {} does not exist.'.format(username))
        return redirect(url_for('users'))

    return render_template('users/user.html', user=user)
Пример #9
0
def find_participants(n=2):
    bots = list(db.query(Bot).order_by(func.random()).limit(n))

    if len(bots) != n:
        raise LookupError('Not enough bots found in database')

    logging.info('Letting %s fight', bots)
    return bots
Пример #10
0
def bot_page(username, botname):

    user = session.query(User).filter_by(nickname=username).one_or_none()
    if user is None:
        flash('User {} does not exist.')
        return redirect(url_for('users'))

    bot = session.query(Bot).filter_by(user=user, name=botname).one_or_none()
    if bot is None:
        flash('{} does not exist or does not belong to {}'.format(
            botname, username))
        return redirect(url_for('user_page', username=username))
    paginated_bot_participations_ = paginate(
        bot.participations.order_by(desc(MatchParticipation.match_id)))

    return render_template(
        'bots/bot.html',
        bot=bot,
        paginated_bot_participations=paginated_bot_participations_)
Пример #11
0
def bot_page(username, botname):

    user = session.query(User).filter_by(nickname=username).one_or_none()
    if user is None:
        flash('User {} does not exist.')
        return redirect(url_for('users'))

    bot = session.query(Bot).filter_by(user=user, name=botname).one_or_none()
    if bot is None:
        flash('{} does not exist or does not belong to {}'
              .format(botname, username))
        return redirect(url_for('user_page', username=username))
    paginated_bot_participations_ = paginate(bot.participations.order_by(
        desc(MatchParticipation.match_id)
    ))

    return render_template(
        'bots/bot.html', bot=bot,
        paginated_bot_participations=paginated_bot_participations_)
Пример #12
0
def match_page(matchid):
    match = session.query(Match).filter_by(id=matchid).one_or_none()

    if match is None:
        flash('Match with id {} does not exist.'.format(matchid))
        return redirect(url_for('matches'))

    my_participations = [participation
                         for participation in match.participations
                         if participation.bot.user == login.current_user]
    return render_template('bots/match.html', match=match,
                           my_participations=my_participations)
Пример #13
0
def match_page(matchid):
    match = session.query(Match).filter_by(id=matchid).one_or_none()

    if match is None:
        flash('Match with id {} does not exist.'.format(matchid))
        return redirect(url_for('matches'))

    my_participations = [
        participation for participation in match.participations
        if participation.bot.user == login.current_user
    ]
    return render_template('bots/match.html',
                           match=match,
                           my_participations=my_participations)
Пример #14
0
def remove_bot(username, botname):
    if username != login.current_user.nickname:
        abort(400)  # Should not happen

    bot = (session.query(Bot).filter_by(user=login.current_user,
                                        name=botname).one_or_none())

    if bot is None:
        flash('{} does not exist or does not belong to {}'.format(
            botname, username))
        return redirect(url_for('profile'))

    db.remove_bot(bot)
    flash('Removed bot "%s" succesfully!' % botname)
    return redirect(url_for('profile'))
Пример #15
0
    def validate(self):
        rv = Form.validate(self)
        if not rv:
            return rv

        user = session.query(User).filter_by(nickname=self.nickname.data).one_or_none()
        if user is None:
            self.nickname.errors.append('User unknown.')
            return False

        check = user.check_password(self.password.data)
        if not check:
            self.nickname.errors.append('Incorrect password.')
            return False

        return True
Пример #16
0
    def validate(self):
        rv = Form.validate(self)
        if not rv:
            return rv

        user = session.query(User).filter_by(nickname=self.nickname.data).one_or_none()
        if user is None:
            self.nickname.errors.append("User unknown.")
            return False

        check = user.check_password(self.password.data)
        if not check:
            self.nickname.errors.append("Incorrect password.")
            return False

        return True
Пример #17
0
def remove_bot(username, botname):
    if username != login.current_user.nickname:
        abort(400)  # Should not happen

    bot = (session.query(Bot)
           .filter_by(user=login.current_user, name=botname)
           .one_or_none())

    if bot is None:
        flash('{} does not exist or does not belong to {}'
              .format(botname, username))
        return redirect(url_for('profile'))

    db.remove_bot(bot)
    flash('Removed bot "%s" succesfully!' % botname)
    return redirect(url_for('profile'))
Пример #18
0
def remove_bot(user, botname):
    code_dir = os.path.join(config.BOT_CODE_DIR, user.nickname, botname)
    try:
        shutil.rmtree(code_dir)
    except FileNotFoundError:
        # Don't crash if for some reason this dir doesn't exist anymore
        logging.warning('Code dir of bot %s:%s not found (%s)' %
                        (user.nickname, botname, code_dir))
        pass

    bot = session.query(Bot).filter_by(user=user, name=botname).one_or_none()
    if bot is None:
        logging.warning(
            'Trying to remove a bot that does not exist. User:{}, Bot:{}'.
            format(user, botname))
        return

    delete(bot)
Пример #19
0
def login():
    form = LoginForm()
    if form.validate_on_submit():
        user = session.query(User).filter_by(nickname=form.nickname.data).first()
        if user is None:
            abort(513)  # shouldn't happen

        if not login_user(user, remember=form.remember_me.data):
            flash('Failed to login.')
            return redirect(url_for('home'))

        url = request.args.get('next') or url_for('home')
        flash('Logged in succesfully!')
        return redirect(url)

    elif request.method == 'POST':
        flash('Failed to log in. Please check your credentials.')

    return render_template('users/login.html', form=form)
Пример #20
0
def login():
    form = LoginForm()
    if form.validate_on_submit():
        user = session.query(User).filter_by(
            nickname=form.nickname.data).first()
        if user is None:
            abort(513)  # shouldn't happen

        if not login_user(user, remember=form.remember_me.data):
            flash('Failed to login.')
            return redirect(url_for('home'))

        url = request.args.get('next') or url_for('home')
        flash('Logged in succesfully!')
        return redirect(url)

    elif request.method == 'POST':
        flash('Failed to log in. Please check your credentials.')

    return render_template('users/login.html', form=form)
Пример #21
0
def ranking():
    bots = session.query(Bot).order_by(desc(Bot.score))
    ranked_bots = enumerate(bots)
    return render_template('ranking.html', bots=ranked_bots)
Пример #22
0
def users():
    users_ = session.query(User).order_by(User.nickname).all()
    return render_template('users/users.html', users=users_)
Пример #23
0
def matches():
    # TODO add pagination
    matches_ = session.query(Match).order_by(desc(Match.id)).limit(40)
    return render_template('matches.html', matches=matches_)
Пример #24
0
def ranking():
    bots = session.query(Bot).order_by(desc(Bot.score))
    ranked_bots = enumerate(bots, 1)
    return render_template('ranking.html', bots=ranked_bots)
Пример #25
0
def load_user(id):
    return session.query(User).filter(User.id == id).one_or_none()
Пример #26
0
def users():
    users_ = session.query(User).order_by(User.nickname).all()
    return render_template('users/users.html', users=users_)
Пример #27
0
 def rank(self):
     bots = enumerate(session.query(Bot).order_by(desc(Bot.score)).all())
     return next(dropwhile(lambda bot: bot[1] != self, bots))[0]
Пример #28
0
def load_user(id):
    return session.query(User).filter(User.id == id).one_or_none()
Пример #29
0
 def rank(self):
     bots = enumerate(session.query(Bot).order_by(desc(Bot.score)).all())
     return next(dropwhile(lambda bot: bot[1] != self, bots))[0]
Пример #30
0
def matches():
    matches_ = session.query(Match).order_by(desc(Match.id))
    paginated_matches_ = paginate(matches_)

    return render_template('matches.html',
                           paginated_matches=paginated_matches_)
Пример #31
0
def matches():
    matches_ = session.query(Match).order_by(desc(Match.id))
    paginated_matches_ = paginate(matches_)

    return render_template('matches.html', paginated_matches=paginated_matches_)