def put(self, challenge_id): challenge = models.Challenge.query.get_or_404(challenge_id) data = flask.request.get_json() old_unlocked = challenge.unlocked for field in ( 'name', 'description', 'points', 'cat_slug', 'unlocked', 'weight', 'validator'): setattr( challenge, field, data.get(field, getattr(challenge, field))) if 'answer' in data and data['answer']: answer = utils.normalize_input(data['answer']) validator = validators.GetValidatorForChallenge(challenge) validator.change_answer(answer) if 'attachments' in data: challenge.set_attachments(data['attachments']) if 'prerequisite' in data: challenge.set_prerequisite(data['prerequisite']) else: challenge.prerequisite = '' if 'tags' in data: challenge.set_tags(data['tags']) if challenge.unlocked and not old_unlocked: news = 'Challenge "%s" unlocked!' % challenge.name models.News.game_broadcast(message=news) app.logger.info('Challenge %s updated by %r.', challenge, models.User.current()) models.commit() cache.clear() return challenge
def post(self): data = flask.request.get_json() unlocked = data.get('unlocked', False) answer = utils.normalize_input(data['answer']) chall = models.Challenge.create( data['name'], data['description'], data['points'], '', data['cat_slug'], unlocked, data.get('validator', validators.GetDefaultValidator())) validator = validators.GetValidatorForChallenge(chall) validator.change_answer(answer) if 'attachments' in data: chall.set_attachments(data['attachments']) if 'prerequisite' in data: chall.set_prerequisite(data['prerequisite']) if 'tags' in data: chall.set_tags(data['tags']) if unlocked and utils.GameTime.open(): news = 'New challenge created: "%s"' % chall.name models.News.game_broadcast(message=news) models.commit() app.logger.info('Challenge %s created by %r.', chall, models.User.current()) return chall
def post(self): data = flask.request.get_json() unlocked = data.get('unlocked', False) answer = utils.normalize_input(data['answer']) chall = models.Challenge.create( data['name'], data['description'], data['points'], answer, data['cat_slug'], unlocked) if 'attachments' in data: chall.set_attachments(data['attachments']) if 'prerequisite' in data: chall.set_prerequisite(data['prerequisite']) if 'tags' in data: chall.set_tags(data['tags']) if unlocked and utils.GameTime.open(): news = 'New challenge created: "%s"' % chall.name models.News.game_broadcast(message=news) models.commit() app.logger.info('Challenge %s created by %r.', chall, models.User.current()) return chall
def put(self, challenge_id): challenge = models.Challenge.query.get_or_404(challenge_id) data = flask.request.get_json() old_unlocked = challenge.unlocked for field in ( 'name', 'description', 'points', 'cat_slug', 'unlocked', 'weight'): setattr( challenge, field, data.get(field, getattr(challenge, field))) if 'answer' in data and data['answer']: answer = utils.normalize_input(data['answer']) challenge.change_answer(answer) if 'attachments' in data: challenge.set_attachments(data['attachments']) if 'prerequisite' in data: challenge.set_prerequisite(data['prerequisite']) else: challenge.prerequisite = '' if 'tags' in data: challenge.set_tags(data['tags']) if challenge.unlocked and not old_unlocked: news = 'Challenge "%s" unlocked!' % challenge.name models.News.game_broadcast(message=news) app.logger.info('Challenge %s updated by %r.', challenge, models.User.current()) models.commit() cache.clear() return challenge
def post(self): data = flask.request.get_json() unlocked = data.get('unlocked', False) answer = utils.normalize_input(data['answer']) if not validators.IsValidator(data.get('validator', None)): raise errors.ValidationError('Invalid validator.') chall = models.Challenge.create( data['name'], data['description'], data['points'], '', unlocked, data.get('validator', validators.GetDefaultValidator())) validator = validators.GetValidatorForChallenge(chall) validator.change_answer(answer) if 'attachments' in data: chall.set_attachments(data['attachments']) if 'prerequisite' in data: chall.set_prerequisite(data['prerequisite']) if 'tags' in data: chall.set_tags(data['tags']) if unlocked and utils.GameTime.open(): news = 'New challenge created: "%s"' % chall.name models.News.game_broadcast(message=news) models.commit() app.logger.info('Challenge %s created by %r.', chall, models.User.current()) return chall
def post(self): data = flask.request.get_json() answer = utils.normalize_input(data['answer']) points = controllers.submit_answer(data['cid'], answer) models.commit() cache.delete_team('cats/%d') cache.delete('scoreboard') return dict(points=points)
def post(self): data = flask.request.get_json() answer = utils.normalize_input(data['answer']) try: correct = controllers.test_answer(data['cid'], answer) except errors.IntegrityError: raise errors.InvalidAnswerError('Invalid answer.') if not correct: raise errors.InvalidAnswerError('Invalid answer.') return dict(message='Answer OK.')
def post(self): data = flask.request.get_json() answer = utils.normalize_input(data['answer']) try: correct = controllers.test_answer(data['cid'], answer) except errors.IntegrityError: raise errors.InvalidAnswerError('Invalid answer.') if not correct: raise errors.InvalidAnswerError('Invalid answer.') return dict(message='Answer OK.')
def post_player(self, data): answer = utils.normalize_input(data['answer']) try: points = controllers.submit_answer(data['cid'], answer, data.get('token')) except (errors.IntegrityError, errors.FlushError): models.db.session.rollback() raise errors.AccessDeniedError( 'Previously solved or flag already used.') cache.delete_team('cats/%d') cache.delete('scoreboard') return dict(points=points)
def post(self): data = flask.request.get_json() answer = utils.normalize_input(data['answer']) points = controllers.submit_answer(data['cid'], answer) try: models.commit() except (errors.IntegrityError, errors.FlushError): models.db.session.rollback() raise errors.AccessDeniedError("You've already solved that one!") cache.delete_team('cats/%d') cache.delete('scoreboard') return dict(points=points)
def post_player(self, data): answer = utils.normalize_input(data['answer']) try: points = controllers.submit_answer( data['cid'], answer, data.get('token')) except errors.IntegrityError: raise errors.AccessDeniedError( 'Previously solved or flag already used.') try: models.commit() except (errors.IntegrityError, errors.FlushError): models.db.session.rollback() raise errors.AccessDeniedError("You've already solved that one!") cache.delete_team('cats/%d') cache.delete('scoreboard') return dict(points=points)
def test_normalize_input(): # Sanity checking assert utils.normalize_input("hello") == "hello" assert not utils.normalize_input("hello") == "world" # Test normalize_input on preceding whitespace assert utils.normalize_input(" hello") == "hello" assert utils.normalize_input(" hello") == "hello" assert utils.normalize_input("\thello") == "hello" assert utils.normalize_input(" \n hello") == "hello" assert utils.normalize_input("\t\nhello") == "hello" # Test normalize_input on trailing whitespace assert utils.normalize_input("hello ") == "hello" assert utils.normalize_input("hello ") == "hello" assert utils.normalize_input("hello\t") == "hello" assert utils.normalize_input("hello \n ") == "hello" assert utils.normalize_input("hello\n\t") == "hello" # Test normalize_input on encompassing whitespace assert utils.normalize_input(" hello ") == "hello" assert utils.normalize_input(" hello\n ") == "hello" assert utils.normalize_input(" \nhello ") == "hello" assert utils.normalize_input("\t\thello\n\n") == "hello" assert utils.normalize_input(" \t\nhello\n\t ") == "hello" # Test normalize_input on encompassed whitespace assert utils.normalize_input("hello world") == "hello world" assert utils.normalize_input(" hello world") == "hello world" assert utils.normalize_input("hello world ") == "hello world" assert utils.normalize_input(" hello world ") == "hello world" assert utils.normalize_input("\t\n hello world \n\t") == "hello world" # Test normalize_input on uppercase text assert utils.normalize_input("Hello") == "hello" assert utils.normalize_input("HELLO") == "hello" assert utils.normalize_input("hElLo") == "hello" assert utils.normalize_input("HeLlO") == "hello" assert utils.normalize_input(" \t\n HeLlO \t\n ") == "hello" # Test normalize_input on common flag formats assert utils.normalize_input("CTF{ }") == \ "ctf{ }" assert utils.normalize_input("this_is_a_flag") == \ "this_is_a_flag" assert utils.normalize_input("CTF{this_is_a_flag}") == \ "ctf{this_is_a_flag}" assert utils.normalize_input("\t CTF{this_is_a_flag} \n") == \ "ctf{this_is_a_flag}" assert utils.normalize_input("\t CTF{THIS_IS_A_FLAG} \n") == \ "ctf{this_is_a_flag}" assert utils.normalize_input("this-is-a-flag") == \ "this-is-a-flag" assert utils.normalize_input("CTF{this-is-a-flag}") == \ "ctf{this-is-a-flag}" assert utils.normalize_input("\t CTF{this-is-a-flag} \n") == \ "ctf{this-is-a-flag}" assert utils.normalize_input("\t CTF{THIS-IS-A-FLAG} \n") == \ "ctf{this-is-a-flag}" assert utils.normalize_input("this,is,a,flag") == \ "this,is,a,flag" assert utils.normalize_input("CTF{this,is,a,flag}") == \ "ctf{this,is,a,flag}" assert utils.normalize_input("\t CTF{this,is,a,flag} \n") == \ "ctf{this,is,a,flag}" assert utils.normalize_input("\t CTF{THIS,IS,A,FLAG} \n") == \ "ctf{this,is,a,flag}" assert utils.normalize_input("this/is/a/flag") == \ "this/is/a/flag" assert utils.normalize_input("CTF{this/is/a/flag}") == \ "ctf{this/is/a/flag}" assert utils.normalize_input("\t CTF{this/is/a/flag} \n") == \ "ctf{this/is/a/flag}" assert utils.normalize_input("\t CTF{THIS/IS/A/FLAG} \n") == \ "ctf{this/is/a/flag}" assert utils.normalize_input("this|is|a|flag") == \ "this|is|a|flag" assert utils.normalize_input("CTF{this|is|a|flag}") == \ "ctf{this|is|a|flag}" assert utils.normalize_input("\t CTF{this|is|a|flag} \n") == \ "ctf{this|is|a|flag}" assert utils.normalize_input("\t CTF{THIS|IS|A|FLAG} \n") == \ "ctf{this|is|a|flag}" assert utils.normalize_input("this+is+a+flag") == \ "this+is+a+flag" assert utils.normalize_input("CTF{this+is+a+flag}") == \ "ctf{this+is+a+flag}" assert utils.normalize_input("\t CTF{this+is+a+flag} \n") == \ "ctf{this+is+a+flag}" assert utils.normalize_input("\t CTF{THIS+IS+A+FLAG} \n") == \ "ctf{this+is+a+flag}" return True