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)
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)
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)
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))
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
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)
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_)
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_)
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)
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)
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'))
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
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
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'))
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)
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)
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)
def ranking(): bots = session.query(Bot).order_by(desc(Bot.score)) ranked_bots = enumerate(bots) return render_template('ranking.html', bots=ranked_bots)
def users(): users_ = session.query(User).order_by(User.nickname).all() return render_template('users/users.html', users=users_)
def matches(): # TODO add pagination matches_ = session.query(Match).order_by(desc(Match.id)).limit(40) return render_template('matches.html', matches=matches_)
def ranking(): bots = session.query(Bot).order_by(desc(Bot.score)) ranked_bots = enumerate(bots, 1) return render_template('ranking.html', bots=ranked_bots)
def load_user(id): return session.query(User).filter(User.id == id).one_or_none()
def rank(self): bots = enumerate(session.query(Bot).order_by(desc(Bot.score)).all()) return next(dropwhile(lambda bot: bot[1] != self, bots))[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_)